views:

317

answers:

6

For generated code I have an option to either indent or or not indent at braces which are only used to house variables at scope. Currently it does not indent at this level and I am wondering if I am going to imply a nested structure by indenting it? What is the common practice?

/* loop through the total number of letter a rules  */
for (a = 0; a < (number_a_rules - 1); a++) 
{
        /* loop through secondary position rules */           
        {
        int a2end = arulestableend[2];
        for (int a2 = arulestablestart[2]; a2 < a2end; a2++) 
        {
                  /* stuff */
        }
        }
} /* end for a 0 to numberarules -1 */

Versus

/* loop through the total number of letter a rules  */
for (a = 0; a < (number_a_rules - 1); a++) 
{
        /* loop through secondary position rules */
        {
                 int a2end = arulestableend[2];
                 for (int a2 = arulestablestart[2]; a2 < a2end; a2++) 
                 {
                     /* stuff */
                 }
        }
} /* end for a 0 to numberarules -1 */

Clarification: Using a debugger the extra indent implies another level of looping in hard to read code...

+3  A: 

I would definitely always indent code when using braces for variable scoping.

One technique I've used for generated C (and C++) code is to not worry at all about the formatting when generating the code. Just throw the code out there without regard for spacing, indenting, line endings, or anything. After generation, run it through GNU indent to make the code readable for debugging purposes. This is much easier than trying to create all the formatting yourself.

Greg Hewgill
I don't have the gcc compiler on some of these platforms :(
ojblass
Alternatively, there's Artistic Style http://astyle.sourceforge.net/ which also handles C++ and Java.
Dana the Sane
I don't even have gmake to build gnu tools :(
ojblass
With some digging, you might be able to find a more bare bones formatter. AStyle has fairly broad OS support, so you might be able to build it.
Dana the Sane
Do any beautifiers have scope only indent option... tidy appears to leave the scope only braces alone...
ojblass
+5  A: 

To my eyes, it's more misleading without indenting. I look at the unindented version and think "what's wrong here"?

Is it really necessary to have the redundant braces?

Update

I want to answer ojblass's comment and it'll take more space than a comment I think.

I thought you cannot declare variables in c without the braces... at least on some miserable compilers.

You're not as free in C as in C++; what you have to do is put any new declarations in before any executable code. So, in this fragment

for (a = 0; a < (number_a_rules - 1); a++) 
{
        /* loop through secondary position rules */
        {
                 int a2end = arulestableend[2];
                 for (int a2 = arulestablestart[2]; a2 < a2end; a2++)

you could write it

for (a = 0; a < (number_a_rules - 1); a++) 
{
     int a2end = arulestableend[2];
     for (int a2 = arulestablestart[2]; a2 < a2end; a2++)

and it would work perfectly well. On the other hand, the declaration in the for loop isn't straight C either.

Now, what is possible, and wouldn't show in the example, is if questioner is using the braces to limit scopes so he has a simpler namespace management; that is, so you could have

{
     int a2 = some_good_thing();
     // do stuff
}
{
     int a2 = something_else();  // Now a different a2, 
                                 // and it's the compiler's problem
     // do other stuff
}

(NB: Sorry, OJ, didn't realize you were the questioner. My eyes hurt, please take the necessary corrections to grammar as read.)

Charlie Martin
I thought you cannot declare variables in c without the braces... at least on some miserable compilers.
ojblass
You can only declare variables at the beginning of a block (before any code). The body of a loop is a block, though, so you don't need the extra set of braces shown in your example.
Michael Carman
@Lucas, thank you.
Charlie Martin
I am currently learning the code generator... I think they use the scoping to avoid collisions between their modules and the users code... its complete crap +1
ojblass
Selected for addressing the issue of which would you prefer in the general case as well as outlining the various issues
ojblass
@ojblass: Only C89 / ANSI C has that restriction. All C99 conforming compilers don't have that same limitation. GCC allows it when compiling with -std=c99, but Microsoft's C compiler doesn't (I don't think).
dreamlax
NO kidding? I'm always working in the ANSI C world, I haven't kept up on the extensions.
Charlie Martin
dreamlax "doesn't (I don't think)" arrrgggggggggggg reflexive negative voice... I think I get it I can test it and see what it does with a large number of nested loops in MS
ojblass
+2  A: 

I can't think of a good reason not to indent. The braces indicate a block which has significance whether or not it is for a loop or other conditional statement. In the end, it comes down to what you think is more readable so go with your gut but be consistent.

Arnold Spence
Using a debugger the nest implies another level of looping in hard to read code... I want them to see that its only a scope level brace.
ojblass
I don't agree that the braces would imply another level of looping but at a glance I would know that it implied something that I should pay attention to and in this case, that something is 'int a2end' has intentional limited scope and I'd want to find out why. Without the indenting I might not catch that immediately. On the other hand, double braces at the same level just yell wtf? :)
Arnold Spence
Indenting the code inside braces doesn't imply a loop. It makes it clear that it's a block. As capar said, it highlights that the scope of 'a2end' is limited. This is important because it's immediately clear that a2end is independent of everything else (even another variable with the same name used elsewhere).
Michael Carman
A: 

Spit out autogenerated code for its true customer: the compiler. A few dollops of newlines will usually help compilers, which are still optimized for lots of short lines, but other than that, who cares? There are plenty of pretty printers out there that can make it look nice when you want to check it out by hand for correctness.

Sebastian Good
This is the final preprocessing step and the compiled code will likely be debugged by a human :(
ojblass
And there is also the developer of the code generator to consider - they need to be able to read the generated code to be sure that it is correct. If the generated code is formatted for readability, it makes maintenance of the code generator easier.
Jonathan Leffler
sure enough, but can't you apply a pretty printer after all the code is generated, perhaps in code? code generation is hard enough without combining it with pretty printing.
Sebastian Good
+1  A: 

If you intend people to read it, indent. Braces don't work without indentation. I had to stare at the top example for a while to convince myself it wasn't broken.

Indentation does not imply a loop; there are many other constructs that use it. I might have been confused if the braces had been nested without any other content:

for (...)
{
    {
        /* huh? */
    }
}

But the use of a comment immediately followed by a brace in the second example made it quickly obvious what was going on.

Are you worried about using too many indents and hitting the ‘80 character limit’? If so, increase the limit and/or decrease the indent size (8 spaces is a bit big really).

(If you don't intend it to be read by humans, then who cares?)

bobince
A: 

How about using the comma operator in the first part of the for statement?

for (int a2 = arulestablestart[2], int a2end = arulestableend[2]; a2 < a2end; a2++) 
{
    // do something
}
barrowc