Tigraine
Daniel Hoelbling talks about programming

Good ideas worth spreading: Guards

June 6th, 2009 . by Daniel Hölbling

It’s amazing how much smarter you can become by simply looking at other people’s code. So, today I spent almost half the morning looking at different test frameworks from the TDD/BDD world looking for cool tricks I haven’t thought of (I examined MSpec, NBehave, NSpec and xUnit). One of those interesting little tricks (trivial at best, but valuable) is the following I found in xUnit’s Guard.cs:

Guard class, used for guard clauses and argument validation

Imagine the following method:

public bool Authenticate(string username, string password)
{
    return username == "daniel" && password == "tigraine";
}

Let’s say my specification for this method says: “input username and password can’t be null and should return a ArgumentNullException”. Reasonable, since we never trust input. So, usually I’d create guard clauses at the top of my method to protect me from said bad input:

public bool Authenticate(string username, string password)
{
    if (username == null)
        throw new ArgumentNullException(username);
    if (password == null)
        throw new ArgumentNullException(password);

    return username == "daniel" && password == "tigraine";
}

I always thought about this as rather readable and nice to work with, until I saw what xUnit did in Guard.cs, allowing me to shorten the above to a simple:

public bool Authenticate(string username, string password)
{
    Guard.ArgumentNotNull("username", username);
    Guard.ArgumentNotNull("password", password);

    return username == "daniel" && password == "tigraine";
}

I still believe this can be improved upon, maybe making it only one argument instead of two, but for now this is way better than what I used to write before.


  • http://bradwilson.typepad.com/ Brad Wilson

    If you target 3.5, you could write a guard which used expressions, and then you could evaluate the expression in order to fill things out. Unfortunately, the syntax ends up a little wonky, but at least you’re not repeating yourself:

    Guard.ArgumentNotNull(() => username);

    You could also do the same thing with complex expressions:

    Guard.Precondition(() => username.Length > 10);

    and when you throw the exception, it can even contain the actual condition code, extracted from the expression.

  • http://www.tigraine.at Daniel Hölbling

    Hi Brad, thanks for your comment.

    My first instinct was also to throw some of 3.5s syntactic sugar at the problem, but I then came up with a much simpler solution that isn’t too esoteric but still cuts down on the typing:
    Resharper Live template.

    I simply created a Resharper live template with the following content:

    Guard.ArgumentNotNull(“$parametername$”, $parametername$);
    $END$

    that way I only need to type the parametername once and it will still be there twice. Not perfect, but does the trick.

  • http://www.tigraine.at/2009/06/06/dry-guard-clause-performance/ DRY Guard clause performance | Tigraine

    [...] Wilson left me an inspiring comment on my post about his Guard class that I immediately tried out: If you target 3.5, you could write a guard which used expressions, [...]