scope guards

Manu via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 4 03:09:35 PDT 2014


On 4 August 2014 17:21, Jacob Carlborg via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> On Monday, 4 August 2014 at 04:09:07 UTC, Manu via Digitalmars-d wrote:
>
>  Almost all exceptions I throw are in relation to bad input data, and they
>> are to be caught at a slightly higher level of input processing. My code
>> has become try/catch-tastic, and I really don't like looking at it.
>>
>
> Why does it matter it has a lot of try/catch-blocks.


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.

 It rather sickens me and reminds me of Java
>>
>
> So just because your code looks like Java you want to come up with a new
> feature that is exactly like "catch", but with a new name. Instead your
> code will become scope-tastic (whatever that means) and it doesn't look
> like Java because it use a different keyword.


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?

 , 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.

 So... why not make scope guards more useful? It wouldn't be hard.
>> scope(failure, MyException e) is completely non-destructive, and adds
>> significant power to the concept.
>>
>
> I think it's better to support catch-blocks without a try-block. BTW, if I
> recall correctly, I have already proposed this and it was turned down.


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140804/4b9ee71c/attachment.html>


More information about the Digitalmars-d mailing list