DEV Community

loading...

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

Collapse
oleksiirybin profile image
oleksii-rybin

Usually, when I read the code, I don't read it like a book - all the lines one after another, instead, I read it following the logic path I'm currently interested in, from one logical block to another logical block, and it doesn't matter whether it's a "happy path" or not (it's up to me to decide, depending on the task at hand). So, reading through if/else statements don't create any issues for me, I just look at the condition and immediately proceed to "if" or "else" body. But what frustrates me - is complex "if" conditions and early returns. They both make it more difficult to only read through the code you need while skipping through the parts which are irrelevant to your current goal. First, I have to use the extra time to process the "inverted condition" and then go through the whole function making sure there are no early returns that will unexpectedly interrupt the flow I'm currently interested in, so as a result - I have to read much more lines (basically, all of them) and put more effort to get through the code.

Long story short: I'm not convinced. I still think using if/else statements and a single return at the end of a function is better for readability and stability of the code. Besides, if you're still lacking the context, I assume, any modern IDE lets you just click on the closing parentheses of any statement to quickly remind you what was the "if" statement at the beginning of it.