Sign in to follow this  
RyanZec

Coding style with brackets ?

Recommended Posts

RyanZec    124
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
UMichGOBLUE    127
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
Ezzaral    122
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
tstrimp    1798
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
benryves    1999
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
Aardvajk    13207
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
PlayfulPuppy    419
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
RyanZec    124
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
tstrimp    1798
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
programering    105
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


You write:
<source> (begin tag)
Your content goes here.
</source> (end tag)

Edit: but with the square brackets, what's the ampersands for that?
Edit2: square brackets 'S', thanks Oluseyi. Edit3: But that is wrong.

[Edited by - programering on May 4, 2007 8:56:05 AM]

Share this post


Link to post
Share on other sites
Crehl    134
I use the second method, I don't like the first. I don't like how blocks seem to overlap onto unneeded lines, such as the '}else{' in the example.

Share this post


Link to post
Share on other sites
Telastyn    3777
As benryves says, good IDEs will do this sort of layout for you and convert between. One of the best features I've seen added to IDEs along with collapse to definition and improved debug interfaces. Helps make something like this that is mostly preference into an actual preference since team members can easily opt between.

That said, I prefer the first greatly (albeit with the added spacing in #2's conditionals). To me, the (begin block) represented by the { is a logical part of the function/if/for/while statement so belongs with it. The indentation provides sufficient visual info about what looping depth you're in. The second method makes me have to shift my sight back to the left to check on the brace rather than simply continue down like I can with style #1.

Share this post


Link to post
Share on other sites
gunning    749
It's all personal taste. I've worked with some very intelligent programmers before and have seen many different styles, all of which had logical reasons to back them up.

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.

That could be, however, I find if you make an effort to change up your coding habits (language, styles, whatever) every once in a while it keeps you from getting stuck in old ways when a newer or better idea comes around.

Share this post


Link to post
Share on other sites
TheUnbeliever    963
I prefer the following (code is fluff):

void foo(int i)
{
if (false)
++i;

bool running;
do
{
running = checkStatus();
std::cout << "It's running. This previous statement is " << running << std::endl;
} while (running);

while (++i < 100)
{
for (int j = 0; j < 1000; ++j)
std::cout << "i: " << i << " j: " << j << "\n";
}

int multipleValue = 5;
if (multipleValue < 3)
{
std::cout << "Lesser" << std::endl;
}
else if (multipleValue == 3)
{
std::cout << "Equal" << std::endl;
}
else
{
std::cout << "Greater" << std::endl;
}
}



Curly brackets take a new line, else takes a new line, lines involving brackets that are not a) being used in relation to a function or b) being used to force order-of-evaluation have a space between the brackets (see ifs, whiles for examples), plenty whitespace between logical 'chunks' of code. Indentation is four spaces if I'm typing code and can't use tab easily (e.g. typing into these posts), or a tab in an IDE (with the tab spacing set to 4 spaces) because then others can change the indentation to suit their style.

Summary: pretty similar to your style. Its down to personal preference, in the end, and this isn't how I started out. Certainly, I can't understand some of the people that manage to write code (outside an IDE) that isn't indented.

Share this post


Link to post
Share on other sites
Zahlman    1682
De gustibus non disputandum est.

I prefer the first style aesthetically. There's very little I can say to justify that, but I will point out that it tends to make undesired semicolons after loop constructs more obvious:


for (int i = 0; i < 10; ++i);{ // <-- oops, code in body only executes once
// since we actually have an empty for loop, followed by code in 'non-purposed'
// braces which simply create a new scope.
doSomething();
}


The reason I say it's "more obvious" with this bracing style is that ";{" just plain doesn't look right. :) (In the cases where I *do* want "non-purposed" braces, they do get their own line. That's not an exception; it's simply a reflection of the fact that I'm writing a block where the text introducting the block is zero-length.)

Share this post


Link to post
Share on other sites
EXGE    122
Hey RyanZec,
I prefer the second style.
In my oppinion the code looks way more readable and things ({}) that belong together are on the same level that way.

I think it all comes down to a matter of taste!
Although I think it is highly important to have a readable and constant way of setting brackets and writing code.

Keep it up the second style! ;)

Share this post


Link to post
Share on other sites
Promit    13246
Tabs not spaces, 4-wide tab stops.
protected void CommonInit( BinaryReader reader, out int totalVertices, out int totalIndices )
{
//read off the header
FileHeader header;
header.FourCC = reader.ReadBytes( 4 );
header.Version = reader.ReadUInt32();
header.MeshCount = reader.ReadUInt32();

if( !CheckFourCC( header.FourCC ) )
throw new Exception();
if( header.Version != E61Version )
throw new Exception();

Meshes = new SubMesh[header.MeshCount];

//used to tally a few totals
totalVertices = 0;
totalIndices = 0;

//read off each of the submeshes
for( uint i = 0; i < header.MeshCount; ++i )
{
ReadSubMesh( reader, ref Meshes[i] );

totalVertices += Meshes[i].VertexCount;
totalIndices += Meshes[i].IndicesCount;
}

ComputeBounds();
}

Share this post


Link to post
Share on other sites
rollo    366
Personally I use the second style, but I have switched a couple of times over the years, so it doesnt really matter much.

I do have a problem with using tabs instead of spaces. Logically tabs are superior - each programmer can select how much indentation they want. In practice though, sooner or later one on the team will make a mistake, use an unconfigured editor etc and you end up with a mix that looks screwed up to everyone else. This has happened on every project I'v been on using tabs (only university projects though... all professional projects I'v done so far have been spaces only... so it could just have been lazy students).

Share this post


Link to post
Share on other sites
tstrimp    1798
Quote:
Original post by rollo
Personally I use the second style, but I have switched a couple of times over the years, so it doesnt really matter much.

I do have a problem with using tabs instead of spaces. Logically tabs are superior - each programmer can select how much indentation they want. In practice though, sooner or later one on the team will make a mistake, use an unconfigured editor etc and you end up with a mix that looks screwed up to everyone else. This has happened on every project I'v been on using tabs (only university projects though... all professional projects I'v done so far have been spaces only... so it could just have been lazy students).


That's one of the things I love about Visual Studio. ctrl-a, ctrl-x, ctrl-v and it's all laid out just the way I like it. There is probably a command somewhere to do the same thing, I just don't know where it is.

Share this post


Link to post
Share on other sites
demonkoryu    980
Quote:
Original post by tstrimp
That's one of the things I love about Visual Studio. ctrl-a, ctrl-x, ctrl-v and it's all laid out just the way I like it. There is probably a command somewhere to do the same thing, I just don't know where it is.


And here is how you do it with VIM: ggVG=
Sorry, had to. [smile]

Share this post


Link to post
Share on other sites
Nathan Baum    1027

// Function declarations always have whitespace between the name and the parameters.
int some_function (int x, char *p)
{
// Whitespace after control statements so it doesn't look like a function invocation.
// Whitespace between binary operators.
if (1 != 2) {
// I never use do.
do {
// No whitespace for unary operators.
// Prefer preincrement.
++x;
// Declare "for" variable inside the "for" where possible.
for (int i = 1; i < x; ++i) {
do_something();
// I don't use an assignment expression as a conditional expression.
if (x = i) {
do_something();
} else {
do_something();
}
}
} while (x < 10);
}
}


Quote:
Original post by rollo
Logically tabs are superior

Even if the tabs are set at the right width, they mess with my head. They make cursor movement unpredictable.

Tabs should never been used for indenting code. Tabs are for tabulation. Anyone using tabs in a medium which doesn't support setting tab stops should be shot. To death. Twice.

Share this post


Link to post
Share on other sites
Promit    13246
Quote:
Original post by Konfusius
Quote:
Original post by tstrimp
That's one of the things I love about Visual Studio. ctrl-a, ctrl-x, ctrl-v and it's all laid out just the way I like it. There is probably a command somewhere to do the same thing, I just don't know where it is.


And here is how you do it with VIM: ggVG=
Sorry, had to. [smile]
Ctrl-K, Ctrl-F to format selection. Sort of emacs-ish. There's a couple other neat commands in that vein too...C-k, C-d to format entire document, C-k, C-c to comment, or C-k C-u to uncomment.

Share this post


Link to post
Share on other sites
tstrimp    1798
Quote:
Original post by Promit
C-k, C-d to format entire document, C-k, C-c to comment, or C-k C-u to uncomment.


I just select and press / to comment/uncomment blocks. I love Visual Assist.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this