Say No to If/Else Statements

Ryan Francis
LaunchPad Lab
Published in
3 min readMay 23, 2017

One of the first lessons we learn in programming is how to write a conditional. If/Else statements seem so central to programming that it is hard to imagine a world without them.

Over the last year, I have made it my goal to minimize my usage of If/Else. I have found that it is actually quite easy and results in code that is much easier to read and maintain.

Example If/Else Statement

def run
water_temperature = Water.get_last_temperature
if water_temperature > 100.0
Timer.start(end_in: 8.minutes)
else
temperature_left = 100.0 — water_temperature
time_left_in_minutes = (temperature_left / TEMP_INCREASE_PER_MINUTE)
send_message(“Minutes remaining: #{time_left_in_minutes}”)
end
end

I hope you’re confused — I know I would be if this was the first time I read that method.

Here are a few questions that may have popped up as you read that method:

- Why are we checking that the water_temperature exceeds 100.0? What is special about 100.0?
- Why do we start a timer and set the timer to 8 minutes when temperature exceeds 100.0?
- What is happening in the second branch of this conditional and why is it implemented?

Quick Refactoring

Now, let’s rewrite this same If/Else statement but with the following changes:

1. Each branch of the conditional becomes a method
2. The conditional check itself (water_temperature > 100.0) becomes a method

def run
if boiling?
set_timer_for_pasta
else
show_time_until_boiling
end
end
privatedef water_temperature
Water.get_last_temperature
end
def boiling?
water_temperature >= 100.0
end
def set_timer_for_pasta
Timer.start(end_in: 8.minutes)
end
def show_time_until_boiling
temperature_left = 100.0 - water_temperature
time_left_in_minutes = (temperature_left / TEMP_INCREASE_PER_MINUTE)
send_message("Minutes remaining: #{time_left_in_minutes}")
end

With the simplified If/Else statement, we can now convert it to a ternary operator:

def run
boiling? ? set_timer_for_pasta : show_time_until_boiling
end
private# ...

Results

With this refactoring, a seemingly complicated if/else statement just became a dead simple ternary operation. Any programmer can understand this:

boiling? ? set_timer_for_pasta : show_time_until_boiling

Furthermore, our confusions earlier are now obvious:

- Why are we checking that the water_temperature exceeds 100.0? What is special about 100.0?

Answer: It is checking to see if the water is boiling (100.0 must be Celsius temperature by this logic)

- Why do we start a timer and set the timer to 8 minutes when temperature exceeds 100.0?

Answer: Water is boiling so it is time to start cooking some pasta and we don’t want to overcook it!

- What is happening in the second branch of this conditional and why is it implemented?

Answer: When the water is not boiling, we simply want to show how much time is left until the water will boil.

Small Methods, Big Documentation

The beauty of small methods is that they serve as wonderful little journalists, documenting their purpose to the world. If/Else statements are complicated because there are three components that often lack documentation: (1) the conditional, (2) the first branch, and (3) the second branch.

By simply breaking each of these components into small methods it allows us to drop the If/Else into a ternary every time, improving the readability of the logic itself while bolstering the documentation attached to the three components of the conditional.

One Simple Technique to Solve Complex Problems

The next time you spot an if/else statement, just move each branch and the conditional logic itself into separate methods and create a nice ternary operator. As you follow this pattern over time, your code will become much easier to digest and will naturally document itself.

Originally posted at launchpadlab.com

--

--

Ryan Francis
LaunchPad Lab

Developer at LaunchPad Lab and Co-Founder of Francis Lofts & Bunks