PDA

View Full Version : Formatting Standards



subdene
11-21-2002, 02:31 PM
Is it me or am I just arrogant? One of the most annoying things I come across is the way in which people code differently, i.e. formatting. It is really iterating when you see code indented by about 40 tab spaces between curly braces, and your scrolling across the screen for about a mile. I personally think there should be a formatting standard, and not just a guideline. Anybody agree on this?

Magos
11-21-2002, 03:21 PM
Originally posted by subdene
It is really iterating when you see code indented by about 40 tab spaces between curly braces, :D

It's not that easy to do as it may sound.

And if there will be a global style, it better not be the kernel. I hate it :). It's so damn ugly when the preceeding bracket is placed last on the previous line...

...perhaps I'm a little pedantic...

subdene
11-21-2002, 03:36 PM
well i just wish people could follow guidelines. That way reading and maintaining code could would be that much easier.

RoD
11-21-2002, 03:40 PM
i hate curly's on same ligns, example:




for (int i = 1; i >=100; ++i){

cout<<i<<endl;}

as opposed to

for (int i = 1; i >=100; ++i)
{

cout<<i<<endl;

}

Imperito
11-21-2002, 03:51 PM
Well none of the majors use a closing brace after a semicolon, that'd be just wrong.

I use the K&R (also called kernel or 1TBS), where opening braces are places on the same line as whatever flow control structure te braces are used to denote.

The other major style is Allman, in which the opening curly braces are on the next line down. This style is for people who are stylistically defective. :D

There is actually another division of each of these, that is the placement of the closing curly brace on either the enclosed or enclosing scope. The enclosing scope is largely prefered, and used almost exclusively by kernel style coders, while Allman sometimes uses the enclosed, again attributable to defects in the users of this brace style. :D

subdene
11-21-2002, 04:17 PM
yes, i use the "allman" style, just how i've taught.

MethodMan
11-21-2002, 04:29 PM
I have always like to put opening and closing curly braces on a line by themselves, since I can locate the faster and check for unbalanced braces at the same time. I guess people just code using their own personal preference.

Waldo2k2
11-21-2002, 05:38 PM
i agree with method man
I use the almann style simply because it's easier to find chunks of code and to identify missing brackets (unexpected eof found). I used to do the other way
function(){
whatever;
}
but i like this way better

imperito,
so why exactly am i defective?

Stoned_Coder
11-21-2002, 08:10 PM
I have to say that I hate K&R style and definately prefer Allman. This is because I find it make the code much more readable. Also very easy to spot missing braces and brackets. About the only time I change this is maybe an empty constructor body.... just {} after the initialization list and on simple accessor functions where it helps to just bang it all on the one line. Helps group related functions together without bags of whitespace between them.

Mister C
11-21-2002, 09:46 PM
As an instructor I force my students to follow coding standards.
I prefer the Allman way. However, I make my Java students follow the Java programming conventions set by Sun.

adrianxw
11-22-2002, 12:39 AM
I had a URL that I can no longer find. On it were a list of university and business studies of C/C++ coding formats. In all the studies, Allman formatting came out with the best readability and lowest error stats.

The old fashioned K+R type formats, (there are acouple of others - Stroustrup, Whitesmith, etc.), are less readable. The original formatting was created because the printers of the day, (remember, people used to print out their code and plan their edits before returning to the card punch of teletype!), used less paper because the code lŠength was that little bit shorter.

With a modern scrolling full file editor/IDE this is no longer an issue.

nvoigt
11-22-2002, 04:30 AM
I personally think there should be a formatting standard, and not just a guideline. Anybody agree on this?


All companies I know have coding standards which include formatting.

salvelinus
11-22-2002, 08:05 AM
Originally posted by nvoigt
All companies I know have coding standards which include formatting.
Not mine. They just want whatever works in the shortest amount of time. Not good, I know. Try maintaining 50K+ line programs written in a variety of styles with no comments at all.

Brian
11-22-2002, 08:56 AM
Allman style here.

But since I cut and paste a lot of code from tutorials because I am still in "complete newbie" mode, my code ends up very messed up. Allman style here, kernel style there, hungarian notiation here, etc.

Stoned_Coder
11-22-2002, 09:47 AM
This (http://www.cuj.com/experts/1911/hyslop.htm?topic=experts) just about sums up my thoughts on hungarian notation.

adrianxw
11-22-2002, 02:52 PM
>>> All companies I know have coding standards which include formatting.


The same here. It is because a firm I was with was trying to enforce K+R that I did the research that showed Allman to be better.

The simple truth is that most people are happiest/faster/productive with the format they are familiar with. They will do less well in an environment that forces them to use a different one. So, let all the employees use their own style but dictate a company style for archived code and supply a code formatter template which allows anyone to change to/from whatever format they like.

salvelinus
11-22-2002, 03:10 PM
This is something that trips me up in Delphi as well. No brackets there, instead it uses "begin" and "end;". If there's an else, you omit the semicolon on the previous statement, even if it's an end. The default formatting starts the begin/end in the same column as the if (or whatever) statement beginning the block. If you get nested or sequences of statements within a block, it gets confusing over how many end statements and whether they need a semicolon, etc.
The compiler catches it, but it's annoying trying to figure out how many begin/ends are matching up. Not to mention the compiler is single pass, so you have to fix one error before finding another.

Cshot
11-22-2002, 03:48 PM
I prefer Allman also. I also like substituting spaces for tabs so they look consistent with all editors. I hate seeing posts where the indentation is 10+ spaces.

Imperito
11-22-2002, 04:14 PM
I never use tabs either. I use K&R with 2 spaces per level of indentation.

Waldo2k2
11-22-2002, 04:21 PM
spaces are probably more superior in that respect, however it is much easier to tab twice than space 10 times (standard tab=5 spaces)
But, it is much much quicker when an editor or ide use auto tabbing. Textpad (which i use to code for the web) has autotab but it doesn't go backwards to the previous level after } whereas visual c does. For speed of codign i prefer tabs.

Imperito
11-22-2002, 04:36 PM
I prefer spaces just b/c tabs are usually too big, and of course it is not clearly defined how they are represented. Many IDEs wil automatically turn tabs into X number of spaces, but even so they usually use too many.

Plus it is easiar to use sed scripts on spaced programs.