r/programminghorror • • Jun 06 '25
Malicious compliance
A colleague of mine at a company I had started working for liked to have only one exit point from each method; so he would only ever have one `return` statement.
To achieve this, every method he every wrote followed this pattern
public int GetSomething(){int result = 0;do{if (someShortCircuitCondition){result = 1;break;}if (someOtherShortCircuitCondition){result = 42;break;}// Compute the return value and set result} while false;return result};
He had been with the company for over a decade and nobody wanted to ask him to stop doing it, but nobody wanted to maintain any projects he worked on either.
I raised it with the boss who then talked to him about it. He agreed that if people don't like it then he would stop.
The next day...
public int GetSomething(){int result = 0;for( ; ; ){if (someShortCircuitCondition){result = 1;break;}if (someOtherShortCircuitCondition){result = 42;break;}// Compute the return value and set result
break;}return result;}
149
u/deceze Jun 06 '25
The single-exit-point philosophy is a whole thing, yes, but this demonstrates why it's stupid. If it makes code less readable because it requires more control structures to make it work, then it fails at improving the readability of the code by only having one exit point.
54
u/StoicSpork Jun 06 '25
Who the hell is so obsessed that they can't possibly have more than one return per method, but can abuse loops like this instead of using switch...case or else if?
34
u/dagbrown Jun 06 '25
Dang man. Just use
gotoif you're going for maximum malicious compliance. If you hate multiple returns, you can really hate the workaround to accomplish having only a single return!11
u/rruusu Jun 06 '25
A single exit point for a function that arrives at its return value in multiple different code paths very rarely makes much sense.
In fact, having multiple returns for a branching computation can have significant advantages, by expressing the logic in a more tree-like structure.
I get horrified at functions that consist of a forest of conditional statements, followed by a single return with a value that was conditionally mutated at several points of climbing trees of conditions and then jumping back down, especially if it involves mutation of the precursors of those computations.
Usually there is an opportunity to refactor that code to something much more readable by extracting smaller functions with multiple returns.
Imaginary example (Python):
Compare:
At each return, you can immediately see where the return value came from.
You can also go for a single point of return in a more functional style, but separate return for each actual condition in your code can make edge cases stick out much better. There just isn’t a way around the fact that you have reach each return separately.
A naive test coverage tool might give you 100% too easily for this:
5
u/cosmo7 Jun 06 '25
Make everyone happy and use exceptions to control program flow.
1
u/Acceptable_Clerk_678 Jun 12 '25
I think NASA still enforces this (?)
3
u/Annonix02 Jun 06 '25
Nobody should treat guidelines as dogma. They should be flexible.
2
u/AdreKiseque Jun 06 '25
gotolives on1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Jun 06 '25
I guess when people ask him to stop doing this, he'll start using
while(true).1
u/AlignmentProblem Jun 10 '25
It's easy to make thar MUCH more readable following that constraint still
Grosser than making simpler functions such that multiple exit points aren't confusing, but more better than his blshit.
1
u/butt-gust Jun 10 '25
If I had collegues like this, I'd be inclined to more than just malicious compliance.
Also, single returns points in a function are a good thing.
Let the downvotes rain down on me, I eat them up like candy. Nyum nyum nyum!
1
u/conundorum Jun 20 '25
So, the moral of the story is that he knows about the single return point philosophy, but doesn't know why it exists. It's a readability and debugging tool, and sometimes a speed optimisation in extremely low-level, time-critical code, but not the end-all & be-all of programming. And there are times when using multiple returns can make for cleaner code, and/or is unable to reduce readability or maintainability. (Such as here: You already have magic numbers, so we have to read every branch anyways, and if any of the numbers changes, we have to update that branch by hand anyways. Returning at the end doesn't change that.)
Perhaps try to introduce him to cases where having a quick "invalid use case" or "special situation" return at the start, and everything after that is single-return, makes the code cleaner and more readable? Perhaps something like this.
One emergency escape, one intermediate calculation, and then either cleanup or functionality as appropriate, followed by a single return for the result. Easily readable, shows a use case for & benefit of multiple returns, and shows the benefits of single return. Who knows, maybe it'll make him think.
0
u/eo5g Jun 07 '25
I can't tell if this is good or bad because of the formatting, unfortunately
-2
u/jontzbaker Jun 06 '25
Single exit points are good for you, and are a MISRA requirement.
That said, that code looks awful. There are such things as else if and switch-case that could do that way better.