Eliminate many conditionals with this one weird trick
Recently I attempted to classify the conditionals in software according to where in the code they originate. The first category of these was: Checking a value returned to me from code I own. A large proportion of these can be eliminated quite simply.
Imagine we want to begin reading a file at the last saved read position, which may be null if we haven’t read anything yet:
var readOffset = fileReader.GetSavedReadPosition();
if (readOffset == null)
readOffset = new ReadPosition(fileReader, 0);
This code violates almost every rule in the book:
There is duplication, because both the caller and the callee have special code for the “no saved position” branch.
There is Connascence of Meaning, because both need a common understanding of what the special null value means.
We are violating the Tell, don’t Ask principle because we are making a decision on the basis of a value returned from a call to a different object.
All in all, this code has problems — and yet I see code like this everywhere I look. So, what to do?
Let’s look at this code from the Connascence point of view. The problem is the null value representing the special “not saved” case: both the caller and the callee have to agree to use null to mean that (hence “Connascence of Meaning”). Now, connascence becomes stronger with increasing distance. Our example smells strongly because the two connascent code fragments are in different classes; thus we could weaken it if we can bring the endpoints closer together.
Currently, the fileReader decides what to return when it has no saved read position, and the client code has to cope with that decision. What if, instead, the client code decides what it would like to get back in that case; what if it could simply tell the method what to return if it can’t do the thing we’re asking:
var readOffset = fileReader.GetSavedReadPosition(0);
Now the connascence has disappeared, and the conditional has disappeared with it. It’s as simple as that.
Many frameworks and APIs offer this kind of default return value parameter. For example, the standard Ruby Hash class provides this feature via the fetch method.
But what if there’s no sensible default value that we can pass into the called method? For example, consider the following case:
var username = auth.GetPrincipal();
if (username == null)
throw new UsernameNotFound();
auth.SetPassword(username, password);
We still have Connascence of Meaning due to the null return case; and we don’t want to push the error handling (or whatever) down into the authentication object, because different callers may want to handle the situation differently. But we can take a leaf from the default parameter book, and have the authentication object execute code for us:
var throwIfNotFound = () => throw new UsernameNotFound();
var username = auth.GetPrincipal(throwIfNotFound);
auth.SetPassword(username, password);
Again, the null value has disappeared, and so has the Connascence and the conditional. For the sake of symmetry we can often also dispense with the return value altogether and pass in both code branches to the callee:
auth.WithPrincipal(
(username) => auth.SetPassword(username, password),
() => throw new UsernameNotFound());
Pretty much every modern language supports this kind of code now:
Functional languages allow us to pass in functions to do the job; likewise Javascript;
C# lets us use lambdas via Linq; Ruby lets us pass in lambdas, and even Java 8 now allows lambdas;
C and C++ let us pass function pointers.
Some even let us use named parameters to make the options a little clearer; for example, we might write the above example in Ruby thus:
auth.withPrincipal(
if_known: lambda {|username| auth.set_pass(username, passwd)},
if_unknown: lambda {raise UsernameNotFound.new})
It’s as simple as that. By adopting these approaches it is possible to simplify a lot of code, effectively removing the connascence and the duplicated conditionals from the caller and the callee.
Kevin Rutherford's Blog
- Kevin Rutherford's profile
- 20 followers

