Re: braces needed?

From: Old Wolf (oldwolf_at_inspire.net.nz)
Date: 02/02/04


Date: 2 Feb 2004 13:01:12 -0800


> The main problem with not including braces that I come across is poorly
> written macros particularly debug ones or ones requiring a local variable
>
> For example:
> #define DEBUG(s) if( debug ) puts(s)
>
> This works ok for
>
> if( x > 3 ) DEBUG("too big");
>
> But fails miserably the moment you add an else/else if clause

This bug should have been caught in two other places:
  - you should not write a macro that can be misused like that
  - the person who wrote "if (blah) MACRO()" should have been more awake

I would not want to impose braces on all my code in order to avoid
that situation.

In my years of braceless coding I have only had a couple of
unexpected if..else behaviours, and one situation that took an hour
to find. IMHO this is not enough reason to start crippling
my code with unnecessary braces. The gist of it was:

    if (blah)
       some_large_expression();
    else
       return some_other_large_expression(),

// debug();
    printf("blah was true\n");

and the function would print "blah was true" when blah was false,
and return the wrong thing, and I was even more annoyed by the fact
that if I put in the debug line then the behaviour would all change
again. :)



Relevant Pages

  • Re: Question about #define
    ... > #ifdef DEBUG ... Get and use a C99 compiler. ... Get and use gcc and the gnu variadic macro extensions. ...
    (comp.lang.c)
  • Re: Inserting Command Button (Form Control)
    ... wrong - but I have 2 braces with gray lettering between them saying ... MacroButton Test1 DblClick here to run macro - and when I double click ... achievement, and who at the worst, if he fails, at least fails while ...
    (microsoft.public.word.vba.general)
  • Re: Style isnt always religious
    ... >> parenthesis following the function parameters. ... >> braces are placed in K&R, I suspect they would have put braces ... > many programmers, and yes, it was fully customizable, you could ... > modifying the 'routines' macro to support your particular style. ...
    (comp.lang.c)
  • Re: What policy for BUG_ON()?
    ... > our own debug printk() that shows the mess. ... Then the BUG_ONmacro would ... macro would be disabled for general users, ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Converting from 6.0 to VS2005
    ... It's hidden in the macro CheckParameters_* - something to do with the Assert ... I've no idea how DEBUG came to NOT be defined in VC6.0 but I've just wrapped ... or it was a modified Debug project that had ... DebugTraceFn can be used. ...
    (microsoft.public.win32.programmer.messaging)

Loading