Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have seen many different formats for if, ifelse, else statements(The curly braces {}). I would like to know if by placing these curly braces in a specific position, it would affect the code. If they do not, could you tell me which one looks more "organized?" Thanks in advance!


examples:

if(something) {
    do this;
}
//---------
if(something)
{
do this;
}
//---------
if(something)
{do this;}
Posted
Comments
PIEBALDconsult 3-Apr-15 19:06pm    
None of the above.
UWStudents 3-Apr-15 19:11pm    
Then which one(s) should I do?
PIEBALDconsult 3-Apr-15 19:22pm    
Whatever you like, or whatever your colleagues use, but be consistent.
Dave Kreskowiak 3-Apr-15 20:54pm    
It's all a matter of personal preference, or if in a company that does development work, it's usually spelled out in the coding standards documentation what you should be using.

Oh, and NO, it's has no effect on the execution of the code at all.

1 solution

My answer would be: use Visual Studio formatting settings, but customize them if you want.

In Visual Studio, it's the best to base all formatting on formatting rules defined in settings, in the framework of the sets of allowed options. In other words, if you cut all code from the file and paste it, it will be auto-formatting, and this will be the best style. Tune your Visual Studio settings, not text.

Frankly, there is more to it: comment and something else. I, for example, strictly follow my rules about there there are blank lines and where not, and where to put region markup, if any. Some of these aspects are not covered by formatting rules.

By the way, after some arguments, I once established the common rule for people using different formatting style. The rule is: be neat and let every team member use he/his own style! Why? Because anyone can reformat it by one's taste. It cannot make any harm, because, even if this code is committed to revision control code base, the other developer can reformat it in some different style; it takes no time. Additional rule is: the last one who touched the file is the master. And now, piece, no arguments.

But the code style itself (not formatting) is extremely important.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900