|
 |
|
 |
|
View Poll Results: Which Style of curly braces layout do you prefer?
|
|
Style 1
|
  
|
7 |
30.43% |
|
Style 2
|
  
|
16 |
69.57% |
03-30-2003, 01:14 PM
|
#16 (permalink)
|
|
bloomberg
Join Date: Jun 2002
Location: bloomberg
Posts: 263
|
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.
|
|
|
03-30-2003, 02:56 PM
|
#17 (permalink)
|
|
Code Monkey
Join Date: Feb 2003
Location: Netherlands
Posts: 89
|
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.
|
|
|
03-30-2003, 07:46 PM
|
#18 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
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.
|
|
|
03-30-2003, 07:50 PM
|
#19 (permalink)
|
|
Regular Contributor
Join Date: Feb 2003
Posts: 120
|
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.
|
|
|
03-31-2003, 06:35 AM
|
#20 (permalink)
|
|
Code Monkey
Join Date: Feb 2003
Location: Netherlands
Posts: 89
|
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.
|
|
|
03-31-2003, 10:02 PM
|
#21 (permalink)
|
|
Regular Contributor
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
|
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--
|
|
|
03-31-2003, 11:14 PM
|
#22 (permalink)
|
|
bloomberg
Join Date: Jun 2002
Location: bloomberg
Posts: 263
|
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.
|
|
|
04-01-2003, 02:10 AM
|
#23 (permalink)
|
|
Regular Contributor
Join Date: May 2002
Location: Alkmaar, the Netherlands
Posts: 167
|
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)
|
|
|
04-01-2003, 02:43 AM
|
#24 (permalink)
|
|
bloomberg
Join Date: Jun 2002
Location: bloomberg
Posts: 263
|
why not put ALL your code on one line! after all... the compiler doesn't care!
ps: this technique really impresses work-mates.
__________________
-- bloomberg.
|
|
|
04-01-2003, 02:54 AM
|
#25 (permalink)
|
|
GNU/Punk
Join Date: Jul 2002
Location: stuffs
Posts: 66
|
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
|
|
|
04-01-2003, 08:57 AM
|
#26 (permalink)
|
|
Code Monkey
Join Date: Feb 2003
Location: Netherlands
Posts: 89
|
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.
|
|
|
04-01-2003, 12:27 PM
|
#27 (permalink)
|
|
Registered User
Join Date: Mar 2003
Posts: 3
|
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.
|
|
|
04-01-2003, 04:05 PM
|
#28 (permalink)
|
|
Regular Contributor
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
|
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--
|
|
|
04-02-2003, 05:41 AM
|
#29 (permalink)
|
|
Person of interest
Join Date: Mar 2003
Location: New Jersey
Posts: 102
|
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
|
|
|
04-02-2003, 07:13 AM
|
#30 (permalink)
|
|
Code Monkey
Join Date: Feb 2003
Location: Netherlands
Posts: 89
|
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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 12:04 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|