"Someone introduced me to The Daily WTF back when I was a freshman at university, and I've been reading it ever sense," writes Patrick, "I was convinced that most of the bad code was made up or exaggerated, and that I'd never have the horrifyingly irresistible compulsion pleasure of submitting bad code."

"However, now that I'm in the real world, and getting settled into my new job at certain, large financial services provider, I've started reviewing their codebase to get a feel for how they do things around here. Though the codebase was an uncommented beast conjured up through outsourcing to Kerbleckistan, none of it was quite WTF. At least, until I got to the code written by our in-house rockstar.

"Not only did our rockstar re-implement the C# generic collections (but this time with liberal use of uncommented lambda functions turning values into keys and getting items), but he decided to make sure the code he wrote was safe and, well, guarded from any unexpected exceptions. So he created a Guard class that protects against NullReferenceExceptions by...throwing ArgumentExceptions if the argument is null! But just to be clear to everyone that that the method ArgumentNotNull is an assertion, he annotated it with a custom tag that asserts to fellow coders that the argument better not be null or else.

public static class Guard
{
    /// <summary>
    /// Checks a string argument to ensure it isn't null or empty
    /// </summary>
    /// <param name="argumentValue">The argument value to check.</param>
    /// <param name="argumentName">The name of the argument.</param>
    /// <exception cref="ArgumentException">argumentValue is null or an empty string</exception>
    [AssertionMethod]
    public static void ArgumentNotNullOrEmptyString(
        [AssertionCondition(AssertionConditionType.IsNotNull)] string argumentValue,
        string argumentName)
    {
        if (String.IsNullOrEmpty(argumentValue))
            throw new ArgumentException("Argument cannot be null or empty",argumentName);
    }

    /// <summary>
    /// Checks an argument to ensure it isn't null
    /// </summary>
    /// <param name="argumentValue">The argument value to check.</param>
    /// <param name="argumentName">The name of the argument.</param>
    /// <exception cref="ArgumentNullException">argumentValue is null</exception>
    [AssertionMethod]
    public static void ArgumentNotNull(
        [AssertionCondition(AssertionConditionType.IsNotNull)] object argumentValue,
        string argumentName)
    {
        if (argumentValue == null)
            throw new ArgumentNullException(argumentName);
    }

    /// <summary>
    /// Checks an Enum argument to ensure that its value is defined by the specified Enum type.
    /// </summary>
    /// <param name="enumType">The Enum type the value should correspond to.</param>
    /// <param name="value">The value to check for.</param>
    /// <param name="argumentName">The name of the argument holding the value.</param>
    public static void EnumValueIsDefined(Type enumType, object value, string argumentName)
    {
        if (Enum.IsDefined(enumType, value) == false)
            throw new ArgumentException("Value is not contained in enum [" + enumType.ToString() + "]",argumentName);
    }
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!