Jump to content
  • Advertisement
Sign in to follow this  
RyanZec

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!