1. Early returns
When we start learning programming, we are taught that we should only have one return statement. While the advice is not wrong, it becomes challenging to read code if the code base is large. Even though we should follow single responsibility and other SOLID principles while writing code, early returns make the code more readable and give a clear idea of the flow of code.
Early returns provide a way of writing better conditional expressions and make the code more readable. While some people will disagree with this, but my preference is to return early if there is an invalid condition.
Instead of having:
We can do:
It definitely will boil down to preference and at times the piece of code being written as well, but early returns definitely help in quicker glances, lesser code blocks and easier debugging.
2. Avoiding multiple conditionals
Multiple if else blocks can be replaced by switch statements to improve readability.
can be better written as:
But there is an even better way. We can use object literals/maps to rewrite it in a concise manner:
The object literal/dictionary is the cleanest form of the implementation and also adds the benefit of being extensible by just adding key values to it.
We could have used
mapping[key] && mapping[key]() instead of the
Object.keys implementation but if there were some keys like
toString, it would mess it up. So it future proofs our implementation from a potential gotcha in the future.
3. Use built-in Array methods
For matching more than one condition, we usually write the code as:
But we can use the inbuilt Array.includes() method for doing so and not have to worry about adding so many “or” conditions.
Or, if we were dealing with array of objects and wanted to check properties, we could make use of the Array.some() method to check if a condition is met:
We can similarly use Array.every to check if all objects have a property or not, or Array.find() to perform some logic.
4. Using default values and destructuring
Making use of the latest ECMAScript features is always a good thing. And providing default values removes the need for some conditionals while initializing variables. And the same goes for making use of object destructuring too.
We can use default function values to remove the condition around value initialization.
And if we were dealing with objects, we could make use of the destructuring operator. From:
can be rewritten to:
If you want to know more about the destructuring operator, you can read our post about destructuring and spread operator.
We can also use the optional chaining and nullish coalescing operators for similar initialization-related conditions.
And that is all there is regarding writing better conditional expressions. Using these techniques for writing better conditional expressions, we can make our code cleaner and more readable. If you have any other suggestions on how to write better conditional expressions, feel free to drop a comment below, and we will add it to our list!