DEV Community

loading...

Discussion on: Write better code and be a better programmer by NEVER USING ELSE statements

Collapse
sctgcs profile image
Simon Tillson

I'm sorry, but this is wrong, and I'm not going to be kind about it.

I remember years ago when people started saying 'never use goto in C'.
The real reason for that was the provability of source code. With GOTOs littered all over, it's impossible to prove the output of a program. Without them, it is possible.
For safety-critical software, like flight controllers and medical devices, that's damned important.

Is this the reason people gave when shouting at the top of their lungs 'if anyone in this team uses GOTO, they're out the door!' ? No. It is not.

At the time, coders put up all sorts of other arguments about style, readability, maintainability and even occasionally, simple personal preference. These are all fair reasons, and there was never any justification for littering code with GOTOs, especially conditional ones! In many systems, it was even essential to use GOTO in some places. But I digress...

Your argument for avoiding ELSE is hollow. Source code style and layout is not hindered by ELSE at all. If used correctly, with decent coding style, it looks rather nice and obvious. Avoiding it often makes functions longer or hides their logic, when a clearly stated ELSE case is very straightforward. Of course we don't want 10 levels of indented IF..ELSE..IF.. but that's obvious, isn't it? No reason to ban the ELSE word.

In the real world, we've got more important things to concentrate on, and I don't think you will find the ELSE statement disappearing from any language anytime soon!

If anyone wants to try writing all their code without ELSE, go for it.
Just don't make a religion out of it, ok?