Re: braces needed?
From: Old Wolf (oldwolf_at_inspire.net.nz)
Date: 02/02/04
- Next message: David Rubin: "Re: be a programmer?"
- Previous message: Rob Williscroft: "Re: Are numeric constants in a namespace visible globally?"
- Maybe in reply to: lilburne: "Re: braces needed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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. :)
- Next message: David Rubin: "Re: be a programmer?"
- Previous message: Rob Williscroft: "Re: Are numeric constants in a namespace visible globally?"
- Maybe in reply to: lilburne: "Re: braces needed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|