ojblass
ojblass

Reputation: 21620

Is it common to indent code at scope only braces?

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...

Upvotes: 1

Views: 1139

Answers (6)

barrowc
barrowc

Reputation: 10679

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
}

Upvotes: 1

bobince
bobince

Reputation: 536389

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?)

Upvotes: 1

Charlie Martin
Charlie Martin

Reputation: 112366

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.)

Upvotes: 5

Sebastian Good
Sebastian Good

Reputation: 6360

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.

Upvotes: 0

Arnold Spence
Arnold Spence

Reputation: 22272

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.

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 993105

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.

Upvotes: 3

Related Questions