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-30-2003, 01:14 PM   #16 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
Quote:
Originally posted by Travis Dane
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.
big and ugly code?

Code:
if(x){
  //a
}else{
 //b
}
// hardly larger than:
if(x)
  //a
else
  b
i use style one always and "style 3" when i want to save 4 bytes.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-30-2003, 02:56 PM   #17 (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 abc123
big and ugly code?

Code:
if(x){
  //a
}else{
 //b
}
If we had this about ten times, It would take in alot of extra
space with no reason whatsoever, Just because we can't
remember braces?

To get to the point: Style #3 is the best, If you can't remember
to put braces or not than don't use it, Otherwise it would save
you space, Even though it is not much it IS space.
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-30-2003, 07:46 PM   #18 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
It is only space on paper, not at run time.

And curly braces aren't here for nothing. They are designed for the programmer to use it, for extra checking by programmer AND (important!!) by the system at compile time, to quote Mr. Soustroup.

And space is often (not always) not an issue for the hardcore/professional programmer because they tend to program on at least 19'" monitors (preferably 22" or more). Only if you are stuck with a 15"-er then you are outta luck.

But nevertheless, curly braces do have an important meaning as mentioned before.
Valmont is offline   Reply With Quote
Old 03-30-2003, 07:50 PM   #19 (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 know what you are doing, and can spot the error if you forgot braces, then save space. I use all three, just depends what I want the if statement to do. If I can save that line of code, and that 4 bytes, I probably will. Usually depends on my mood, but I try to use style one for if statements, and style two for fxns, consistently just for the consistency.
alpha is offline   Reply With Quote
Old 03-31-2003, 06:35 AM   #20 (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 Valmont
And curly braces aren't here for nothing. They are designed for the programmer to use it, for extra checking by programmer AND (important!!) by the system at compile time, to quote Mr. Soustroup.
I'd like to see where Strousup said that.
Also, You must take into account that Mr. Strousup programmed
on very old compilers, Back in the DOS age. We have everlasting
corrective compilers these days.
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-31-2003, 10:02 PM   #21 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
Quote:
Originally posted by Travis Dane
If we had this about ten times, It would take in alot of extra
space with no reason whatsoever, Just because we can't
remember braces?

To get to the point: Style #3 is the best, If you can't remember
to put braces or not than don't use it, Otherwise it would save
you space, Even though it is not much it IS space.
I recently finished up a Web application for a client that was just under 7000 lines of code. Out of curiosity, I wrote a little script to count the number of times I could have gotten away with leaving the braces off a statement.

The total "space" savings would amount to less than a kilobyte. So I guess if I was really anal about the size of my script I could reduce it from 269K to 268K.

Nah, I think I'll leave the braces in.

JJoSA made a good point that it can make debugging a nightmare.
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 03-31-2003, 11:14 PM   #22 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
Quote:
Originally posted by Epsilon
I recently finished up a Web application for a client that was just under 7000 lines of code. Out of curiosity, I wrote a little script to count the number of times I could have gotten away with leaving the braces off a statement.

The total "space" savings would amount to less than a kilobyte. So I guess if I was really anal about the size of my script I could reduce it from 269K to 268K.

Nah, I think I'll leave the braces in.

JJoSA made a good point that it can make debugging a nightmare.
no no, the trick is to reduce all your "if" statements so that they can fit in one statement

Code:
if(a){
  doThis();
  doThat();
  doSomething();
}

// becomes:
if(a)
  doThis();
if(a)
 doThat();
if(a)
 doSomething();
seriously tho, i use the no-bracket if statement all the time. I just wish I could write methods that way
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-01-2003, 02:10 AM   #23 (permalink)
Hrqls
Regular Contributor
 
Join Date: May 2002
Location: Alkmaar, the Netherlands
Posts: 167
Hrqls is on a distinguished road
when you dont use .. why would you place the code on seperare lines at all ??? using 1 line would even decrease the amount of code ...

i love brackets ...
__________________
The specialty of the house? thats me (cheap as always)
Hrqls is offline   Reply With Quote
Old 04-01-2003, 02:43 AM   #24 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
why not put ALL your code on one line! after all... the compiler doesn't care!

ps: this technique really impresses work-mates.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 04-01-2003, 02:54 AM   #25 (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
Quote:
Originally posted by abc123
why not put ALL your code on one line! after all... the compiler doesn't care!

ps: this technique really impresses work-mates.
that would be real fun to debug, 1000 parse errors on line 1
__________________
cd /usr/ports/misc/life && make install
EscapeCharacter is offline   Reply With Quote
Old 04-01-2003, 08:57 AM   #26 (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 Epsilon

The total "space" savings would amount to less than a kilobyte. So I guess if I was really anal about the size of my script I could reduce it from 269K to 268K.

Nah, I think I'll leave the braces in.
It's not about saving space, It's about how it looks.
If something like this:

Code:
 if(a==1)
 {
  // blabla
 }
 else
 {
  // blabla
 }
Could look like this:

Code:
 if(a==1)
  //blabla
 else
  //blabla
Then i prefer the last because it simply looks better (In my point
of view). As far as the debugging problems, I have no idea what
you're talking about, Just put a brace with 2 or more statements
otherwise not. Can't be simpler.
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 04-01-2003, 12:27 PM   #27 (permalink)
Cagez
Registered User
 
Join Date: Mar 2003
Posts: 3
Cagez is on a distinguished road
I agree with Travis here, the latter looks much better IMO. I find it ugly when there is only one line of code in the statement, takes the meaning out of "code block" IMO.
Cagez is offline   Reply With Quote
Old 04-01-2003, 04:05 PM   #28 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
Actually, when I have a conditional with only one statement I'll usually write it as:
Code:
if (something){doSomething;}
I see the point that the brackets are not needed, but everyone has their own coding habits. Not much sense in changing something that works fine IMO, and both methods work fine.
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 04-02-2003, 05:41 AM   #29 (permalink)
jeffro
Person of interest
 
jeffro's Avatar
 
Join Date: Mar 2003
Location: New Jersey
Posts: 102
jeffro is on a distinguished road
This may just be me but I only use brackets when I have more than one command or statement in an if block. Travis' style is correct but, if there are only 2 outcomes for the if block (true|false). If that is the case, why not just use this style?
Code:
(condition) ? true : false;
Other wise I have to use brackets to surround my statement.
Code:
if (var == 0 ) 
{
  //adsf
  //asdf
} else {
  //;lkj
  //;lkj
}
jeffro
__________________
Jeffro
Linux counter#:213782
GnuPG ID: 406238E7
jeffro is offline   Reply With Quote
Old 04-02-2003, 07:13 AM   #30 (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 jeffro
This may just be me but I only use brackets when I have more than one command or statement in an if block. Travis' style is correct but, if there are only 2 outcomes for the if block (true|false). If that is the case, why not just use this style?
Ternary Operators are used to give variable a value, It returns
a certain value, You can't really use it to execute, For example
functions.

Epsilon, Your alternative is good but you can get problems when
you have to for example excute a long function and have an else
in it, Becomes fairly long then.
__________________
OpenGL, DirectX
Travis Dane 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 12:04 AM.


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