C99: Suggestions for style(9)
Christoph Mallon
christoph.mallon at gmx.de
Fri May 1 20:32:27 UTC 2009
Zaphod Beeblebrox schrieb:
> On Fri, May 1, 2009 at 4:30 AM, Julian Elischer <julian at elischer.org> wrote:
>
>> As an old-fart I have found many cases where what I thought was
>> a silly style rule, turned out to save my work in some way.
>>
>> Christoph Mallon wrote:
>>
>>
>>
>>>> struct foo *fp;
>>>> struct bar *bp;
>>>>
>>>> fp = get_foo();
>>>> if (!fp) return;
>>>> bp = fp->bp;
>>>>
>>>> this can't easily be translated to the more natural:
>>>>
>>>> struct foo *fp = get_foo();
>>>> struct bar *bp = fp->bp;
>>>>
>> Well more natural for you, but not necessarily for everyone,
>> and NOT the same as what is there now, as you noticed.
>>
>>
>>
>>>> since really you'd want to write:
>>>>
>>>> struct foo *fp = get_foo();
>>>> if (!fp) return;
>>>> struct bar *bp = fp->bp;
>>>>
>>>> which isn't legal in 'C'. However, we have enough where this isn't
>>>>
>>> You're mistaken, this is perfectly legal C. See ISO/IEC 9899:1999 (E)
>>> §6.8.2:1. In short: you can mix statements and declarations.
>>>
> Sure, but it's still very bad: If I'm not mistaken, this would mean that
> "bp" would only be valid within the "if" clause --- which isn't very useful.
You are mistaken. Re-read the "if": It already contains a "return;" as
then-part. The declaration of "bp" has no relation to the "if".
In fact this is very good: "bp" can only be used after the "if", because
it is declared after it. Further, it most probably is only assigned a
value once, so declaration and the signle assignment are in the same
place, which aids readability and makes the code more concise.
Christoph
More information about the freebsd-hackers
mailing list