PDA

View Full Version : Which brace style do you prefer?



NovaAesa
August 16th, 2008, 12:09 PM
Which bracketing style do you prefer? I'm not talking about what you work or uni makes you do, but rather what you prefer to use. Which is the best and why? Is there any advantage of one over the other or is it just personal preference?

Also, people who don't use languages with curly braces please don't vote.




K&R, BSD KNF:


<blockstart> {
<block_contents>
}


Allman style:


<blockstart>
{
<block_contents>
}


Whitesmiths:


<blockstart>
{
<block_contents>
}


GNU style:


<blockstart>
{
<block_contents>
}


Pico style:


<blockstart>
{ <block_contents1>
<black_contents2> }


Banner style:


<blockstart> {
<block_contents>
}

lisati
August 16th, 2008, 12:15 PM
I'm about 50/50 for Whitesmiths & GNU in the rare occasions that I use 'C', it depends on my mood at the time.

LaRoza
August 16th, 2008, 12:16 PM
Allman, although I also like GNU.

NovaAesa
August 16th, 2008, 12:17 PM
Well as for me, I use K&R.

-grubby
August 16th, 2008, 12:19 PM
I don't write much code that warrants brackets.. But in CSS I usually use K&R style

Kadrus
August 16th, 2008, 12:23 PM
GNU,but i like Pico as well.

samjh
August 16th, 2008, 12:26 PM
Ahem: http://ubuntuforums.org/showthread.php?t=758698 ;)

I chose K&R, but will also use Allman.

NovaAesa
August 16th, 2008, 12:36 PM
Woops, I probably should have checked if this kind of thing had been done before :S


EDIT: anyway, my reasoning for K&R is that it is most like Visual Basic. Yes I know, an MS language etc etc, but it was my first :P

K&N greatly resembles this:


IF i = 5 THEN
i = i + 1
END IF

bobbocanfly
August 16th, 2008, 12:48 PM
Allman, without a doubt. Everything else looks too messy for me, and when you are drunk at 3 in the morning trying to write C, it definately helps to have clean(ish) code.

nvteighen
August 16th, 2008, 12:52 PM
Allman.

jimi_hendrix
August 16th, 2008, 01:00 PM
i knew that it didnt matter how you placed your brackets but i never knew there were "styles" for it

nvteighen
August 16th, 2008, 01:07 PM
i knew that it didnt matter how you placed your brackets but i never knew there were "styles" for it
It's one of the religious wars in the C universe!

EDIT: See this from Hackles! http://hackles.org/cgi-bin/archives.pl?request=98

NovaAesa
August 16th, 2008, 01:09 PM
It's almost as big as Vi vs Emacs.

grndrush
August 16th, 2008, 01:11 PM
The type that itches the least.

"See y'all at the debates, bitches." -- Paris Hilton

jimi_hendrix
August 16th, 2008, 01:12 PM
well i like allmans because its cleanest and i tear my hair out when i see any of the other styles in a book

tad1073
August 16th, 2008, 01:14 PM
I don't code very often, but when I do, I prefer to use Allman or GNU.

Torajima
August 16th, 2008, 01:30 PM
Definitely Pico... I started doing this style on my own, didn't know there was a name for it.

LaRoza
August 16th, 2008, 02:04 PM
Allman, without a doubt. Everything else looks too messy for me, and when you are drunk at 3 in the morning trying to write C, it definately helps to have clean(ish) code.

+1. The K&R style is way to messy for me.



It's one of the religious wars in the C universe!

EDIT: See this from Hackles! http://hackles.org/cgi-bin/archives.pl?request=98

I love Hackles, especially that cartoon (and the one about editors as well)


It's almost as big as Vi vs Emacs.

No, bigger. It doesn't matter if other developers are using Emacs or Vi as long as it is readable in other editors (means, no notepad). However, the braces style matters more to others.

pmasiar
August 16th, 2008, 02:28 PM
What about Python-style braces? :-)

Best braces are no braces!

In case someone is wondering, it was just a joke. K&R braces for me, or whatever IDE supports.

LaRoza
August 16th, 2008, 02:31 PM
What about Python-style braces? :-)

Or Lisp.



Best braces are no braces!

True, but since the braces have to be there, there are many ways of dealing with them. Most of the styles try to accommodate the braces and keep it readable. The indenting part is easy (like Python), what to do with the two little characters that hang around isn't.

NovaAesa
August 16th, 2008, 02:57 PM
Here is what python-like braces would be like (if there was such a thing :P)



if (booleanCondition) {
do some stuff }
else {
do some other stuff }




while (booleanCondition) {
do some stuff
do some more stuff }

imdano
August 16th, 2008, 04:17 PM
K&R when I'm writing Java, Allman if I'm doing C/C++.

kostkon
August 16th, 2008, 05:21 PM
K & R, don't like putting the start brace in a new line.

issih
August 16th, 2008, 05:51 PM
I tend to do Allman for just about everything except function and class opening and closing braces where I use K&R, provided there are no initialisation lists to confuse things.

Basically code flow I use Allman, function/object definition K&R, to me it makes sense to differentiate, but then I am odd.

elithrar
August 16th, 2008, 05:54 PM
K&R, though it's a tough choice between that & Allman. Most of my code is Python, though, so it's not something I have to think about that often.

pmasiar
August 16th, 2008, 06:51 PM
Here is what python-like braces would be like (if there was such a thing :P)



if (booleanCondition) {
do some stuff }
else {
do some other stuff }




while (booleanCondition) {
do some stuff
do some more stuff }


Such braces: Never!

Guido is genius, he avoided such useless holy wars with single brilliant decision (of not having braces at all). That way, it's much easier to agree where not to place them :-)

slavik
August 16th, 2008, 07:06 PM
and then he started another one, how many spaces for indentation and whether to use spaces or tabs. :)

LaRoza
August 16th, 2008, 07:16 PM
and then he started another one, how many spaces for indentation and whether to use spaces or tabs. :)

That is important in C as well.

NovaAesa
August 16th, 2008, 07:19 PM
Except that in C it doesn't matter if you have a mix (from different people in your team having different preferences). If you try doing that in Python.... uh oh.

British0zzy
August 16th, 2008, 07:59 PM
I use allman for functions, and K&R for everything else (if, else, while, for, structs, etc...)

Darkhack
August 16th, 2008, 08:05 PM
When I first started programming I used K&R. A friend later showed me Allman style. I've been using Allman for the past couple years but I can easily switch between it and K&R with no problems. I think I prefer Allman though, because if I'm searching through code, I can find the opening and ending of the block with no problems. With K&R, the opening of the block isn't as easy to see on a glance. I could tell by whitespace (a la Python), but having an actual character there makes it easier it seems. Also any IDE that's worth my time will highlight code blocks.

As for creating whitespace. Tabs. An IDE isn't worth using if it doesn't have an option to change how large a tab is. That way, other programmers can change the setting and automatically the code is set to their preferred tab size. Plus all you have to do is hit a single tab key. With spaces, you can't change the size and you have to hit the space bar multiple times. I prefer that a tab equals four spaces, but that's just me.

I can understand why people have different opinions on brace style and tab size, but it baffles the mind to think that anyone would prefer spaces to tabs.

Npl
August 16th, 2008, 08:07 PM
Allman for almost everything, K&R for smallish and simple (not many levels) do/while/if blocks

pmasiar
August 16th, 2008, 08:34 PM
it doesn't matter if you have a mix (from different people in your team having different preferences). If you try doing that in Python.... uh oh.

That's common myth (that different preferences in Python project will clash), but the only problem is mixing tabs and spaces (which will be syntax error in Py3K).

Certainly different parts of code can be written using different preferences (it might be ugly, like such mixed code is in any language, but certainly not a syntax problem). The only limit is to maintain single preferences inside any block.

Guido officially pronounced (in his blog) Python: Myths about Indentation (http://www.secnetix.de/olli/Python/block_indentation.hawk) as official FAQ dispelling such myths.

NovaAesa
August 16th, 2008, 09:21 PM
With spaces, you can't change the size and you have to hit the space bar multiple times.
Any good plain text editor (and IDEs) should be able to insert the desired number of spaces when you press the tab key.


I can understand why people have different opinions on brace style and tab size, but it baffles the mind to think that anyone would prefer spaces to tabs.
What happens when you need to keep the maximum width of each line to 80 characters? (this is a standard in many languages). Tab sizes aren't universally defined, so a line might display as 82 characters wide on one setup and 78 wide on another.


That's common myth (that different preferences in Python project will clash), but the only problem is mixing tabs and spaces (which will be syntax error in Py3K).my bad :S

jmillikin
August 16th, 2008, 09:35 PM
Allman, with tabbed indentation. I hate viewing code indented with spaces because they usually have very small indents (2/4 spaces), which I find hard to read.


Any good plain text editor (and IDEs) should be able to insert the desired number of spaces when you press the tab key.

If the author of the code uses 4 spaces to indent, and you prefer 8 spaces, I've never seen an IDE that lets you view the 4 as 8 without changing the file.


What happens when you need to keep the maximum width of each line to 80 characters? (this is a standard in many languages). Tab sizes aren't universally defined, so a line might display as 82 characters wide on one setup and 78 wide on another.

If you're so bothered by a line being 2 extra characters long, just shorten your tabs.

LaRoza
August 16th, 2008, 10:00 PM
I can understand why people have different opinions on brace style and tab size, but it baffles the mind to think that anyone would prefer spaces to tabs.

Because a space is a space and will look like a space no matter what. With tabs, you risk mixing them (I often press the space bar instead of hitting the tab button (which enters four spaces anyway))

bruce89
August 17th, 2008, 01:08 AM
I wonder if people know there's a note in K&R which says "we're doing it like this to save paper".

Sinkingships7
August 17th, 2008, 01:55 AM
K&R. I just find it much easier to read. Don't waste precious white spaces.

happysmileman
August 17th, 2008, 02:01 AM
K & R plus Allman.

Basically classes and functions get Allman and if, else, while, for etc. get K & R

e.g.



class myClass
{
...;
};

int main(int argc, char* argv[])
{
if (x == 3) {
...;
} else {
...;
}

for (int i = 0; i < 6; ++i) {
...;
}

do {
...;
} while (x == 3);
}

yabbadabbadont
August 17th, 2008, 02:06 AM
Because a space is a space and will look like a space no matter what. With tabs, you risk mixing them (I often press the space bar instead of hitting the tab button (which enters four spaces anyway))

That is why you tab to the base indentation level, and then use spaces to adjust from there if needed. That way, you can change the tab width to anything you want, but stuff will still be aligned correctly. (assuming that that is important)

lisati
August 17th, 2008, 02:09 AM
What happens when you need to keep the maximum width of each line to 80 characters? (this is a standard in many languages).

Isn't the 80-character limit a hanger-on from the days when punched cards were more common and when the displays on your typical x86-based PC were 80 text-mode characters wide?

pmasiar
August 17th, 2008, 02:35 AM
Isn't the 80-character limit a hanger-on from the days when punched cards were more common and when the displays on your typical x86-based PC were 80 text-mode characters wide?

No, kernel code still has that restriction.

It helps code readablity immensely if you don't have to scroll sideways to read it - and if you need more than 3-4 indents, your code has bad design and you better redo it. Line length is important CodeSmell test.

yabbadabbadont
August 17th, 2008, 02:53 AM
Isn't the 80-character limit a hanger-on from the days when punched cards were more common and when the displays on your typical x86-based PC were 80 text-mode characters wide?

72 columns for us old FORTRAN users... the extra columns at the end were usually used for card sequence numbers. God help you if you didn't use them and then dropped a deck of cards...

yabbadabbadont
August 17th, 2008, 02:55 AM
No, kernel code still has that restriction.
That is a convention, not a physical restriction of the compiler or editors used. Unlike with punched cards. ;)


It helps code readablity immensely if you don't have to scroll sideways to read it - and if you need more than 3-4 indents, your code has bad design and you better redo it. Line length is important CodeSmell test.

You are quite right about that.

pmasiar
August 17th, 2008, 03:07 AM
That is a convention, not a physical restriction of the compiler or editors used.

Try to tell that to Linus :-)

Of course it is a convention - but without it, email-based review of code patches would be much harder.

Wybiral
August 17th, 2008, 03:34 AM
Try to tell that to Linus :-)

Of course it is a convention - but without it, email-based review of code patches would be much harder.

Also, coding in a terminal-based editor becomes frustrating (since most terminals are 80x20, or something like that).

But mostly, it's good to have standards... And more than 80-chars wide is hard to visually parse for information.

yabbadabbadont
August 17th, 2008, 03:40 AM
Also, coding in a terminal-based editor becomes frustrating (since most terminals are 80x20, or something like that).

But mostly, it's good to have standards... And more than 80-chars wide is hard to visually parse for information.

80x25 was the old DOS standard. Console ttys and X terminals can be just as big as the connected display will allow. My text console is 160x64 while my xterm windows are only 158x60. (different font sizes in each)

indecisive
August 17th, 2008, 03:43 AM
Ahem: http://ubuntuforums.org/showthread.php?t=758698 ;)

And? It provides fewer options and the options are very biased. Perhaps that is why you have different results from your poll...

LaRoza
August 17th, 2008, 04:29 AM
I wonder if people know there's a note in K&R which says "we're doing it like this to save paper".

Probably not. Most people don't read K&R, they just have a copy in their house in a place people will see it.


That is why you tab to the base indentation level, and then use spaces to adjust from there if needed. That way, you can change the tab width to anything you want, but stuff will still be aligned correctly. (assuming that that is important)

Are you suggesting mixing tabs and spaces or did I read that wrong? In the big picture, using all tabs, or all spaces doesn't matter, however, the convention for Python is to use four spaces. I find that to be a good size, very readable without wasting space, so I use it. I go for individuality in my personal life, but not when I write code (or other things I want people to understand with minimal effort)