Art Of Programming

musings by Dmytrii Nagirniak

Shame on Me: AI With ApplicationException

I have just found a piece of code which is probably about 3 years old. This is active project that I work on it almost day by day.
The method below is a helper utility for unit testing (and for that matter is used only for unit testing) to compare 2 value wrapped in IElement (Enterprise Core Objects).
private static bool CheckOclEqual(IElement expectedElement, IElement actualElement)
{
    if (expectedElement == null && actualElement == null)
        return true;

    if (expectedElement == null)
        return false;
    if (actualElement == null)
        return false;

    object o1 = expectedElement.AsObject;
    object o2 = actualElement.AsObject;

    if (o1 == null && o2 == null)
        return true;

    if (o1 != null)
        return o1.Equals(o2);
    if (o2 != null)
        return o2.Equals(o1);
        
    throw new ApplicationException("Implementation bug: Invalid logic in this method.");
}

Do you see this “Implementation bug: Invalid logic in this method.”. Now, do you see the ApplicationException? That is ridiculous!
If I would ever get this exception i would think: System found a bug for me. It must be AI.
Instead of writing this stupid message wrapped in the system exception the last lines of the method should just look like:
    if (o1 != null)
        return o1.Equals(o2);
    return o2.Equals(o1);
    // No need in that stupid exception at all here
Well, nothing to say on this except:
  • shame on me;
  • it made me smile;
  • strangely, I have never got this exception :);
  • it overlived .NET 1.1, .NET 2.0, .NET 3.0 and jumped straight into .NET 3.5;
  • it is not yet a legacy code.
And BTW, ReSharper pointed me to this; I doubt I would ever look into that method/class.

Comments

Dmytrii Nagirniak
Huh, I hope I was the same person as I am now. Probably with different expectations and expirience :)

I cannot live without TDD at all. "Stressed? Write a test!".

The thing I like the most is that after writing set of tests for a feature, making them pass I finally come to implementing UI. It turns out to be as easy as hooking up UI elements together with newly implemented and well tested functionality.
No logic there (except some obvious things). After that, the application with a new feature "just works".

Haven't really came to BDD. It really looks like another view on TDD doing pretty much the same job but using different ideology.
Alexander
Shame on you :)

Though, I don't think that 3-yr old code can be considered 'your' code.

It's code of other person, you have been this person 4 yr before.

And yes - tools are good. Reshaper helped you, or whatever. I'm now crazy about TDD and BDD - tried this in a little learning project, and, damn, I like the way it works.

Comments