Deep nesting vs early returns

Nick Sabalausky (Abscissa) SeeWebsiteToContactMe at semitwist.com
Thu Oct 4 19:23:37 UTC 2018


On 10/02/2018 02:14 PM, Andrei Alexandrescu wrote:
> Kate Gregory makes a good argument on something I've often commented in 
> code reviews: https://youtu.be/n0Ak6xtVXno?t=2682

I was in college during the height of the Java craze, so my instructors 
highly recommended the deep nesting approach. This was because return 
statements are control-flow, and control-flow isn't very 
object-orientedy, and is old-fasioned and in the same category as the 
dreaded goto and was therefore bad. So I switched to the 
nesting-instead-of-returning style because it was "The Right Way".

Eventually, I realized the deep nesting approach meant the main core of 
my functions were carrying around extra context with them, thus 
decreasing my ability to read and reason about my code. By contrast, 
with early-returns, all of that special-case junk is taken care of and 
moved out of the way (much like catch blocks), so the main core of the 
function no longer had to be burdened by it. So I switched back to 
early-returns and learned to love them.


More information about the Digitalmars-d mailing list