Code Opinion: Simple Return Statements


Given enough time software developers tend to develop a taste for the code styles and patterns they like, and the ones they do not. In other words, they all have opinions.

Today I have one that is a little different. Not tab vs. spaces ( tabs! ), but instead:

Return statements should be simple

That typically means that a return statement will be a single item returning a variable or constant. I have run into too many complex return statements that make it difficult to figure out how to pull them apart. And there are times where pulling them apart becomes a requirement, often from refactoring or language compatibility changes.

Here are a few examples. The simple function call might tempt you, but what about the oh so fun ( NOT ) nested ternary?

// DO NOT

return some_other_function();

// Do

$thing = some_other_function();
return $thing;

// --- //

// DO NOT

return $stuff ? $thing ? 'another' : 'maybe' : 'back again';

// Do

if ( $stuff ) {
	if ( $thing ) {
		return 'another';
	} else {
		return 'maybe';
	return $thing;
} else {
	return 'back again';

In my book nested ternaries should never be allowed, and a single standard ternary should be rare, but that is a post for another time.

Imagine this is code that was written 9 years ago, the person who wrote has left the company, and no one has touched it since. Things change, and now you need to make adjustments to the return 'maybe' condition.

In the nested ternary case do you first rewrite as if/else, test it to make sure you got everything correct, and then update the code? If so, the original author made more work for you. The other option is to layer it up with more parentheses to preserve order of operations and inject more code into the ternary? Each option carries extra work and potential pain. In situations like this it feels like the only winning move is not to play.

How about a few more.


// DO NOT

return $this || $that;

// Do

if ( $this || $that ) {
    return true;
}

return false;

// -- //

// DO NOT

return ($a && $b || $c && !$d) ? ($e || !$f && $g) : ($h && ($i || !$j) || $k);

// Do

if ( ( $a && $b ) || ( $c && !$d ) ) {
    if ( $e || ( !$f && $g ) ) {
        return true;
    } else {
        return false;
    }
} else {
    if ( $h && ( $i || !$j ) || $k ) {
        return true;
    } else {
        return false;
    }
}

Even the “cleaner” if/else style of that last one is pretty bad. It is at least sort of manageable compared to the complex single line version. Imagine needing to alter some logic around the $g value. More work and an increased risk of getting it wrong.

Someone out there is going to say that I have taken these examples too far. I wish that were true. While these are not copied from in the wild examples, I have run into these types of complex return statements with a series of multiple nested comparisons. More than once.

Do yourself a favor, only use simple return statements. Because the pain you save might be your own.