Friday, November 13, 2020

Good Coding Tip: NEVER USE ELSE Statement

This author has a really good point (to NEVER USE ELSE statements) that I've never spent much time meditating on.

"We can see that without the if statement, we can't determine what this [else] is meant to be doing. Why would it return an empty string? Is this an error, or the ‘normal' behaviour? This code instead relies on us remembering, and having read, the earlier context. This doesn't matter much when the statements are small, but if there's complicated logic within the
if { … }
block or we are scanning quickly, then the separation of context from code can hurt readability massively. It hurts even more when if/else statements are nested, or there are multiple of them in one function (which if statement is this else for?)."

Read the full article here:

https://dev.to/dglsparsons/write-better-code-and-be-a-better-programmer-by-never-using-else-statements-4dbl

No comments:

Post a Comment