Thursday, April 16, 2009

C# - Keep me from hurting myself

I just fixed a funny bug where it was behaving very odd and it was because I had a return statement where I intended to have a continue. I musta looked at that line a dozen times and did not think anything of it. Just went back to the basics of debugging and ran through the code line by line, iteration by iteration. What an embarassing relief!

[Upon further reflection]
What if there was new language feature that made this less likely to happen. Does it help readability and/or intent? Take the following loop.

foreach (var i in theCollection)
{
if (i.Visible == false)
continue;

// other code goes here
}

What if I could change the syntax to something like this...

foreach (var i in theCollection)
except i.Visible == false
{
// other code goes here
}

Upon futher thought what if I just used LINQ instead?

var iVisible = theCollection.FindAll( i => i.Visible)

foreach( var i in iVisible)
{
// other code goes here
}

Not to bad from a syntax point of view. I don't like that I am making a shallow copy of the original list for the purposes of only iterating through it. The language extension above works on the original list.

No comments:

Post a Comment