On 31 July 2013 02:56, Karl Williamson <public@khwilliamson.com> 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. Karl++ I almost always use braces in C, and often convert braceless Perl core code to include the braces. I dont think leaving out the braces wins enough for the risk, and it makes it harder to switch back and forth between Perl and C. YvesThread Previous | Thread Next