Brace style
-
I have never written anything significant in Perl.
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
I much prefer the latter, code is easier to follow... Now a follow-up question for those that prefer the latter style: Which do you use/prefer when there is only one executable statement after an 'if' or 'else'? Style 1-
if (this)
doThis();
else
doThat();OR Style 2-
if (this)
{
doThis();
}
else
{
doThat();
}Me? I use Style 2. Yes it takes up more space, but I think it is more readable and is less prone to error if later adding another statement to the if or else section(s). (I just noticed that this question was discussed a bit earlier in the thread... sorry for the repeat) -- modified at 9:53 Thursday 17th May, 2007
-
Read the entire post and then comment on it. The key bits are: 1. Does the code work? 2. Does it follow coding guidelines? If you have guidelines that state how it should look then you follow them - regardless as to how distasteful you find them. I have my preferences for bracing style, but that doesn't mean that my opinion carries any more weight than somebody else who likes a different style.
norm .net wrote:
the first think you learn are the programming standard for that company and at a time top of the list if braces style. You'd get hanged if you coded the brace if(blah de blah){ } If so freakin unreadble!
Now extend that to include all the companies where you'd get hanged for using
if(...) { }
. I've been in this game so many years and I've been forced to use both styles by so many clients that I don't think it's worth getting worked up about. There are arguments on both sides, so don't get worked up about it. BTW - you haven't come down with a position on the use of braces for single line statements.
if(...) { DoThis(); }
versus
if(...) DoThis();
Deja View - the feeling that you've seen this post before.
oh yes we have,......see above
Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture) http://www.frankkerrigan.com/[^]
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
In college (late 80s), I was all about lined up braces (choice 2). However, once I stopped hacking so much, and instead started writing better planned-out code, readability of individual code statements became less important to me than readability of class-level design (function prototypes, member variables). Thus, I developed a consistent style of cramming code together. No empty lines unless it's temporary (fill in the blank later type of thing), and no newlines for braces unless there are two or more statements. Here is an extreme example of a one-function class. Assume that the set is one statement and the get is one statement (if more than one, I split it out like choice 1 in the original posting). namespace abc { class xyz { public string ToolTip { set { do; } get { return something; } } } } Basically, once I write it, I don't want to have to look at the implementation of it again. Sometimes during development of longer functions, I'll spread it out. Then, when it's all correct and tested, I shrink it down according to the one-statement rules. I'm always consistent with the {} usage. Of course, I couldn't pull off this crammed style in a typical team environment because people wouldn't trust each other to get it right the first time. If you did trust each other, it could work in that environment. Dale P.S. For the last two years, I wrote VB.Net code and got totally used to spread-out code. Experienced programmers can get used to any consistent style, I think. Newer programmers are typically more insistent on their own unique style. -- modified at 9:46 Thursday 17th May, 2007
-
If you keep the lines short then the former works just fine (experience), but the longer the lines get the more likely it is that you will fail to notice the opening brace. Modern environments allow you to place the cursor at a brace and use a key combination to find the other one, but you should not need to do that. If you failed to place the brace after the test statement then you may be confused as to why the compiler generated and error until you search for the missing brace, provided you realize that is the problem. Indention tells you it is supposed to be a block but the compiler does not recognize your intensions, only the facts. When you look at a test statement with an opening brace following it on the next line, you know that it is being blocked out. Of course symmetry also helps when reading the code. I resonantly spent a lot of time programming in ‘.Net’ and the default code generation produced very long lines and it is even hard to stay away from them in standard C++. Which means that unless you start wrapping your test statements to the next line (which I do and hate doing), then the opening brace may not be visible (if you use the former style). If you look at it from a strictly indention point of view then the brace would be in the wrong place because it would be inconsistent with the rest of the code. What all that bull amounts to is that the latter makes it easier to read and debug the code, and I have done enough of both to drive myself a little crazy.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
I agree with this. if (...) { DoThis(); } ...is just so much more readable when I've got a fairly long and complicated function. I also really despise this: if (...) { DoThis(); } else if { DoThat(); } else { DoThose(); } Extend the DoThis(), DoThat(), and DoThose() to something long, and you'll be going "huh? where the hell are my opening and closing braces?"
-
John R. Shaw wrote:
Regardless of the project, I automatically use what ever style is currently used if I am modifying or adding to someone else’s code.
Same here. Although if the style is especially bad, as was the case recently in some JavaScript I was maintaining, I ignore the existing style.
Kevin
Some years ago I had an application switch over to me so I could fix all the (major) problems with it. By the time I was through all the problems where fixed and all the files where reformatted to my liking. I was still maintaining that code and adding to it till the day I walked out, and it has not been changed since.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
I much prefer the latter, code is easier to follow... Now a follow-up question for those that prefer the latter style: Which do you use/prefer when there is only one executable statement after an 'if' or 'else'? Style 1-
if (this)
doThis();
else
doThat();OR Style 2-
if (this)
{
doThis();
}
else
{
doThat();
}Me? I use Style 2. Yes it takes up more space, but I think it is more readable and is less prone to error if later adding another statement to the if or else section(s). (I just noticed that this question was discussed a bit earlier in the thread... sorry for the repeat) -- modified at 9:53 Thursday 17th May, 2007
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
Navaneeth.Which one you prefer ? if{//do something} Or if{//do something}
Actually, I prefer: if (condition == true) { // do something }
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
-
Some years ago I had an application switch over to me so I could fix all the (major) problems with it. By the time I was through all the problems where fixed and all the files where reformatted to my liking. I was still maintaining that code and adding to it till the day I walked out, and it has not been changed since.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
if you left how do you know the code never was changed since then?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
The latter. The former is used only by miscreants and rogues. :suss:
----
i hope you are feeling sleepy for people not calling you by the same.
--BarnaKol on abusive words
Personally, I think it's a question of who really cares as long as you can read the thing and it kinda depends on what language I'm working with. For C# I pretty much go with the second example but as a complete heretic who occasionally finds himself coding Perl in a hurry, I've been known to chuck both examples and go with the following blasphemy:
do something if true;
Or even (gasp!):do something unless true;
Both of which eliminate the braces altogether... :-D -
Wrap it in pre tags to keep the formatting intact.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
dan neely wrote:
Wrap it in pre tags to keep the formatting intact.
:doh: I put   in for spaces. BTW, it was fun getting the   to print out!
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
-
I agree with this. if (...) { DoThis(); } ...is just so much more readable when I've got a fairly long and complicated function. I also really despise this: if (...) { DoThis(); } else if { DoThat(); } else { DoThose(); } Extend the DoThis(), DoThat(), and DoThose() to something long, and you'll be going "huh? where the hell are my opening and closing braces?"
That speaks louder than anything I could say on the subject. I have actually seen that done and was quick to untie the knots so I could understand what it was supposed to be doing. :laugh:
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
I like the latter as well, but I don't feel I am in any position to say it's more 'proper'. I would confine my public opinion of what is proper programming is only to ensure all braces match.
As far as "proper" I sorta meant it as a tongue in cheek remark. The style to which one uses brace formatting is really up to the user. It has its merits either way.
All things being equal, tommorrow will never equal today
-
If you keep the lines short then the former works just fine (experience), but the longer the lines get the more likely it is that you will fail to notice the opening brace. Modern environments allow you to place the cursor at a brace and use a key combination to find the other one, but you should not need to do that. If you failed to place the brace after the test statement then you may be confused as to why the compiler generated and error until you search for the missing brace, provided you realize that is the problem. Indention tells you it is supposed to be a block but the compiler does not recognize your intensions, only the facts. When you look at a test statement with an opening brace following it on the next line, you know that it is being blocked out. Of course symmetry also helps when reading the code. I resonantly spent a lot of time programming in ‘.Net’ and the default code generation produced very long lines and it is even hard to stay away from them in standard C++. Which means that unless you start wrapping your test statements to the next line (which I do and hate doing), then the opening brace may not be visible (if you use the former style). If you look at it from a strictly indention point of view then the brace would be in the wrong place because it would be inconsistent with the rest of the code. What all that bull amounts to is that the latter makes it easier to read and debug the code, and I have done enough of both to drive myself a little crazy.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
I whole heartedly agree with you. I personally feel that code like
if (something == true)
{
// blah blah
// blah blah blah
do
{
// blabbity blab
// ditto
if (somthingelse == false)
{
// initialize the flux capacitor
}
} while (somecodition == true);
}makes the blocks of code more clear. Just my personal preference. ;)
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
-
You said it about a lot of contractors there - no-brainer.:laugh: Mind you, I must admit that I cheat - I let the IDE take care of it for me. BTW - my personal preference is braces on a new line.
Deja View - the feeling that you've seen this post before.
-
I have never written anything significant in Perl.
You're not missing anything. Dreadful language.
Kevin
-
if you left how do you know the code never was changed since then?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
Because I lied, there were some minor changes. I know because I was contracted to make them. One of the reasons I left was because they did not really need a full time coder any more, they just did not know it. They did not hire anyone to take my place and any time there is a question or they need something modified they call me. About the only thing that took more than a couple of hours to do was when a third party control failed to work on XP and I had to write a replacement from scratch (the 3rd party did not exist any more). Now if only they would contract me to produce a modern version of the two main programs I would be a happy camper, because they would not let me do it when I worked there.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
http://astyle.sourceforge.net/[^] because I prefer:
style=ansi indent-namespaces
and one of the senior programmers keeps us from having a style guide by demandingstyle=ansi indent-brackets
Of course there is programmer brawl number #2342: how do you make a header comment (I was told yesterday there is one and only one way). :)_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
God of course intended that there be only one bracing style. All other expose one as a programmer who has fallen from the path. The pattern for an 'if' is as follows:
if (conditional) { // waste not, want not: don't waste vertical space
statement(s) // note the indention, this clearly identifies these statements as
// belonging to the if
} // note the indention again, this closes the if logic, it is the
// end of the existing block, not the beginning of a new oneOf course heritics abound, but my place in "code vault store" is assured (ya have to be a fan of STTNG Klingon afterlife to get that one). Go forth and spread the glad tiding and good bracings and do not hesitate to cntl-K cntl-D the code of the unbelievers. ;):laugh: Brace Zealot -- modified at 11:16 Thursday 17th May, 2007