Brace style
-
I like the latter, because it conforms to style of proper programming. the boolean in me says yes.
All things being equal, tommorrow will never equal today
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.
-
Simple the second choice, because it makes more since and is easier to read. I used the first one for a few years and eventually found it very annoying when reading the code, because it is too easy to miss where the block starts. The third option would be to indent the braces, but that has never made since to me. Of course writing ‘if ()’ as opposed to ‘if()’ still does not make since to me, unless you are programming in multiple languages, where one requires that (like VB6). And I would like to know which idiot started the trend of writing ‘i++’ instead of ‘++i’, because that is not a matter of formatting but is a matter of understanding the language. Sorry, I almost got carried away for something so simple.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
John R. Shaw wrote:
Simple the second choice, because it makes more since and is easier to read. I used the first one for a few years and eventually found it very annoying when reading the code, because it is too easy to miss where the block starts.
I fully agree.
John R. Shaw wrote:
he third option would be to indent the braces, but that has never made since to me.
Same here.
John R. Shaw wrote:
Of course writing ‘if ()’ as opposed to ‘if()’ still does not make since to me
It makes perfect sense to me. I hate not having the space after the keyword. However, it's not a big deal. It's less of a deal than bracket placement and that's not much of a deal.
John R. Shaw wrote:
And I would like to know which idiot started the trend of writing ‘i++’ instead of ‘++i’, because that is not a matter of formatting but is a matter of understanding the language.
Most of the time it makes no difference and on most of the occasions when it does we shouldn't be writing code that way anyway. I've got used to writing i++ and it just looks nicer to me. However, I'm clued up enough to use ++it for STL iterators. But I haven't done any C++ for over 2 years.
Kevin
-
Who cares? Does this actually improve the quality of your code in any way? Does the first version produce different code to the second? It doesn't matter. As long as you are consistent and follow internal coding standards then it shouldn't matter which one it is.
Deja View - the feeling that you've seen this post before.
Pete O`Hanlon wrote:
Who cares?
I care, the first leads to unreadable code and working for many software houses, 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!.net is a box of never ending treasures, every day I get find another gem.
-
Doh, I learned to code from that book. For my 2c, I find myself siding daily with Messrs Kernighan and Richie on this point. I can't abide by all those extra line breaks.
Line breaks that do what? Bloat your source? I seldom code methods that need more than my current window, and that is with being anal about braces, as in even for single statement 'if' blocks, e.g. if (someExpression) { SomeStatement; }
-
Who cares? Does this actually improve the quality of your code in any way? Does the first version produce different code to the second? It doesn't matter. As long as you are consistent and follow internal coding standards then it shouldn't matter which one it is.
Deja View - the feeling that you've seen this post before.
Pete O'Hanlon wrote:
Does this actually improve the quality of your code in any way?
No, but religious wars are fun.:) Pointless, but fun.
Kevin
-
Pete O`Hanlon wrote:
Who cares?
I care, the first leads to unreadable code and working for many software houses, 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!.net is a box of never ending treasures, every day I get find another gem.
norm .net wrote:
I care, the first leads to unreadable code
I don't think it's unreadable. But it's less readable. However. many think the first is more readable on the grounds that they can see more lines of code at once. Incidentally there is some variation on the first style if (blah) { } else { } versus if (blah) { } else { } The latter is even worse as the if and else don't line up.
Kevin
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
Most people prefer the later, perhaps because they can more easily identify matching braces because they line up vertically. However, the former is often used in printed material because it takes up less space.
-
Nemanja Trifunovic wrote:
probably K&R these days
K&R the dudes or K&R the book? See my post: Even K & R preffered the latter
-
norm .net wrote:
I care, the first leads to unreadable code
I don't think it's unreadable. But it's less readable. However. many think the first is more readable on the grounds that they can see more lines of code at once. Incidentally there is some variation on the first style if (blah) { } else { } versus if (blah) { } else { } The latter is even worse as the if and else don't line up.
Kevin
Kevin McFarlane wrote:
don't think it's unreadable. But it's less readable
Ok I over-exaggerated somewhat, but brace per newline line seems to be the standard in every company I have worked.
.net is a box of never ending treasures, every day I get find another gem.
-
Line breaks that do what? Bloat your source? I seldom code methods that need more than my current window, and that is with being anal about braces, as in even for single statement 'if' blocks, e.g. if (someExpression) { SomeStatement; }
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
Is'nt it amazing how people constantly complain about code they didn't have to write? I think as long as the person doing the coding is consistant in there style and methods it really doesn't matter. The worst thing in the world would to make a rule about what is the proper way to code. The proper way is any way the compiler can understand...lol.
-
I have never known anyone who used them. But then again I have known few peaple who seem to want to make their life more difficult.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
pascal poisoning. Indenting
Begin End
pairs actually did make some degree of sense IMO. People who do the same in C style languages are as bad as those usinglpszFoo
style naming in VB.-- 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
-
Do'h, of course HTML strips spaces! so, try again: if (condition) do something; other code;
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
-
Which one you prefer ?
if{
//do something
}Or
if
{
//do something
}
printf("Navaneeth!!") www.w3hearts.com
The latter.
-
Bet you didn't know that! 20+ years ago I read an interview with the two and this topic was broached. They both said they were stunned at the attitude of "...if it's good enough for K & R, it's good enough for me" and the thought by some that copying the K & R style of braces made them a good programmer. Of course we are talking about their infamous book, "The C Programming Language." What they also divuldged was that it was not their brace style at all! Their explanation: The publisher discovered that they could save a whole line of space in the book by moving the opening brace to the end of the previous line and the program would still work. They also remarked, and I'm paraphrasing here, "Thank God the publisher didn't know you could write the whole program on one line!" Now you know the rest of the story... Page 2... :-D The last edition of "The C Programming Language" was updated for ANSI C in 1988. It is still in print! :omg:
Is the interview up on the web anywhere?
-- 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
-
Pete O`Hanlon wrote:
Who cares?
I care, the first leads to unreadable code and working for many software houses, 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!.net is a box of never ending treasures, every day I get find another gem.
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.
-
John R. Shaw wrote:
simple logic and experience will eventually show you what the problem is
I can over-complicate things for anyone. Sadly, simple logic really is not one of my strong points. :( However, I'd be delighted to benefit from experience, and I don't doubt that yours is more extansive than mine. Please, do enlighten me. I would like to learn.
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
-
That reminds me, how the hell do I make VS 2005 go to the next freaking line when I press Enter after typing 'try'? This is one of the most intrusive behaviours in this IDE.
-
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.
Pete O`Hanlon wrote:
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
Not worked up about. I suppose when contracting any old style will do (from experience working as a contractor with other contractors you couldn't give a sh@t as long as the code worked), but when working for software houses or development teams standard is a must and postion I've been in has always opted for brace per newline. And for single line coding I'd vote for
if (...) Func();
But at the end it's entirely up to you come which style you'd use..net is a box of never ending treasures, every day I get find another gem.
-
Pete O`Hanlon wrote:
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
Not worked up about. I suppose when contracting any old style will do (from experience working as a contractor with other contractors you couldn't give a sh@t as long as the code worked), but when working for software houses or development teams standard is a must and postion I've been in has always opted for brace per newline. And for single line coding I'd vote for
if (...) Func();
But at the end it's entirely up to you come which style you'd use..net is a box of never ending treasures, every day I get find another gem.
norm .net wrote:
Not worked up about. I suppose when contracting any old style will do (from experience working as a contractor with other contractors you couldn't give a sh@t as long as the code worked), but when working for software houses or development teams standard is a must and postion I've been in has always opted for brace per newline.
Errm - reread what I said. Any contractor should care about the standards they follow. Where there is an in-house standard, this MUST be followed. I've stated that twice now. What doesn't matter is what the standard is - use it and concentrate on your logic.
Deja View - the feeling that you've seen this post before.