Reducing Conditional Logic to a Single Location

My Anti If Campaign post still generates a lot of questions and discussion. In summary:

A recent real world example was refactored which highlighted the points previously, but inverts the problem and solution. How do you remove conditional statements if your system itself has to make the decisions internally?


The simplified example shows the result of invoking a third party service. This result contained a flag indicating either success or failure. Effectively there were two hidden types here. Finally the result was returned based on the HTTP status code.

After the result of the third party call, the domain would decide how to respond.

Both the client and the domain logic was split over multiple source files. This made noticing the duplication tricky. Both the client and the domain also knew the fact that the result of the third party call can succeed or fail.

Sadly the domain violates the SRP at a method level. While not a requirement yet, if further status codes are required or the contents of responses controlled flow, we are in trouble. The type flag would need to evolve from a boolean to something more complex. The contents of the responses may also need to be provided. This solution could leak HTTP details down into the domain unless careful.


Recognise the boolean flag is actually hidden two types. Remove the flag and introduce a concrete type for each path. Each concrete type performs the right operation. In this case executing the relevant methods within the domain.

Each concrete type is easy to test, change or throw away. In this example an interface is provided. This contains just the necessary methods that the process requires. The domain is now simplified. The domain instance itself is simply provided as an argument. The concrete instance of each result will perform the right operation.



Stick the procedural code on the edge of your system and be done with it. Just ensure that you only perform such checks once.

Just because you are not performing explicit type checks, the use of boolean flags usually indicates at least two hidden types.

The anti if campaign is not the removal of all conditional checks. They need to happen somewhere. Just try to limit them.