if-else blocks are integral part of any programming language. They are also a great tool in a programmers repertoire. But often times I see those blocks being used where a simpler piece of code would suffice.
The purpose of this post is not to improve the performance of those code blocks or claim that one is better than the other but rather to provide an alternative way to write the same code which at times could be more readable and concise.
e.g.
- use return statements efficiently
The above piece could be simplified as:
2. Use ternary operator
In case approach 1 doesn’t work, in cases where you want to return anything other than a boolean
e.g.
Use this:
More to come….