A series about the surprising benefits of writing and maintaining clean code
Conditional complexity causes code to be more complicated to understand and therefore to maintain. In addition, conditional complexity is usually an indicator that the code is coupled.
In the case that we want to increase the quality of our code, it is advisable to avoid generating code in which there is conditional complexity.
This post will present some of the techniques and recommendations that can be applied to any code to avoid conditional complexity.
Don’t Use Flags As Function Parameters
The first advice I have to give you to avoid complexity is to eliminate flags as parameters of a function.
Instead, we must create two functions that implement the logic of our problem, instead of using a single function in which we have the logic of the two functionalities since they are different.
The first example shows how the parameter
isPremium is used, which will make the decision to use one function or another.
On the other hand, the most correct path would be the one in which we have a declarative way to describe the two functionalities using two different functions for it.
Don’t make me think! Please encapsulate the conditions in a function that has semantic value.
In the first example, you can see how there is a complex condition that makes anyone think, while in the second example, it is easily understandable when reading the name of the function.
Replace Nested Conditionals With Guard Clauses
This advice is vital in the lives of programmers. You should not have nested conditionals.
One of the main techniques that allow us to avoid nested conditionals is the guard clauses technique. Just imagine developing without needing the
else keyword perfectly.
The following example shows the guard clauses in a demo code, where the reading of the code has improved considerably with a technique that could be automated even by an IDE.
Therefore, do not hesitate to use it when it is interesting, you just have to think of the logic to the contrary that they taught you in programming courses.
If you want to delve deeper into the guard clauses you keep, I recommend you read my specific article: Guard clauses.
Another common error that can be seen in code of a junior programmer is the constant checking of whether the object is null and, depending on that, check whether a default action is shown or not. This pattern is known as null-object pattern.
The following example shows how you have to check for each of the objects in an array if the animal is null or not able to emit the sound.
On the other hand, if we create an object that encapsulates the behavior of the null object, we will not need to perform said verification, as shown in the code in which the pattern is applied.
If you want to go deeper into this pattern, I recommend you read my specific article: Null-object pattern.
Remove Conditionals Using Polymorphism
switch control structure is a tool that most programmers think is cleaner than nesting if (regardless of whether they have a different behavior) we should think about everything else.
If we have a
switch in our code we must think that we have just introduced a great complexity to our code that will eventually make us think too much.
The following example shows the misuse of these conditionals to define the logic of a method based on the type of the object.
In this case, we can make use of a solution based on inheritance that makes use of polymorphism to avoid this complexity since a class will be created for each of these specific types.
In this way, we will have a more declarative solution since we will have the definition of the method in each of the types of concrete objects.
Remove Conditionals Using the Strategy Pattern (Composition)/Command Pattern
Other patterns that allow us to avoid conditional complexity in our code is the application of the strategy and command design patterns.
In the concrete example that illustrates this section, you can see the strategy pattern in which the strategy is selected dynamically.
Notice how the complexity of the
switch control structure is eliminated using different strategies to solve this problem.
In this post, we have presented some recommendations for avoiding conditional complexity.
Conditional complexity makes the code more complicated to read. In addition, it is usually an indication that the code is coupled and therefore is not very flexible.
In this article, different techniques and recommendations have been presented that allow us to avoid conditional complexity in our code by making it climb a quality step.
Finally, the points we have addressed are the following:
- Don’t use flags as function parameters.
- Encapsulate conditionals.
- Replace nested conditional with guard clauses.
- Remove conditionals using polymorphism.
- Remove conditionals using the null-object pattern.
- Remove conditionals using the strategy pattern (composition).
- Remove conditionals using the command pattern.