0% found this document useful (0 votes)
3 views1 page

Stop Using If Else Statements Write Clean, Maintainable Code Without

The article discusses the limitations of using If-Else statements in programming and advocates for the use of the state pattern to create cleaner, more maintainable code. It outlines a three-step process for implementing the state pattern, which involves creating a base state class and individual state classes that manage behavior based on the object's current state. The author emphasizes that while some conditional logic remains necessary, adopting this pattern enhances code readability and reduces complexity.

Uploaded by

Andrei Grichine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Stop Using If Else Statements Write Clean, Maintainable Code Without

The article discusses the limitations of using If-Else statements in programming and advocates for the use of the state pattern to create cleaner, more maintainable code. It outlines a three-step process for implementing the state pattern, which involves creating a base state class and individual state classes that manage behavior based on the object's current state. The author emphasizes that while some conditional logic remains necessary, adopting this pattern enhances code readability and reduces complexity.

Uploaded by

Andrei Grichine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

WE’RE REIMAGINING THE STARTUP ARE YOU CURIOUS?

The Startup
Medium's largest
active publication,
followed by +699K
people. Follow to
join our community.

Follow

4.6K

84

APPLIED DESIGN PATTERNS: STATE

Stop Using If-Else Statements


Write clean, maintainable code without if-else.

Nicklas Millard Follow


Jun 16 · 5 min read

You’ve watched countless tutorials using If-Else statements. You’ve probably


also read programming books promoting the use of If-Else as the de facto
branching technique.

It’s perhaps even your default mode to use If-Else. But, let’s put an end to Top highlight

that right now, by replacing If-Else with the state objects.

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.

You’ll increase complexity with any new conditional


requirement implemented using If-Else.

Applying the state pattern, you simply alter an


objects behavior using specialized state objects
instead of If-Else statements.

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.

The Best Advice for Delivering Better Software From My Mentor


Releasing better software at increased velocity is a matter of focusing your
attention to what matters — at the right…

medium.com

“Okay, I’m convinced If-Else is evil, now show me how to avoid


messy branching code”
We’ll be looking at how I replace If-Else branching in production-ready
code. It’s a made-up example, but the approach is the same I’ve used in
codebases for large clients.

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.

Refactoring branching logic out of our code is a three step process:

1. Create an abstract base state class

2. Implement each state as a separate class inheriting from base state

3. Let the Booking ` class have a private or internal method that takes the

state base class as a parameter

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.

Additionally, the base state has a “special” EnterState(Booking booking)

method. This is called whenever a new state is assigned to the booking


object.

Secondly, we’re making separate classes for each state we want to


represent.

Notice how each class represents a state as described in the beautiful


diagram above. Also, the CancelledState won’t allow our booking to
transition to a new state. This class is very similar in spirit to the Null Object
Pattern.

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

Finally, the booking class itself.

See how the booking class is simply delegating the implementation of


Accept and Cancel to its state object?

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.

How to deal with new conditional features?


If the new feature would normally have been implemented using some
conditional checking, you can now just create a new state class.

It’s as simple as that. You’ll no longer have to deal with unwieldy if-else
statements.

How do I persist the state object in a database?


You don’t.

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.

You can map a state to a friendly type name, an enum or an integer.


Whatever you’re comfortable with, as long as you have some way of
converting the saved value back into a state object.

But you’re still using IFs?


Yes — they’re essential. Especially when used as guard clauses. It’s the If-
Else combination that is a root cause for maintainability headaches.

It’s a lot of additional classes!


Indeed. As I’ve mentioned in another article, complexity does not originate
from the number of classes you have, but from the responsibilities those
classes take.

Having many, specialized classes will make your codebase more readable,
maintainable, and simply overall more enjoyable to work with.

. . .

Resources for the curious


--------------------------

Examples by Refactoring Guru

Examples by SourceMaking

How to make your code more Object-Oriented by Zoran Horvat

C# Design Patterns: State by Marc Gilbert

Hard-Coded, UnconPgurable Classes Are Headache-Inducing


How to create easily conWgurable classes, featuring a great step-by-step
refactoring example

medium.com

Refactoring From Trash to SOLID


Amp up your code quality, the easy way.

medium.com

32 Opinionated Advice and Lessons Learned in Software


Development
Useful advice for any developer

medium.com

Better Software Without If-Else


5 Ways to Replace If-Else. Beginner to advanced examples

medium.com

Nicklas Millard is a software development engineer in one of the fastest-


growing banks, building mission-critical \nancial services infrastructure.

Previously, he was a Big4 Senior Tech Consultant developing software for


commercial clients and government institutions.

Connect on LinkedIn

Programming Software Development Software Engineering Technology Csharp

4.6K claps 84 responses

WRITTEN BY

Nicklas Millard Follow

Tech writer with 414K+ views. Danish C# backend engineer in


banking. Ex Big4 senior tech consultant. Sharing my opinion
and what I learn.

The Startup Follow

Medium's largest active publication, followed by +699K people.


Follow to join our community.

More From Medium

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

Discover Medium Make Medium yours Explore your membership


Welcome to a place where words matter. On Medium, Follow all the topics you care about, and we’ll deliver the Thank you for being a member of Medium. You get
smart voices and original ideas take center stage - with best stories for you to your homepage and inbox. Explore unlimited access to insightful stories from amazing
no ads in sight. Watch thinkers and storytellers. Browse

About Help Legal

You might also like