Mommy, there be dragons in my code.

Every developer, and every shop has a Acme.Commons, or Acme.Core library, where they keep useful helper classes and extensions used in almost every project. Ayende has his Rhino Commons that I’ve seen him blogging about a few times but I never quite got around to actually take a look at the code…

…until today, and I’m shocked. In a mostly positive way. There are few good ideas there, that I would like to discuss.

The first class I found astonishing was HackExpiredException. My first reaction looked something like this:

Then I looked for some code that throws this exception and found this class:

/// <summary>
/// Helper class that allow to specify temporary hacks in a 
/// simple fashion
/// </summary>
/// <example>
/// <code>
/// TimeBombHack.Until(new DateTime(2007,12,31), "Ignoring security for the client demo");
/// </code>
/// </example>
public static class TimeBombHack
{
    /// <summary>
    /// Will throw with the specified message after the 
    /// <param name="expiredAt">The expiry date</param>
    /// <param name="message">The message.</param>
    /// </summary>
    public static void Until(DateTime expiredAt, string message)    
    {
        if (DateTime.Today > expiredAt)
            throw new HackExpiredException(
                string.Format(
                    "The hack ({0}) expired on ({1}). You really should fix it already",
                    message,
                    expiredAt));
    }
}

I really like the idea of being able to mark some parts of code as TODO and have it blown into your face in a controlled fashion when you forget about it. Too many times a forgotten quick and dirty hack slips under the radar and get left behind, forgotten waiting to blow up in production or during very important presentation.

Another, very neat idea I found in the library is the ThereBeDragonsAttribute class.

This marker attribute decorate dangerous parts of the code where for some reason the developer decided to do something in a way that is not straight-forward. This usually means that this is using some functionality that is not fully supported.

Using wacky, unsupported or hackish code is something everyone does occasionally. I usually comment this piece of code, be it a method or a class, but comments are often not enough. With little help of NDepend to bring these pieces of code to everyone’s attention this may be a very useful warning sign for everyone on the team to handle this code with special care.

There are many other useful classes in Rhino Commons, but most of them are pretty standard stuff (maybe with the exception of property indexers).

What other non-standard, helpful little (or not so little) classes do you use in your code?