Deep nesting vs early returns
Nick Treleaven
nick at geany.org
Fri Oct 5 16:02:49 UTC 2018
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.
This is an occasionally useful general pattern (I use early
returns).
More information about the Digitalmars-d
mailing list