If statements are probably the most common thing that makes your code spaghetti. I’d like to share a few techniques that can help you make your code more readable.
Let’s examine the following example:
Maps
One of the techniques is to use map instead of if statements.
The above example is simply key-value pair mapping, but with help of basic functional interfaces you can use this also for a business logic.
“If Applicable” pattern - aka IF inversion of control
A really nice one. You should consider using it if you expect that “if” logic will grow up. It looks like this:
(1) -> you need common interface for a logic. Like "if body block"
(2) -> condition. Like "if condition statement"
(3) -> a few implementations. Like next "else ifs"
(4) -> example how you can use it in spring boot application
Above example is a Java example, but this pattern can be easily implement in Typescript, or Javascript. I’m using it in Angular as well.
Angular tip
A tricky part for angular is that you cannot inject list of “interfaces”. The solution is to use InjectionToken.