Coding style with brackets ?

Started by
54 comments, last by rollo 16 years, 11 months ago
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?
Advertisement
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.
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 :)
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.
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]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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.
Quote:Original post by EasilyConfused
I 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.
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
Quote:Original post by RyanZec
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


Try source instead of code. It's in the faq link in the upper right hand side of the page.
I use and have always used the second method. IMO the first is just sloppy layout.

This topic is closed to new replies.

Advertisement