DESIGNING BETTER SOFTWARE
Better Software Without If-Else
5 Ways to Replace If-Else. Beginner to advanced examples
Let me just say this right off the bat: If-Else is often a poor choice.
It leads to complicated designs, less readable code, and may be pain inducing to refactor.
Nevertheless, If-Else has become the de facto solution for code branching — which does make sense. It’s one of the first things any aspiring developer is taught. Unfortunately, lots of developers never advance to more suitable branching strategies.
Some live by the mantra: If-Else is a hammer and everything’s a nail.
The inability to determine when to use a more suitable approach is among those that distinguish juniors from seniors.
I’ll show you some techniques and patterns that’ll put an end to this horrific practice.
The difficulty will increase with each example.
Quick side-note: if you’re a visual learner, you can follow this new video that explains different approaches to eliminating if-else and switch cases.