LLVM Coding Standards

Nick Sabalausky a at a.a
Mon Apr 11 18:09:47 PDT 2011


"Spacen Jasset" <spacenjasset at yahoo.co.uk> wrote in message 
news:invvmu$7ha$1 at digitalmars.com...
>
> That seem all fairly sensible. It also reminds me of open source projects 
> written in C, where GOTO is used, like so:
>
>
> HANDLE handle1 = open(...);
>
> ...
> if (out_of_memory)
> goto cleanup;
>
> if (invalid_format)
> goto cleanup;
> ...
>
> cleanup:
>    if (handle1)
>       close(handle1);
>    if (handle2)
>     close(handle2);
>
>
> This code uses the dreaded goto statement, but I belive you can see that 
> the author is trying to make the code more readable, or at least get rid 
> of the nested indents/multiple cleanup problem you inevitably come across 
> at some points in C code. It does tend to be more readable than the 
> alternative, too.
>

I'm a big anti-goto guy, but I think what bugs me about that code more than 
the goto is that its particular style of error handling gives me flashbacks 
of VB6. Bring on the try/catch/finally and scope guards!

Of course, I do agree, without try/catch/finally or scope guards that code 
really isn't too bad of an alternative.

> I think that people like to follow rules, that is as soon as they have 
> internalised them and made them their own. What this means is that they 
> often then follow them to a fault, and you get deeply nested, but 
> "structured" code, where instead you would be better of with more 
> logically linear code as in the case of the early exit. Coding standards 
> should probably just say: try and write readable code. Everyone knows what 
> readable code looks like. It just not always quick or easy to make it that 
> way.
>

I think that's a good analysis. And particularly true of programmers. We're 
trained to think in terms of defining structure and following it. We're more 
like Hermes from Futurama than most of us might think. Minus the dreadlocks, 
of course (which incidentally look terrible in real life, unlike on TV. In 
real life they just look like someone forgot to wash their hair for twenty 
years.)

>
> While I am on the subject, I've *always* thought major languages have poor 
> loop constructs:
>
>
[..snip..]
>
> Instead you could just have:
>
> loop
> {
>  ...
>  if (condition) exit;
>  ...
> }
>
> instead of WHILE and DO. Whereby you *must* have an exit condition.
>

Yea, once in a while I do come across a need to have the exit condition in 
the middle of the loop body. It doesn't happen to me often, but it is kind 
of annoying when it does crop up. I feel like I have to contort the task to 
fit the tool. Fortunately, though, like I said, I don't come across such 
cases often. YMMV, of course.

> I'll put the coffee down now...
>

[OT]: My local World Market has a "Vanilla Macadamia Kona Coffee". A little 
pricey as far as coffees go, but I think it's becoming my new god. :)  If 
only I could find it in whole-bean decaf form... (I don't like the way 
caffeine affects me, and pre-ground doesn't store well unless you have an 
air-tight container on hand.)  It's kind of weird if you think about it how 
much our society has gone nuts over what's essentially "bean juice" - 
doesn't sound so appetizing when you describe it that way :) Um, ok, yea I 
guess I *really* need decaf, don't I? ;)





More information about the Digitalmars-d mailing list