Clean Code Applied to JavaScript (Part 6: Avoid Conditional Complexity)

A series about the surprising benefits of writing and maintaining clean code

Carlos Caballero
Jan 10 · 4 min read
Photo by Annie Spratt on Unsplash

This piece is the sixth in a series that delves into the topic of clean code, as applied to JavaScript.

In this series, we discuss the classic tips around clean code that every programmer should know and apply them to a specific JavaScript/TypeScript language.


Introduction

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.

In this specific case, we will work using the JavaScript/TypeScript programming language but the concepts we are discussing in this post are extrapolated to any programming language since what we are doing is giving recommendations and techniques that are not tricks for a specific programming language


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.


Encapsulate Conditionals

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.


Null-Object Pattern

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

The 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.

If you want to learn more about these two patterns, I recommend reading the specific articles in which I have dived into these patterns: Strategy pattern and command pattern.

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.


Conclusion

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:

Better Programming

Advice for programmers.

Carlos Caballero

Written by

Hi! My name is Carlos Caballero and I’m PhD. in Computer Science from Málaga, Spain. Teaching developers and degree/master computer science how to be experts!

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade