Friday, 4 March 2011

Comparing fluent interfaces for guard clauses [part 4]

In the last post on fluent guard checks I showed an interface that aggregates state, removing the trapdoor effect associated with multiple guards. It works, but might in some environments be an unnecessary contributor to memory pressure.

Performance-tweaked fluent chained guards

Rick Brewster came up with an alternative syntax that only instantiates a state object on the first fail. Otherwise, it just passes null. Again, the guard checks are extension methods that hang off the state object – even (I didn’t realise this worked) if the state object is null:

    .IsNotNull(message, "message")
    .SomeOtherCondition(message, "message")

The state object looks like (in this case, a simple equivalent – Rick’s is more sophisticated):

public class Validation
    private _exceptions = new List<exception>(1);
    public List<exception> Exceptions
        get { return _exceptions; }

Validate.Begin() looks like:

public static Validation Begin()
    return null;

An example guard check looks like:

public static Validation IsNotNull(
    this Validation state, 
    object value, 
    string name)
    if(value == null)
        if(state == null)
            state = new Validation();
            .Add(new ArgumentNullException(name));
    return state;

Check() just checks for any exceptions, and throws a ValidationException if there are any, which wraps the Exception list -- just like in part 3.

So, because the argument value and name aren't in the state object they need to be explicitly passed into each guard check; the check code is also more complicated because it does more than one thing.

This makes the syntax a little less clear to me, with a would-be-good-if-it-were-redundant Validate.Begin() always starting a guard chain, and with argument value and name sometimes appearing repeatedly.


So, the decision about whether to avoid creating an unnecessary state object determines to some extent how clear that syntax is. Deciding whether to break fast or accumulate guard state just means that you might need to have something to terminate the chain.

Whether you really need to avoid the state object creation is really app-specific; you’d need to think and test. I’ve hit the need for chained guard checks a number of times though, so I’d definitely look to implement that. My ideal solution is somewhere between the two.

Also interesting:

No comments: