Deep nesting vs early returns

Patrick Schluter Patrick.Schluter at bbox.fr
Fri Oct 5 20:00:03 UTC 2018


On Friday, 5 October 2018 at 16:02:49 UTC, Nick Treleaven wrote:
> On Thursday, 4 October 2018 at 06:43:02 UTC, Gopan wrote:
>>  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 think `switch (0) default:` is better, because it's not a 
> loop so the intent is clear - no continue statements somewhere 
> below (so it's also better than `do ... while (0);`). Also you 
> might forget the final break statement with `while` and get an 
> infinite loop.
>
`do ... while (0);` and also the loop above are abominations. 
They are for goto hypocrits, i.e. people who think that a goto 
when it is not named goto but works exactly like a goto is 
something better (the Voldemort goto).
The breaks above and in are GOTOS, no point in obfuscating them. 
Sorry if I'm a little bit inflammatory about these constructs, 
but I have to work with code written by a `do {} while(0)` 
champion and I can tell you, it's the horror when you have to 
make changes.
It was so bad that I finally used  
__attribute__((__cleanup__(x))) gcc extension to implement 
something resembling D's scope(exit) so that I'm sure that the 
code doesn't leak like it did before.




More information about the Digitalmars-d mailing list