Coding is incomplete without logic. A code block may have single or multiple statements of code grouped together. Things are simple when we have single level code block like below:
But in real time coding we may need to consider nested logics to achieve business use cases. With time and addition of new functionalities, there are high probability that we may end up seeing Arrow Code. In this article I would like to share few tips to handle nested logical blocks
Scenario 1: Using map instead of multiple if else statements.
In the below example, we are using map to refactor nested if else. If new condition is getting added in the logic, we can just add that in the map. The first example shows the nested if else and second example shows the refactored code using map.
Scenario 2: Using Ternary operator to improve the readability.
In the below example, we are using ternary operator to refactor nested if else conditions. This approach improves the readability of the code. Worth noting that since we have range here, we cannot use map.