Stop Using If Else Statements Write Clean, Maintainable Code Without
Stop Using If Else Statements Write Clean, Maintainable Code Without
The Startup
Medium's largest
active publication,
followed by +699K
people. Follow to
join our community.
Follow
4.6K
84
It’s perhaps even your default mode to use If-Else. But, let’s put an end to Top highlight
Note that you’d use this approach if you’re writing a class with methods that
need its implementations to be changed depending on the current state.
You’d apply another approach if you’re not dealing with an object’s
changing state.
Even if you’ve heard about the state pattern, you might wonder how it is
implemented in production-ready code.
For anyone who’s still in the dark, here’s a very brief introduction.
Gone are the days with code looking like this below.
Warning: PTSD trigger — also, hope you caught the logical error in here (other than the whole thing being a
mess)
You’ve certainly written more complicated branching before. I have for sure
some years ago.
The branching logic above isn’t even very complex — but try adding new
conditions and you’ll see the thing explode.
Also, if you think creating new classes instead of simply using branching
statements sounds annoying, wait till you see it in action. It’s concise and
elegant.
Even better, it’ll make your codebase more SOLID, except for the “D” part
tho.
medium.com
Let’s create a very simple Booking class, that has a few states. It’ll also have
two public methods: Accept() and Cancel() .
I’ve drawn a diagram to the best of my abilities that displays the diPerent
states a booking may be in.
3. Let the Booking ` class have a private or internal method that takes the
Demo time
First, we need a base state class that all states will inherit from.
Notice how this base class also has the two methods, Accept and Cancel —
although here they are marked as internal.
Learn more about the Null Object Pattern in Stop Checking for
Nulls
Null Object Pattern, Factory Methods — Let’s see some production ready
code!
medium.com
Doing this allows us to remove much of the conditional logic, and lets each
state only focus on what’s important to itself — the current state also has
the opportunity to transition the booking to a new state.
It’s as simple as that. You’ll no longer have to deal with unwieldy if-else
statements.
The state object is not important when saving an object to e.g. an SQL or
NoSQL database. Only knowing the object’s state and how it should be
mapped to a column is important.
Having many, specialized classes will make your codebase more readable,
maintainable, and simply overall more enjoyable to work with.
. . .
Examples by SourceMaking
medium.com
medium.com
medium.com
medium.com
Connect on LinkedIn
WRITTEN BY
More from The Startup More from The Startup More from The Startup
The Real Reason Nobody Is Two Tools Every Data Scientist Will You Enroll At “Google
Buying Your Startup’s Product Should Use for Their Next ML University” For $49/Month?
Aaron Dinin, PhD in The Startup Project Sayeed Ibrahim Ahmed in The Startup
Sep 3 · 8 min read 1.94K Aug 28 · 7 min read 3.1K
Braden Riggs in The Startup
Sep 4 · 9 min read 415