Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Should curly braces appear on their own line?

Which would be a better way to format single line loops and conditions in C Sharp

if (i < j) 
{
    arr[i] = sum;
}
foreach (int k in arr)
{
    Console.WriteLine(k);
}

or

if (i < j) 
    arr[i] = sum;
foreach (int k in arr)
    Console.WriteLine(k);

or

if (i < j) arr[i] = sum;
foreach (int k in arr) Console.WriteLine(k);
share|improve this question

marked as duplicate by ChrisF Nov 29 '11 at 12:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
This is holy war territory and I'm surprised this question hasn't been closed already. The bottom line is that if you are joining an existing project, use whichever indent style is most used by the project. If you are starting your own project, either use whichever brace style you prefer, or use whichever is most common amongst the community your project is targeted at. –  Mark Booth Nov 29 '11 at 11:02
1  
@MarkBooth Although I agree that the question has holy war potential, judging by the answers I'd say we skipped it this time. If it spirals, it should get closed, but if you still feel it should get closed now you should flag it as "not constructive". –  Yannis Rizos Nov 29 '11 at 11:20
    
@MarkBooth - if the question doesn't get votes or flags we moderators don't always see everything. –  ChrisF Nov 29 '11 at 12:10
    
@ChrisF - Sorry, I didn't mean my comment as a criticism of moderation, just surprise that it hadn't been flagged more vigorously earlier. *8') –  Mark Booth Nov 29 '11 at 15:51

6 Answers 6

up vote 10 down vote accepted

Best practice is to choose one and keep to it in any single code base.

This also means that if the team you are working with use one style, you should stick to it.

Personally, I rather have the block delimiters, as in your first example, for a single line following an if or foreach as it makes things explicit. It is less surprising to some programmers.

So, my preference is:

if (i < j) 
{
    arr[i] = sum;
}

foreach (int k in arr)
{
    Console.WriteLine(k);
}
share|improve this answer
5  
+1 for "Best practice is to choose one and keep to it in any single code base"... Consistency is a major factor of readability. –  Yannis Rizos Nov 29 '11 at 10:33
    
+1 for consistency, too. Altough my personal preference differs from Oded's (see my answer below), I think this is by far the most important aspect. –  Silas Nov 29 '11 at 11:28

A number of other answers have said that you should follow the coding standard for your project. Consistency is the holy grail here. Mixing code styles that follow different rules is highly error prone

Having said that, Steve McConnell pointed out in his great book "Code Complete", using the construct

if ( i < j )
   arr[ i ] = sum ;

has the problem that it is prone to error during maintenance. Should you need to add some other statement to be performed when (i<j) is true then you have to add both the statement(s) and some surrounding braces. It is all too easy to write

if ( i < j )
   arr[ i ] = sum ;
   sum += j ;

when you really meant

if ( i < j )
   {
   arr[ i ]  = j ;
   sum += j ;
   }

Having the surrounding braces there for ALL conditional or loop constructs makes for greater consistency and makes it easier to scan over the code and check that all of the loops follow the same pattern.

share|improve this answer

I also think that consistency is the most important aspect, but I would like to add my personal favourite if there’s no existing code base that determinates a certain style. I would prefer the second version for those reasons:

  • IMO it is good coding style to keep blocks not only as flat as possible (cyclomatic complexity) but also as small as possible. This increases readability, since the block definition is always near to the line you're currently reading. If the braces are already there, it might overcome someone's inhibitions to add a second line to the block much faster.

  • Related to the first point: If you have a block with more than one expression in it, it's quite likely that the expressions inside it are at the same level of abstraction and this is another level than the block definition. So you might violate the Single Level of Abstraction Principle and should extract the block's content to an own method. If you always use curly braces around blocks you can't use them as an indicator for a possible abstraction level confusion.

  • I don't use the third (and fourth added by jk.) version since I think in fact when there is a logical block, so indentation should show this.

share|improve this answer
    
Ahem, who's the +1 for? You should leave a comment under the answer you liked, and remove it from your answer. –  Yannis Rizos Nov 29 '11 at 11:18
    
I already +1'ed the comment. Will edit my answer now. Thanks for the hint. –  Silas Nov 29 '11 at 11:20
    
By the way if the +1 was for my comment (as it's the only one with the word "consistency" in it), you should really upvote Oded's answer instead... We should reward answers not comments... –  Yannis Rizos Nov 29 '11 at 11:22
    
Well, as you can see, my personal preference differs from his. Unfortunately there's no partial upvote only for the first paragraph ;) But I'll do. –  Silas Nov 29 '11 at 11:26
1  
Cyclomatic complexity has nothing to do with flatness of blocks. As I understand it, flatness is only a measure of appearance and indentation style. Cyclomatic complexity is a measure of paths through a program or module. With most intendation styles, highly indented blocks are generally indicative of complexity (cyclomatic or not), but complex code does not need to have a great deal of indentation. –  Thomas Owens Nov 29 '11 at 11:39

you missed an option:

if (i <j) { arr[i] = sum;}
foreach (int k in arr) {Console.WriteLine(k);}

that said I think it depends what you are trying to emphasize in your code. looping or branching in a single line will tend to de-emphasize that bit of code, which is sometimes useful (e.g. not letting precondition checks take up to much space, code that only happens in debug mode etc.). For most production code however I'd prefer the full

if (i < j) 
{
    arr[i] = sum;
}
foreach (int k in arr)
{
    Console.WriteLine(k);
}
share|improve this answer

For me

if (i < j) arr[i] = sum;

foreach (int k in arr)
{
    Console.WriteLine(k);
}

A simple one line if statement I will put in the same line as there is more chance of an accidental mistake if a 2nd line is there. Someone may overlook and put a 3rd line expecting it to be part of the if statement. For loops I always include in a { } block for readability as the one liner like the above if statement just doesn't feel readable to me.

share|improve this answer

I would say that

if (i < j)
   arr[i] = sum;

is much more readable.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.