DEV Community

Discussion on: Code Tip: Avoid unnecessary else statements

Collapse
 
exacs profile image
Carlos Saito

The statement it removes a level of indentation which makes the code more readable is too generic. Not always "less indentation" means "more readability". In some cases, the code is losing balance which could lead to less readability.

In your example without the "else" part, the code looks like the "if" condition being an "exceptional path" (early return) and the rest of the code the "normal path". Was it your intention?

Collapse
 
georgehanson profile image
George Hanson

It is not always the case, that is correct. But there are a number of cases where this issue can prop up and it can lead to more readability. Personally I prefer that if code doesn't need to be there then it shouldn't be there. It often allows you to read what the code is doing at a quicker pace.

In this specific example, the "if" is actually an exceptional path. Considering the context of Mononymous people.