Public Group

# Coding style with brackets ?

This topic is 4065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just want to know if it is just me or something. Most poeple I have know that program seem to like coding thier brackets like this:
if(1!=2){
//do some code
do{
x++;
for(i=1;i<x;i++){
//do something
if(x=i){
//do something
}else{
//do something
}
}
}
while(x<10)
}

and i code like:
if(1 != 2)
{
//do some code
do
{
x++;
for(i=1; i<x; i++)
{
//do something
if(x = i)
{
//do something
}
else
{
//do something
}
}
}
while(x < 10)
}

When I ask most of these poeple that if may way looks cleaner, they so no, what do you think?

##### Share on other sites
I like doing it your way, because then it seems as if the "block" is preserved. With the bracket next to the call (for{ ) it seems to blend in too much.

As long as you are working by yourself it doesn't really matter, as the code does the same thing and you are the one that needs to work with it the quickest.

##### Share on other sites
It's completely a matter of personal preference. I prefer braces at the end of the lines, but I've seen plenty of code with braces on their own lines. I think it spreads blocks out too much, but then that's just me.

Just use whichever you prefer to read, unless you are on a team and it really annoys everyone else :)

##### Share on other sites
I prefer the second. To me it's much easier to line up parenthesis and the extra empty lines (not empty lines, but lines with just { or }) help break up logical chunks of code.

##### Share on other sites
Any good IDE should be able to convert styles.

I personally prefer the former (K&R) style as it better maintains the indentation, whereas I feel the latter adds a lot of useless visual noise. It's a case of personal preference, though, and I don't really mind either as Visual Studio will modify it as I require it. [wink]

##### Share on other sites
I use your second style, although I indent all braces except the function-binding ones:

void f(){    for(int i=0;i<10;++i)        {        std::cout << i << std::endl;        while(false)            {            std::cout << "hello\n";            }        }}

I find you get into these habits early on in C or C++ programming and it is hard to change.

##### Share on other sites
Quote:
 Original post by EasilyConfusedI find you get into these habits early on in C or C++ programming and it is hard to change.

I've changed styles about 3 times over the course of writing my engine, and now it really bugs me when I go through some of the older code and it's using a different bracing method. :(

These days I use the second method.

##### Share on other sites
How to I get code to appear in the white textbox on the forums BTW? I thought it was <code></code but not and it seems
just keeps the formatting

##### Share on other sites
Quote:
 Original post by RyanZecHow to I get code to appear in the white textbox on the forums BTW? I thought it was

Try source instead of code. It's in the faq link in the upper right hand side of the page.

##### Share on other sites
I use and have always used the second method. IMO the first is just sloppy layout.

• 16
• 11
• 11
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999749
×