Deep nesting vs early returns

Gopan gggopan at gmail.com
Thu Oct 4 06:43:02 UTC 2018


On Tuesday, 2 October 2018 at 18:14:55 UTC, Andrei Alexandrescu 
wrote:
> Kate Gregory makes a good argument on something I've often 
> commented in code reviews: https://youtu.be/n0Ak6xtVXno?t=2682

Thank you Andrei for mentioning this.  I always had this question 
of which one to choose - early return or nesting.

But the idea of 'early return' leads to multiple return 
statements too (for separate return conditions), right?

Certain people recommend that there be only one return statement 
(usually at the end) from a function.  The said advantage is 
that, in a maintenance code, if you later want to do something 
before returning, you can add it just above the return statement. 
  I have seen people enclosing the function logic inside a 
while(1) merely to stick on to single return at the end.

while(1)
{
	...
	break; //otherwise return would come here.
	...
	break;
}

return ...;


I (no expert) still don't have clear idea about the multiple 
return statements scattered inside a function.  So, I return 
where ever I want to return.  And, I use your C++ ScopeGuard to 
do that extra thing that is supposed to be done before returning 
( from your article 
http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758 )

Any advices?




More information about the Digitalmars-d mailing list