Right, Wrong and Smart Way of Using ‘Else’

Navid Zarepak
The Startup
Published in
5 min readNov 1, 2020

If and Else statements are the first rules you learn when learning to code. They are so simple but yet a powerful part of any programming language. Their use-case is as simple as it gets; if something is true, then do this, otherwise, do something else.

If you’re a senior programmer/developer and dealt with juniors before, then chances are that you saw them use ‘else’ every time they get a chance. You can even tell how new and inexperienced someone is to programming by calculating the ratio of ‘else’ to ‘if’ usage in their code. You can even say that seniors avoid ‘else’ like how they avoid going out into the real world once in a while.

But jokes aside, there is actually a wrong and a right way - or rather a smart way - to use ‘else’. New programmers tend to use ‘else’ a lot because they have never been told not to and they don’t really think about it. And to be fair, the right way of using ‘else’ requires experience more than anything and that’s not something you can gain by watching 3 YouTube tutorials or reading 5 pages of documentation.

Fibonacci sequence in Python

Consider the code above. It’s a simple code that works perfectly. But why is an ‘else’ there? It could simply be written like this:

You might say that I’m being too picky, but it’s more important than you think. It is important to use the tools we have, simple or not, in a way that actually makes sense and results in value generation. If looking back at your code makes you wonder why something is written the way it’s written, then you must find a good reason for it. otherwise, you wasted your time at both the time of writing it and at the time of reading it while trying to make sense of what you did and why you did it the way you did. The extra ‘else’ in the first code will make you pay attention to it. You're telling your future self or someone that’s going to check your code to “look at this part” while there is no need. There is no need for extra attention to the ‘else’ part. One way to understand when you might need the extra attention is when you might need to change the ‘if’ statement and if you do that, then there is a good chance that you need a change or at least a check for the part coming up after the ‘if’ statement.

Now consider the code above as a very basic tax calculation function. You can remove the else just like the Fibonacci function but that ‘else’ is there for a reason. Imagine they changed the tax rules and the new minimum for 0% tax is 20000. Then you not only need a change in the first line of the code, but also the last line. So the ‘else’ is there to tell the person who’s going to edit the code that “You should also take a look at this part” meaning that they are closely related and a change to one might need some considerations on the other part.

But if you look more closely, that ‘else’ is pointing to another important note, Why the code is written in a way that you need changes in 2 places when only one thing is changed? So the code can be written like this:

Now, this code is much better. Every single line in this code is there for a reason. It’s simple to change and one change in one line won’t require any changes in other parts if only one condition is changed. This is as simple and clean as it gets.

In this case, the ‘else’ was alarming. an alarm that pointed to the fact that the code is written in a wrong way. In the first case with the Fibonacci sequence, the alarm was pointing to the fact that the ‘else’ is completely unnecessary. In the first tax example with ‘else’ included, the idea of putting an ‘else’ there to attract attention is a good idea if the code is written that way even if it’s not the best way to write that code but at least the ‘else’ helps there since a change in the first line requires a change to the ‘else’ part too.

And to add an example for the right way of using ‘else’:

In this case, ‘else’ helps us avoid using the cleanup() function two times (one in the ‘if’ in case we return there and avoid using ‘else’ and one after the ‘if’ statement in case that the condition1() wasn’t met) which is much more understandable and cleaner. This is probably one of the most common cases that using else actually leads to less code and the question “why there is an ‘else’ there?” will be answered right after the statement.

Conclusion:

In most parts, we don’t need to use ‘else’ and there is a good chance that you can write a better code to avoid it when you see one in your code. So since the need to use ‘else’ happens rarely, why not use it for another purpose? Using ‘else’ to attract attention when needed can help you and your colleagues to understand the code better when they’re making changes in the future. So it’s a good idea to think about how maintainable the code you’re writing will be in the future and how can you use the tools available to you to make your code more maintainable.

--

--