There are 2 kinds of brace formatting i usually see when i look at people's code -

Kind (a) - With opening braces on the same line as the construct.
Usually seen in people used to coding in Java.


public class myClass{
public void MyMethod(){
while(i<10){
i++;
}
}
}


Kind (b) - With opening braces on the next line. Usually seen on MSDN examples and most people using MS products seem to prefer this


public class myClass
{
public void MyMethod()
{
while(i<10)
{
i++;
}
}
}


I prefer Kind b
Posted on 2004-10-18 02:55:21 by clippy
method b indeed, a is very ugly.
Posted on 2004-10-18 03:10:57 by lifewire
Method a, but a space between the ) and { is important.
Posted on 2004-10-18 03:56:26 by Eóin
i use method b as i'm used to it. i started programming using this style, however if you want to copy a part of code, it's unconfortable using method b - a would be far better :)

greets, hartyl
Posted on 2004-10-18 04:18:36 by hartyl
I use K&R style:



void func()
{
if (condition) {
...
}

if (condition2) {
...
} else {
...
}
}


I find the brace-per-line approach quite wasteful.
Posted on 2004-10-18 05:30:38 by death
I prefer a. Sometimes I don't even bother indenting. lol.
Posted on 2004-10-18 07:02:09 by roticv
I used to use a, but the company I work for has a coding standard which uses b, so I now use that (and I've used it long enough that I now prefer it).

The real problem I have is propper tokenisation!

"a+=b" vs. "a += b".
"if(a)" vs. "if (a)".

Space between the if and the bracket, no space between a function name and the bracket!

I'm lucky though, the coding standard I've got to follow is fairly lax, another group within the company has a much more strict one. Blank lines before and after comments and other really stupid things that don't make code more readable or maintainable!

Mirno
Posted on 2004-10-18 07:32:46 by Mirno
I like more a, less tyiping be each open brace....

Altought I dont like a lot of things regarded to formating, commenting and documenting.

In that regard I remember much "a source editor is not a text editor" from betov I think he is right, because formating, commentation and edocumentation have been there from time a go, some things are evolutionated changed, but formating, documentating and comenting are really bad things tht each IDE, some RAD do pretty bad way.


For example:

Normally you watch in source: FIX:, TODO: and other things like comments, you normally need search or remember where is a FIX to do ro something TODO...



I am on the side that the source code himself not need commenting and also not need documentating, there should be diferent tools for this diferent things, also link them with a source control system, and you can not only have warning and notes on each function but a more organized source, documentation and coments, warning, todo and other goodies...

By the way the formating should not be done at the side of the programmer, is like we are used to MS Word, but I like in some way more the style that is imposed with a editor in Linux that is called LyX if I remember correctly, there you have a layout to follow and yo dont format nothing, you signal what parts are tittle, what parts are sections, and all the formating of the text is done in the side of the application also some things are generated automatically, numeration, index (with links), if you change a section to other place there is no problem is automatic the indexation, etc.... is more cool than do a manula Index and also more clean, some like that should be done with the source editors, not text editors.




The source code then will have source code.

The comments, warning in general comments and announcements for programmers should be puted in a separate file, not in the source (or at the end of the source), but plz not in the middle of each instruction!!!!!, method, function, label!!!!.

The documentation not need be inserted ecually than announcements to programmers in the middle of the code, that(insertion of no code in the middle of source code) for me make the source code feel and watch very ugly.

The source editor, not the text editor that can call a compiler, assembler, etc. Should open this diferent files, also use the control version system, and in that way you can search in X verrsion for BUGS, WARNINGS, TODO. and others things automatically.



The application need join them if necesary or request of the programmer, or a TODO or annuncement in the actual version....

Pheraphs the applications can do that with the inserted no source code in the source code search for TODO, FIX and others "flags" that normally programmers put, but that type of manula flags should be avoided for keep more easely the track over them....






I guess you have the point, but yes I like more style a.... :)


Have a nie day or night.
Posted on 2004-10-18 08:08:18 by rea
In case if the code is not to be distributed I prefer more A, but with space before the bracets. Also closing bracets i usually put on one line. Makes the source a bit cleaner in my opinion.
void xmpl() {

if () {
while() {
}}}

Otherwise I tend not to put closing bracets on the same line. Most people find it inconvenient.
Posted on 2004-10-18 09:44:14 by arafel
method b, even for if's and such. Easier to see the block as a whole.
Posted on 2004-10-18 10:09:43 by f0dder
Lines of code on screen is important to me.
So, I'm not in the habbit of using method "b".
Posted on 2004-10-18 10:24:52 by bitRAKE
Here is my own method inspired from Pascal, no need of braces. :)


#include <stdio.h>

#define begin {
#define end }

void main()
begin
int i;
for(i=0;i<5;++i)
begin
printf("begin&end instead of braces :)\n");
end
end
Posted on 2004-10-18 12:40:23 by Vortex
ugly, vortex >_<
Posted on 2004-10-18 12:43:19 by f0dder
f0dder,

Believe me, I am not surprised by your reply :-D
Posted on 2004-10-18 12:57:24 by Vortex
It's more typing, and (much worse), not very "visually outstanding" - it's easier to spot the pretty nonstandard '{' and '}' characters. And hell, pascal was my first programming language :)
Posted on 2004-10-18 13:02:14 by f0dder
Here is my own method inspired from Pascal, no need of braces. :)


#include <stdio.h>

#define begin {
#define end }

void main()
begin
int i;
for(i=0;i<5;++i)
begin
printf("begin&end instead of braces :)\n");
end
end



Instead of braces or some symbol for open and close (one or a secuence fo them...), why not use the multicolumn style? only with tabs (I dont know why there are people that like spaces for a thing that should be handled automatically by the source editor...)


void main()
int i;
for(i=0;i<5;++i)
printf("begin&end instead of braces :)??\n");
printf("Nope, best use vista de columna and less\n");
printf("typing :)\n");




Also like a side note that will force more the manual typing of first tabulator and back spaces saved for mark the end of a block... mean some like

#define { TABULATOR
#define } BACKSPACE

Altought the backspaces for delete a uncorr3ct --> incorrect word should not be saved, but the backspaces that delete a tabulator should be the end of the block... and you have automatic indentation one more time without the need of some visible mark and you still watching the blocks ;). Hope you get the Idea :).
Posted on 2004-10-18 13:25:18 by rea
hgb, have a look at python - www.python.org. It's an interesting idea, as it enforces good formatting of source... I still prefer brackets, though.
Posted on 2004-10-18 13:31:02 by f0dder

Instead of braces or some symbol for open and close (one or a secuence fo them...), why not use the multicolumn style? only with tabs (I dont know why there are people that like spaces for a thing that should be handled automatically by the source editor...)


void main()
int i;
for(i=0;i<5;++i)
printf("begin&end instead of braces :)??\n");
printf("Nope, best use vista de columna and less\n");
printf("typing :)\n");



well, with a normal compiler would take the for loop like this:



for(i=0;i<5;++i)
{
printf("begin&end instead of braces :)??\n");
}
printf("Nope, best use vista de columna and less\n");
printf("typing :)\n");


we can start another poll too, about using ++i or i++, in this case there is no difference, but some people believe ++i is faster (imho some ancient legend, it shouldn't make any difference for compilers made after my birth-date :) )
Posted on 2004-10-18 13:49:05 by lifewire

we can start another poll too, about using ++i or i++, in this case there is no difference, but some people believe ++i is faster (imho some ancient legend, it shouldn't make any difference for compilers made after my birth-date :) )

That depends on what is the type of 'i'. If it's primitive data like an int, it doesn't make any difference for compilers made after your birth-date. However, if it's an iterator class, for example, it might be faster indeed, since the postfix increment needs to create a copy of itself prior to increasing and return it.
Posted on 2004-10-18 14:39:16 by death
By the way, void main is just plain wrong. Don't use it.
Posted on 2004-10-18 14:49:47 by death