scope guards

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 5 09:27:43 PDT 2014


On 2014-08-04 12:09, Manu via Digitalmars-d wrote:

> I hate pointless brace and indentation spam, I feel it severely damages
> the readability of my code. And try/catch has a natural tendency to
> produce horrid nested structures.
> I would rather C-style error reporting via sentinel values and 'if()'
> than try/catch in practically every case imaginable, especially when
> try/catches start to nest themselves.

If you want to the if-statements to have the same semantics won't those 
be nested as well. BTW, why don't you just wrap the whole function in a 
try-block and add several catch blocks to it. No need to nest them, it 
will also be closer to how scope-statements behave.

> Okay, so why are scope guards such a key talking point in D if people
> loved try/catch?
> 'scope-tastic' code would be flat and sequential. I find flat and
> sequential code MUCH easier to reason about. Again, why would anyone
> care about 'scope' if they didn't feel this way at some level?

You need to nest the scope-statements to have the same semantics as 
nested try-catch. Or don't nest the try-catch, see above.

>         , and I'm strongly tempted to just
>         abandon my experiment and return to C-style error handling with
>         sentinel
>         values.
>
>
>     I can't see how that will improve anything. Seems like you have some
>     grudge against Java and don't won't your code to look like it.
>
>
> It will produce flat sequential code which is easier to follow.

You can do that with try-catch as well, see above.

> I agree, I had the same thought. But I felt it was better to integrate
> it into an existing (and popular) structure than to form a new one.
> I actually think there would be a valuable place for both though.

So you think a "catch" with an implicit "try" is a completely new feature?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list