Coding style with brackets ?

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

I always use the second way when I'm coding C/C++, I did program that first way when I coded in Java.
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.
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.
....[size="1"]Brent Gunning
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.
[TheUnbeliever]
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.)
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! ;)
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 );		totalVertices += Meshes.VertexCount;		totalIndices += Meshes.IndicesCount;	}	ComputeBounds();}
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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).

This topic is closed to new replies.

Advertisement