Adam Ralph

A blog about how software development fills in the gaps between snowboarding


Guard Clause Abuse

Vote on Hacker News

Today sees the release of version 0.10 of the mighty LiteGuard library.

After announcing the release on JabbR, I found myself considering and commenting on the common abuse of guard clauses that I see so very often:

public class Car
{
    private readonly string numberPlate:

    public Car(string numberPlate)
    {
        Guard.AgainstNull("numberPlate", numberPlate);
        
        this.numberPlate = numberPlate;
    }
    
    public string NumberPlate
    {
        get { return this.numberPlate; }
    }
}

(I’m using the British English term “number plate”, known as “license plate” in the US and some other countries.)

The above code should not be using a guard clause and should not raise an ArgumentNullException. It should throw some kind of model* exception, since it is encapsulating a rule which has been identified in the domain of the application. The code is not de-referencing numberPlate so it doesn’t need a guard clause:

public class Car
{
    private readonly string numberPlate:

    public Car(string numberPlate)
    {
        if (numberPlate == null)
        {
            throw new UsedCarDealershipException("The number plate is missing.");
        }
        
        this.numberPlate = numberPlate;
    }
    
    public string NumberPlate
    {
        get { return this.numberPlate; }
    }
}

* Feel free to substitute the word ‘model’ for ‘business’, according to taste :wink:.

Vote on Hacker News
blog comments powered by Disqus