Follow

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

@zalasur I once worked at a business that had this rule in C++. It was the worst style guide advice ever, as the return variables were defined at the top, functions were 7 layers deep of if-else statements, and finally at the bottom is the one allowed return statement.

My argument against one-return is that it harms readability and actually increases the cognitive burden as nesting increases.

@cj yeah This seems to be the informal consensus, and what you're describing is exactly the type of situation I'm attempting to avoid.

@zalasur I really cannot understate how many dumb extra Boolean temporary values I've seen get created solely to skip sections of control flow because early return wasn't an option. This alone opens a new category of bugs. And how each "else" line in each nesting is a huge waste of vertical space since it is only meaningful to devs when you know the preceding "if", which gets hard when there's many layers of "else", and all the wasted horizontal space for the nested indentation.

@cj @zalasur All rules are made for a reason, and there is a time to break them.

The use of goto is another one: generally, it is considered to be bad b/c it’s easy to overuse. But when it increases the legibility and maintainability of the code base, it is Good to break the rule to not use goto.

To make the case more concrete, show multiple (3, if possible) different implementation methods. The code will (hopefully) speak for itself, but if not, you’ll have better luck than otherwise.

@cj @zalasur I like to have one entry and exit, too, though... so I tend to structure my code with multiple inline function/method calls, such that the logic can be represented in the source in a way that is easy for the programmer, but compiled into binary in a way that makes the most sense.

@SuperFloppies
If I need examples of why early returns are better than nested if statements, I can go back on our legacy code 😉
@cj

@cj @zalasur Whether it's:
if (condition)
return true;
else
return false;

Or:
return condition;

Or (what my teacher told me to do that caused me grief):
returnVariable = condition;
return returnVariable;

Should be a matter of preference. I think forcing an arbitrary code style is distracting.

@franknog
We have colleagues working in Europe who are very much opposed to enforcing coding standards they consider arbitrary. Our counterpoint is usually that we have to collaborate on the same code base between dozens of developers based in three continents, and a common standard is the only thing holding us back from the bad old days when a single function written by the different people was completely unreadable because of the lack of consistency.
@cj

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

Coding preference question 

Sign in to participate in the conversation
Mastodon for Tech Folks

This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either! We adhere to an adapted version of the TootCat Code of Conduct and have documented a list of blocked instances. Ash is the admin and is supported by Fuzzface, Brian!, and Daniel Glus as moderators. Hosting costs are largely covered by our generous supporters on Patreon – thanks for all the help!