Don’t code like this at home, kids

Sometimes when you’re reviewing someone else’s code, you find interesting pieces of “art“. Like the following loop construct in a C# application:

1
2
3
4
5
6
7
8
9
10
11
12
    int i = anArray.Length - 1;

    while (true)
    {
        if (i < 0)
            break;
        else
        {
...    
            i--;
        }
    }

Doh.

You might be tempted to rewrite it as:

1
2
3
    for( int i = anArray.Length - 1; i >= 0; i-- ) {
...    
    }

but it would be too simple, too boring, wouldn’t it?

Leave a Reply

Your email address will not be published. Required fields are marked *