Home > View Post

Trying too hard too soon

Jeremy Miller's recent post about a WTF story reminded me about the reason I posted about Premature Optimisation a few months back. You know the sort of thing...

"... developers who obsess about optimizing string concatenation while writing systems that are chatty to the database."

My favourite WTF story goes like this. A developer needed to loop 256 times and wanted to avoid the 'expense' of the evaluation of the counter in the loop. Here's what they came up with:

byte i = 0;

try
{
    checked
    {
        while (true)
        {
            i++;
        }
    }
}
catch
{}

If you haven't worked out what's going on here, the maximum value that a byte can contain is 255, so if you try and push it higher than that inside a checked block you'll get an exception. As *clever* as this is, it would actually be much slower than a normal for loop because exceptions themselves are, relatively speaking, expensive*.

It's easy to poke fun at situations like this but I think we've all been there at one time or another.

*If you're now tempted to worry about throwing exceptions please go and read the post on premature optimisation, and maybe follow it up with this: exceptions :)

Tags: Other

 
Josh Post By Josh Twist
12:44 AM
27 Jun 2006

» Next Post: XmlWriter or string concatenation
« Previous Post: Run dialog shortcuts

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.