3 Alternatives to If Statements That Make your Code More Readable

Jonny Jackson
The Startup
Published in
3 min readSep 24, 2020

Are you taking advantage of these features of Python that can help make your code make more sense?

Code readability is not just about making your code shorter. It is about reducing the intellectual burden of understanding what your code is doing and how it is doing it.

Some general rules of thumb are: explicit is better than implicit, clear is better than short, and don’t repeat yourself (DRY).

With that in mind, here are three (plus one bonus!) examples of situations where avoiding an if statement can help make your code more readable. They are:

  1. Testing for equality with more than one possible value
  2. Selecting one value from a set of multiple possible values
  3. Dynamically choosing one function to execute from a set of multiple possible functions (bonus: with custom arguments)

1. Testing for equality with more than one value? Use 'in’!

The not-so-good way:

The better way:

Why it’s better:

  • As the number of allowed values increases, the ‘not-so-good’ way will stretch your if statement out.
  • Want to change the allowed values? Simply modify the list.
  • By assigning the list of allowed values to a variable, you can use it elsewhere in your code too. For example, it makes writing assertions a breeze:

2. Got lots of ‘elifs’ for your if statement? Use a dictionary!

The not-so-good way:

The better way:

Why it’s better:

  • As the number of allowed values increases, the ‘not-so-good’ way will stretch your if statement out.
  • Made another possible value? Just add it to the dictionary!
  • Allowed values can be spotted at a glance and also possibly used elsewhere using info_dict.keys().

3. Want to execute a function dynamically? Use a dictionary (again)!

The not-so-good way:

The better way:

Why it’s better:

  • As the number of possible functions increases, the ‘not-so-good’ way will stretch your if statement out.
  • Made another possible operation? Just add it to the dictionary!
  • Allowed operations can be spotted at a glance and also possibly used elsewhere using ops.keys().

Bonus: Dynamic functions with custom arguments

What about if each of the possible functions has its own set of arguments?

The solution is dictionary unpacking. First, what does dictionary unpacking actually do? In the code below, line 6 and line 14 are doing the same thing, the only difference is that line 14 is using dictionary unpacking.

Now, we only need to mildly adjust our earlier example to allow for any custom arguments in our dynamic functions:

This approach can be useful in situations where you want your program to be highly configurable. For example, we can see that in the above code, the sequence of operations specified in the example_usage() function could instead have been defined in a configuration file such as JSON or YAML.

If you’re currently working on a machine learning project, you may want to read about my configuration strategies in my other article!:

Thanks for reading! When it comes to code readability, there should only be guidelines not hard rules. Trust your instincts or ask another developer how easy your code is to understand.

What do you think about these alternatives to if statements? Leave your comments below!

Photo by Javier Allegue Barros on Unsplash

--

--

Jonny Jackson
The Startup

PhD student in Artificial Intelligence and Medicine. Teacher of coding and machine learning to children and adults alike. -> jonny.jxn.co