One of the most common code smells when developing JavaScript applications is the excess of conditional chaining. In this article, I want to argue about how to prevent these cases through architecture and code.
Excessive conditional chaining is a common code smell in JavaScript applications. This article explores how to prevent and refactor these cases through improved architecture and coding practices.
Conditional chains are excessive logical operators used to express conditions in functions or methods. Let's look at an example using a React application:
As you can see in the example above, there is a chain of three conditions only to dictate the way this code should be rendered.
The conditions are:
In this code, there are two main problems:
That being said, the refactored version of the code above is:
This is one of the many ways to deal with conditional chainings using JavaScript's logical operators. As you can see in the code above, I've used a non-common approach to solve the conditional excess of the code.
The !! operator in JavaScript is used to coerce a value to a boolean. It takes advantage of the fact that JavaScript has truthy and falsy values. The first ! operator negates the value, turning truthy values into false and falsy values into true. The second ! negates it again, resulting in a boolean representation of the original value. This is often used to convert values like strings, numbers, or objects into a boolean value (either true or false), based on their truthiness or falsiness.
For example:
!!"Hello" evaluates to true because non-empty strings are truthy.
!!0 evaluates to false because 0 is falsy.
You must not take this as a rule, but in most of the cases where conditional chains are created, the excess of conditions are trying to parse and handle dynamic values, because if you're dealing with static values, the implementation tends to be much simpler and straightforward.
When you're modeling a database, you must have some concerns about the software's dependencies.
People typically learn this kind of dependency study through their IT college studies, but I'll illustrate it.
There are two kinds of dependencies on software:
Your back-end must be responsible for all the logic parts of your system, so, the majority of your functional dependencies must be handled by back-end functions, not by front-end screens or user interactions.
When you start to develop a new feature and to understand what it needs to work, such as props, interfaces, and parameters, you must have in mind what will be required, what will be optional, and what mustn't be used.
You must use the example above as an example of what not to do during your development sessions. As you can see, this interface has only optional parameters, but I doubt this component will only have "maybe" variables attached to it.
You need to understand how your component is supposed to work before developing it and pushing a lot of confusing code to your front-end application. Instead of dealing with a lot of conditions, it is easier if you just decide: what will be used by the component and what will not.
After considering it better, you will come up with something like this:
Now the interface only has required parameters that will certainly be used through the component's lifetime inside your app, not having a lot of optional parameters that could never be defined or used like the component from before.
Preventing and refactoring conditional chains leads to cleaner, more maintainable code. By understanding your component's requirements, shifting logic to the back-end where appropriate, and designing clear interfaces, you can significantly reduce the need for complex conditional chains in your front-end code.
Photo by Samsung Memory on Unsplash
The above is the detailed content of Preventing/Refactoring Conditional Chainings. For more information, please follow other related articles on the PHP Chinese website!