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:

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

private

def 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:

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

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

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.

Ryan Francis

Partner, President, & Head of Sales

As ambitious as he is tall, Ryan has a passion for creation. In 2012, he created Francis Lofts & Bunks, a company in Western Ohio that manufactures aluminum loft beds and bunk beds. Equipped with a burning desire to build things that are useful to others, Ryan has come into his own in web development, combining creativity, logic, and an empathy for others to help clients launch outstanding, easy-to-use products.

Reach Out

Ready to Build Something Great?

Partner with us to develop technology to grow your business.

Get our latest articles delivered to your inbox