develooper Front page | perl.perl5.porters | Postings from July 2013

Re: Using braces on 'if'; Was: Re: Asan help request

Thread Previous | Thread Next
From:
Uri Guttman
Date:
July 31, 2013 03:35
Subject:
Re: Using braces on 'if'; Was: Re: Asan help request
Message ID:
51F885FF.2090403@stemsystems.com
On 07/30/2013 08:56 PM, Karl Williamson wrote:
> On 07/25/2013 02:19 AM, Nicholas Clark wrote:
>> I don't think that using a comma operator in void context just to avoid
>> needing braces is really worth it. It doesn't feel like idiomatic C.
>
> I agree, and I'd like to put in a plug for using braces.
>
> The coding standards where I $worked called for always using braces when
> the 'then' clause was not on the same line as its 'if'.  The reason was
> bitter experience with maintenance safety.  It's too easy for someone to
> later come in and fail to notice the absence of braces, and turn this:
>
>      if (a)
>         b;
>
> into
>      if (a)
>             c;
>             b;
>
> Thus causing b to be executed unconditionally, contrary to the original
> code.  If you're lucky this will generate a compiler warning or error
> (as happened to me on Saturday with the Perl core).  If you're somewhat
> less lucky, the regression tests will have adequate coverage and you'll
> find this problem almost immediately.  But if you're unlucky, this
> regression won't be found until an angry customer and boss are breathing
> down your neck.  Under such pressure (or even without), code reading may
> very easily fail to see what is the problem.  Our eyes look at the
> indentation, and not the lack of braces.  This happened enough that we
> banned the practice, and everybody got into the habit of always saying
>
>      if (a) {
>         b;
>      }
>
> That habit has saved me from creating many bugs over the years.

in my 20 years of heavy c (but rusty now thanks to perl) i always used 
braces on if. i always espoused that for safety, clarity, ease of 
changes, etc.  it also makes it easier to skip code sections with emacs 
by skipping paired chars. of course there was some lout who claimed if 
it is legal c it is ok code.

braces += Inf ;

thanx,

uri

-- 
Uri Guttman - The Perl Hunter
The Best Perl Jobs, The Best Perl Hackers
http://PerlHunter.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About