Code I Found: A Warning about Object Comparisons

Last time I posted a "Code I Found" entry some people got upset... well I'm doing it again, but I'm pairing it with an actual warning about object value comparisons in strongly typed languages. This example shows both a valid problem and some dumb coding that complicates it.

 


Code I Found

Its time for a new category here on my blog... I've been working through a good bit of older code at work and trying to update it to something like modern developer standards. Part of the fun of this has been finding weird, pointless language constructs. Check out this C# example:

if (IsEditable(ID))
    EnableEditButtons(true);
else
    EnableEditButtons(false);

Neat! This developer was able to use 4 lines of code to do one line of code:

EnableEditButtons(IsEditable(ID));

Do you have any good examples of code that just shouldn't have been?

Update!

The EnableEditButton() function from the last example is worth a look too...

protected void EnableEditButtons(bool Value)
{
    if (Value == false)
    {
        buttonAdd.Disabled = true;
        buttonDelete.Disabled = true;
    }
    else
    {
        buttonAdd.Disabled = false;
        buttonDelete.Disabled = false;
    }
}

Could be wrapped up as:

protected void EnableEditButtons(bool Value)
{
    buttonAdd.Disabled = buttonDelete.Disabled = !Value;
}

 
Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a C# .Net developer by day, a ColdFusion guru by night, and all around Javascript fanatic. Stay right here to read my technical posts as I grapple with mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web. Learn more about me.

Post a job. Find one. authenticjobs.com

Interested in becoming a sponsor? Contact me.