DEV Community

loading...

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

Collapse
kant2002 profile image
Andrii Kurdiumov

You can look at how people talk when explaining something. I apply same technique and find it very useful. My motivation for this technique and some other comes from psychology and some linguistics. My coworkers whom I mentor think this is valuable technique, but maybe they do not know better. I cannot find studies regarding applicability of the technique in the real world even if I look for it deliberately. If you know what entity can do such kind of research I’m more then happy to start search again to prove or disprove statements of an article.

My anecdotal evidence is that code written in such way easier for novices to follow, since this kind of code can be easier to reading out loud and it would appear more natural.

Also there usually when express business logic there two kind of code intermixed, guards and shortcuts which require early exit to simplify logic and actual business logic where this rules less strictly applicable.

Why early exits and shortcut processing important? Try to remember how rare cases handled by business persons(can be engineers from other fields). Most people concentrate on core process and for other conditions usually throw simple shortcuts and suggestions which solve problem from their perspective. Such style of code just reflect how business people usually talk, just in slightly more formal way