Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Program Design and Methods

View Poll Results: Which Style of curly braces layout do you prefer?
Style 1 7 30.43%
Style 2 16 69.57%
Voters: 23. You may not vote on this poll

Reply
 
LinkBack Thread Tools Display Modes
Old 03-28-2003, 07:18 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
formatting

which style do you prefer?

Style 1:
Code:
if( var == 1) {

  print("something);

} else {

  print("something else");

}
Style 2:
Code:
if( var == 1) 
{

  print("something);

} 
else 
{

  print("something else");

}
i'm mainly talking about the bracket layout here. which style do you prefer?
sde is offline   Reply With Quote
Old 03-28-2003, 08:17 PM   #2 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
I said style 1 because that is mainly how I do if/else if/else statements. but for fxns, i use style two. and loops, usually style one. constructors, style two.
alpha is offline   Reply With Quote
Old 03-28-2003, 08:44 PM   #3 (permalink)
EscapeCharacter
GNU/Punk
 
EscapeCharacter's Avatar
 
Join Date: Jul 2002
Location: stuffs
Posts: 66
EscapeCharacter is on a distinguished road
Send a message via AIM to EscapeCharacter
1 because im lazy and the other way requires you to hit enter one more time
__________________
cd /usr/ports/misc/life && make install
EscapeCharacter is offline   Reply With Quote
Old 03-28-2003, 09:24 PM   #4 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
#1 because that's just the way I've always done it, which makes it easier for me to read since I'm used to it. And I'm the only one who reads my own code, so I stick with the same style.
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 03-28-2003, 11:11 PM   #5 (permalink)
DesertWolf
Non-profit Techie
 
DesertWolf's Avatar
 
Join Date: Dec 2002
Location: Mesa AZ
Posts: 76
DesertWolf is on a distinguished road
Send a message via AIM to DesertWolf
Actually I do mine like this:

if( var == 1) {

print("something);
}

else {

print("something else");
}

Kind of a cross between 1 & 2
DesertWolf is offline   Reply With Quote
Old 03-29-2003, 03:31 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
Style 2, everytime...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 03-29-2003, 06:03 PM   #7 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
wha wha whaaaa

I've never seen style 2 in my life. Style 1 all the way baby, I give so much space in my code it's like a giant field with little sheep like code snippets grazing about.

When and if I do a final version of anything I'll ususally make it tighter but compilers get rid of white space before they turn it into machine code anyway.
saline is offline   Reply With Quote
Old 03-29-2003, 06:36 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
hint

Check my code in the c++ forums :th: .
Valmont is offline   Reply With Quote
Old 03-29-2003, 06:40 PM   #9 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
Definitely style 2 since the editor I use will hide lines between braces and it works better with style 2.
palin is offline   Reply With Quote
Old 03-29-2003, 06:44 PM   #10 (permalink)
Travis Dane
Code Monkey
 
Travis Dane's Avatar
 
Join Date: Feb 2003
Location: Netherlands
Posts: 89
Travis Dane is on a distinguished road
Send a message via ICQ to Travis Dane
I use style #3.

Style #3 :
Code:
 if(var==1)
     print("something");
 else
     print("something else");
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-29-2003, 08:51 PM   #11 (permalink)
JJoSA
Registered User
 
Join Date: Mar 2003
Location: Between a rock and a hard place
Posts: 7
JJoSA is on a distinguished road
Never use style 3

Just becase you're allowed to leave off the braces, doesn't mean you should.

It's a bad habit, and will cause you endless headaches when you're trying to figure out why something is working, only to realize that a line of code was executing because it wasn't wrapped in an if statement as thought.

You lose nothing by adding braces, and you gain ease of reading and ease of maintainence

Just Friday I spent hours debugging a piece of code, and one of the main problems was exactly this... a coworker left braces off. Combine that with poor (IE no) indentation in the script and it was damn near impossible to stop

Trust me, I've been doing this 20+ years. Don'e leave the braces off
JJoSA is offline   Reply With Quote
Old 03-29-2003, 08:56 PM   #12 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
there is alot more to coding style than mentioned above. luckily, i have a complete .indent.pro file to protect me from your styles.

Code:
--braces-after-if-line
--braces-after-struct-decl-line

--cuddle-do-while
--dont-cuddle-else

--indent-level2
--brace-indent0
--case-indentation0
--no-parameter-indentation
--struct-brace-indentation0

--blank-lines-after-declarations
--blank-lines-after-procedures
--leave-optional-blank-lines
--leave-preprocessor-space
--no-space-after-casts
--no-space-after-function-call-names
--no-space-after-parentheses
--no-space-after-for
--no-space-after-if
--no-space-after-while
--space-special-semicolon

--dont-break-function-decl-args
--dont-break-procedure-type

--continue-at-parentheses
--dont-format-comments
--dont-star-comments
--preserve-mtime
--tab-size2
--no-tabs
joe_bruin is offline   Reply With Quote
Old 03-30-2003, 05:12 AM   #13 (permalink)
Travis Dane
Code Monkey
 
Travis Dane's Avatar
 
Join Date: Feb 2003
Location: Netherlands
Posts: 89
Travis Dane is on a distinguished road
Send a message via ICQ to Travis Dane
Quote:
Originally posted by JJoSA
Never use style 3

Just becase you're allowed to leave off the braces, doesn't mean you should.

It's a bad habit, and will cause you endless headaches when you're trying to figure out why something is working, only to realize that a line of code was executing because it wasn't wrapped in an if statement as thought.

You lose nothing by adding braces, and you gain ease of reading and ease of maintainence

Just Friday I spent hours debugging a piece of code, and one of the main problems was exactly this... a coworker left braces off. Combine that with poor (IE no) indentation in the script and it was damn near impossible to stop

Trust me, I've been doing this 20+ years. Don'e leave the braces off
I have never had the problem of forgetting braces, Never.
Adding braces causes big and ugly code, If you have a problem
with forgetting braces with multiple statements than blame that
and not the braces.
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-30-2003, 06:02 AM   #14 (permalink)
alpha
Regular Contributor
 
Join Date: Feb 2003
Posts: 120
alpha is on a distinguished road
Send a message via AIM to alpha
As long as you remember to use braces if you need two or more lines in your if statement; you'll be fine with style 3.
alpha is offline   Reply With Quote
Old 03-30-2003, 11:29 AM   #15 (permalink)
Cagez
Registered User
 
Join Date: Mar 2003
Posts: 3
Cagez is on a distinguished road
Code:
if($this) {
    that
}

else {
    this
}


loops(conditions)
{
    this
}


function / class
{
    here
}
For me it depends on how much code I'm putting between the curlies. If and else's I always use the same style, but for the others, if I'm only putting three lines, I use Style 1, if I'm putting lots of code, I use Style 2.
Cagez is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:06 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting