Skip to main content
GameDev.net gamedev.net
🔒 Locked

Prefered Bracket Syntax

Started by Rob Loach Apr 22, 2003 at 8:52 AM 32 replies 1.5k views
Original Post
Rob Loach
Rob Loach
I've seen countless times that there are MANY different ways to deal with brackets. I was wondering which bracket syntax you all use and why.... 1.
int main(){
    Code here...  
}  
2.
int main()
{
    Code here...  
{  
3.
int maint()
    {
    Code here...  
    } 


I've experimented with all over and over again and I have to say that I prefer the first one because it takes up less room.

Which one do you prefer and why?    

[EDIT] Added #3.
 

- Rob Loach Current Project: Upgrade to .NET and DirectX 9 "Do or do not. There is no try." - Yoda [edited by - Rob Loach on April 22, 2003 9:55:40 AM]
Rob Loach [Website] [Projects] [
owl
owl
2

Because it's easy (to me) to see at first glance the starts and ends if they are nested.


foo()
{
foo()
{
sabadaba;
badu badu
foo()
{
return fafa;
asn addf;
foo()
{
as d;
return bar;
}
asn addf;
}
sadf ffd;
}
}


vs


foo(){
foo(){
sabadaba;
badu badu
foo(){
return fafa;
asn addf;
foo(){
as d;
return bar;
}
asn addf;
}
sadf ffd;
}
}


[edited by - xaxa on April 22, 2003 10:03:08 AM]
[size="2"]I like the Walrus best.
Miserable
Miserable
#2. I occasionally use #1 when conserving whitespace seems important (when programming in Java, for instance, I tend to use #1 for anonymous classes), but in most situations the increased clarity of #2 - the simple fact that the brackets line up and very clearly indicate the indentation depth - is worth the cost in whitespace.

I''ve only seen #3 in some programs I have had to mark (for school). It always makes me want to strangle people, though I don''t quite know why.
CWizard
CWizard
Please have some decency, the last one is still warm!

Demonstration of some style elements.

class Class
{
private:
Class(int a, int b) : _a(a), _b(b), _c(a+b) {
Function(c);
}
char *Function(int x);

public:
int _a;
int _b;
int _c;
};

char *Class::Function(int x)
{
char *var_name = 0;

if(x < _a) {
...
}

return var_name;
}



davepermen
davepermen
if(someThing) {
}
class SomeClass {
};
void someFunction() {
}

for me, it is more readable. but i can''t give arguments/reasons for it. the same applies to the one above trying to suggest

if(someThing)
{
}
class SomeClass
{
};
void someFunction()
{
}


for me, the first one is more readable..:D

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
intransigent-seal
intransigent-seal
Number 2.
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Jx
Jx
#2 beacuse it is far easier for me to see where two brackets line up in blocks of code. Just the way i''ve always done it i guess

BrownBean
BrownBean
#2 -> Because it''s much more clear, easier for me to read.
Mage2k
Mage2k
I use #2 for classes and data structures and #1 for methods and control structures... They are both very readable to me: #2 is easy because you can see the brackets lining up, #1 relies more on a visual "indented blocks of code" type of thing... But, it really doesn''t matter. Do whatever works best for you: it''s called personal style.
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
cavemanbob
cavemanbob
#1 because it wastes less screen space. #3 is just ugly. It doesn''t matter which though, just prick one you like and use a code formatting tool on other people''s code if you really care.


TerranFury
TerranFury
#2

If I download code and it isn't in #2 format, I make it #2 format! It's 9e99 times easier than #1 on the eyes. #3 is readable too though.

The only exception is stupid little stuff like this, say, in classes:

double getVal{return Val;}


[edited by - TerranFury on April 22, 2003 3:25:27 PM]
krez
krez
#3 for me, it makes it easier to spot code blocks when scrolling around looking for something. the indent (as compared to #2) makes it easy to spot the conditional or loop statement that is controlling the block.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Ilici
Ilici
#2 rules

also i put spaces between everythin (almost):


  

int a, b, aux;
a = b = 1;
do
{
if (a == b)
{
aux = a;
a = b;
b = aux;
}
}
while (a == b);
sanjo
sanjo
as opposite to Ilici''s style, i like to have it very tight

i also use #2.

btw. whats your indent tab size?
LockePick
LockePick
#2

Unless it's a one line function, in which case I'll just do:
void someFunction(){ blah blah; return; }
or
void someFunction()
{ blah blah; return; }

Never, ever #1 though. Even when it is readable, I hate having to move my eyes upwards and all the way over just to see when the function started.
Never seen #3 before. Looks extremely confusing when nesting because it's rare to see.

[edited by - LockePick on April 22, 2003 4:11:12 PM]
owl
owl
Does anybody has the habit of accommodating the declarations like this?



unsigned long ulNumChilds;
unsigned long ulWindowId;
int iTop;
int iLeft;
int iWidth;
int iHeight;
char* pcTitle;
bool bFocus;


I cannot tolerate to see this:

unsigned long ulNumChilds;
unsigned long ulWindowId;
int iTop;
int iLeft;
int iWidth;
int iHeight;
char* pcTitle;
bool bFocus;

[size="2"]I like the Walrus best.
Solo
Solo
quote:
Original post by xaxa
Does anybody has the habit of accommodating the declarations like this?



Yup (but I prefer to group the ''*'' with the variable and not the type).

I like #2 but will tolerate the others provided white space is used to help mark the beginning and ending of the blocks.


// Ryan
Zern
Zern
#2 and yes Xaxa, I do it that way as well.
Arild Fines
Arild Fines
"There is a special place in hell for K&R bracing people"



"Laughter means distance. Where laughter is absent, madness begins. The moment one takes the world with complete seriousness one is potentially insane. The whole art of learning to live means holding fast to laughter; without laughter the world is a torture chamber, a dark place where dark things will happen to us, a horror show filled with bloody deeds of violence."
-- Jens Bjørneboe
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
LockePick
LockePick
quote:
Original post by xaxa
Does anybody has the habit of accommodating the declarations like this?

I do. I noticed it while reading through some other peoples'' code and thought it was quite nifty. I don''t always use it, but if I''m going to make a long list of variables I usually do.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.