Re: Early return (was: Inspiration for a Tombstone.)

From: Date: Sun, 29 Jun 2008 21:57:25 +0000
Subject: Re: Early return (was: Inspiration for a Tombstone.)
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-276116@lists.php.net to get a copy of this message
Robert Cummings wrote:
I'm a big fan of multiple early returns whenever possible, but once the code becomes more complex holding back until the end if possible. I find multiple early returns are very easy to read and then the purpose of the code doesn't get lost within the convolution of the logic. Similarly, I use the exact same approach with a loop from which I want to break/continue. I find it much clearer to see at the very beginning of a function or loop block exactly what easily fails to meet the criteria and is discarded early. Additionally, this makes the code more linear in that far fewer nestings are required. From that I'd argue that the more linear the code is, the more readable and understandable it is.
I think you've captured very eloquently exactly how I feel about this! I've often thought that the if you disagree about early return then you automatically have to hate the whole continue/break thing too when used in loops - basically language *features*. I do concede (as you do) that there are times when the code benefits from nesting rather than "early return/break/continue/exit". Col

« previous php.general (#276116) next »