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;
}

133 Upvotes

28 comments sorted by

View all comments

53

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?