What are your curly-bracing style?
-
Steve Mayfield wrote:
I use an indent with 2 spaces per level
First thing I do when a new dev comes on board, change ALL your indents to 2 spaces. There is nothing worse than picking up some code with a mixture of indents.
Never underestimate the power of human stupidity RAH
-
Code block is so long I can't see the voting bar! This is my preferred style:
if(a>b)
{
print "b is less than or equal to a";
print "it means a is greater than b";
}Each curly brace on it's own line, don't know why but it helps me read the code :confused:
Mine too. :thumbsup:
-
Colin Rae wrote:
I missed punched cards by about 2 years
Baby! I was a guru on the Holorith manual punch card machine! Hard copy terminals? We dreamed of hard copy terminals (first one we had was a T.I. one with thermal paper which, being a smoker back in the day, used to get really annoying when browsing a listing with a fag (English!) in my hand! And re the braces - see my post elsewhere, but your case only deals with finding the if then searching for the end brace - what about finding the end brace and searching up for the - oh, could be an If or some sort of loop, start of a class, method etc. etc.
___________________________________________ .\\axxx (That's an 'M')
-
Use tabs - then you can set them to whatever indentation you (each) like!
___________________________________________ .\\axxx (That's an 'M')
_Maxxx_ wrote:
Use tabs
Sure but in most apps you can set the number of spaces a tab represents, VS is 4, SQL Server is 8 :wtf:, this what I set to 2
Never underestimate the power of human stupidity RAH
-
Colin Rae wrote:
If you've indented it properly
If - well sure, but if you indented everything properly then you wouldn't need to have braces at all. you've - not necessarily me who wrote the original trivial - not sure I agree. if your indentation's large enough and your code block small enough, that's usually the case - but scanning upward your eye is still looking for 'something' in that column - rather than a brace in that column. It's no biggie either way - but I find the reasoning behind a brace on a new line to cover more cases than a brace on the end of a line.
___________________________________________ .\\axxx (That's an 'M')
-
Colin Rae wrote:
If you've indented it properly
If - well sure, but if you indented everything properly then you wouldn't need to have braces at all. you've - not necessarily me who wrote the original trivial - not sure I agree. if your indentation's large enough and your code block small enough, that's usually the case - but scanning upward your eye is still looking for 'something' in that column - rather than a brace in that column. It's no biggie either way - but I find the reasoning behind a brace on a new line to cover more cases than a brace on the end of a line.
___________________________________________ .\\axxx (That's an 'M')
_Maxxx_ wrote:
If - well sure, but if you indented everything properly then you wouldn't need to have braces at all.
Damn compiler seems to want them for some reason...!
_Maxxx_ wrote:
scanning upward your eye is still looking for 'something' in that column
When I've got multiple nesting of braces it doesn't matter whether I'm looking for a brace or an "if" - it's still hard to find the one that lines up! You're right though. It's all personal taste and what you're used to. The one thing I probably hate more than anything is someone trying to impose their own standard as gospel. If I modify someone else's code, I use their style (though I may wince a little!).
-
Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:
if(a>b)
{ // sometimes i write comment here about logic and parameters etc.
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
print "a is either either equal to or less than b"Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:
if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
print "b is less than or equal to a"
print "it means a is greater than b"
} else {
print "a is either either equal to or less than b"
}if(a>b)
{
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
{
print "a is either either equal to or less than b"
}if(a>b)
{ print "b is less than or equal to a"
print "it means a is greater than b"
}Either
if (condition)
{
statement;
statement;
}
else
{
statement;
statement;
}or
if (condition) {
statement;
statement;
} else {
statement;
statement;
}depending what I'm working on, and what everyone else uses in the project. Note the space after the
if
, and operators will have spaces on either side (likeif (a > b)
) Your method, I'd only use at gunpoint (and maybe not even then). Well, you asked! ;) -
Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:
if(a>b)
{ // sometimes i write comment here about logic and parameters etc.
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
print "a is either either equal to or less than b"Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:
if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
print "b is less than or equal to a"
print "it means a is greater than b"
} else {
print "a is either either equal to or less than b"
}if(a>b)
{
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
{
print "a is either either equal to or less than b"
}if(a>b)
{ print "b is less than or equal to a"
print "it means a is greater than b"
}Scarlett Johansson
Watched code never compiles.
-
Steve Echols wrote:
I'm a white space fanatic!
That makes you a bracist...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997Whilst I agree with Steve, I wouldn't put myself in the same bracket.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus!
-
Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:
if(a>b)
{ // sometimes i write comment here about logic and parameters etc.
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
print "a is either either equal to or less than b"Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:
if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
print "b is less than or equal to a"
print "it means a is greater than b"
} else {
print "a is either either equal to or less than b"
}if(a>b)
{
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
{
print "a is either either equal to or less than b"
}if(a>b)
{ print "b is less than or equal to a"
print "it means a is greater than b"
}It's not even close to on topic, but I think the whole premise is flawed. Storing code as plain text is such a pain in the sphincter. Having the code stored in a semantic way and retrieved, displayed and edited in a form that suits me at the time would be vastly preferable to me. But the chances of that happening in my life time? Slim to none. People have stopped talking to each other other spaces vs tabs, let alone not storing code as text.
-
Steve Echols wrote:
I'm a white space fanatic!
That makes you a bracist...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
I used the 1st alt method many years, but now use the 2nd alt method as it is much easier (for me) to see the block structure. I use an indent with 2 spaces per level
Steve _________________ I C(++) therefore I am
Steve Mayfield wrote:
2 spaces per level
But that depends on the font size and resolution -- measure your indent with a ruler, it may be the same visual depth as my four-SPACE indent.
-
Colin Rae wrote:
If you've indented it properly
If - well sure, but if you indented everything properly then you wouldn't need to have braces at all. you've - not necessarily me who wrote the original trivial - not sure I agree. if your indentation's large enough and your code block small enough, that's usually the case - but scanning upward your eye is still looking for 'something' in that column - rather than a brace in that column. It's no biggie either way - but I find the reasoning behind a brace on a new line to cover more cases than a brace on the end of a line.
___________________________________________ .\\axxx (That's an 'M')
_Maxxx_ wrote:
you wouldn't need to have braces at all.
Ah, Python.
-
My style is:
if (something)
{
DoSomething();
SoSomethingOther();
}
else
{
DoTheOpposite();
}As with every style or methodology I use, I try to have a reason for using it, so that I can justify it (even if only to myself!) and, importantly, change should a better method come along (using reasoning to define 'better') So: By having the 'if' aligned with the start and end brace, when scanning code it is trivial to visually see the structure - scan up from the end brace, you just need to look for another brace. If the start brace is at the end of a line of code, then scanning up from an end brace you need to look for if, while, do etc.etc. I always use braces, even with a single line. If, later on, I come back and need to add more cod to the If or the Else, then I insert it between the braces, and never forget - so I don't re-engineer
if (a==b)
printf("a is equal to b");to
if (a==b)
printf("a is equal to b");
HandleCasesWhereaEqualsb();With the advent of cleverer editors, with automatic indenting, highlighting of code blocks etc., the reasoniong becomes somewhat less important - but you don't get all of that when you open source in or print it (does anyone still print code?) And with the cost of VS2010 in the thousands, I can't guarantee that the editor of my choice will be on every workstation I need to edit on. Some of your reasoning is valid (in my view) but I always think that this obsession some programmers have with the reduction in keystrokes (I'd have to press TAB all the time to indent, I don't want to type two extra braces if I don't need to etc.) is plain silly. Much more time is spent looking at code than writing it - often looking for a problem, more often looking for divine intervention or, at least, inspiration!
___________________________________________ .\\axxx (That's an 'M')
10!
_Maxxx_ wrote:
does anyone still print code?)
Yes, but no so much anymore. I now print with Word -- half-inch margins and 8-point font, I get 112 characters per line. I need braces and such to stand out.
-
Use tabs - then you can set them to whatever indentation you (each) like!
___________________________________________ .\\axxx (That's an 'M')
_Maxxx_ wrote:
whatever indentation you (each) like
Edit and Notepad insist on 8. X| And, even the otherwise fine Xacc.IDE doesn't adjust indents properly. (When last I tried.)
-
Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:
if(a>b)
{ // sometimes i write comment here about logic and parameters etc.
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
print "a is either either equal to or less than b"Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:
if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
print "b is less than or equal to a"
print "it means a is greater than b"
} else {
print "a is either either equal to or less than b"
}if(a>b)
{
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
{
print "a is either either equal to or less than b"
}if(a>b)
{ print "b is less than or equal to a"
print "it means a is greater than b"
}PIEBALD's general rule for code formatting (yes, there is one): Things that go together (e.g. parentheses, braces, brackets, etc.) should line up, either horizontally or vertically. Addendum: Separators in lists "go together" with the list designators. Ad nauseum: Be liberal in defining what qualifies as a "separator". :cool: If what you have fits horizontally, so be it, but things are likely to get more complex as they develop so you should plan ahead to avoid having to reformat later. Worrying about vertical space is for others, use as much as you like. Don't choose a style simply because others deride yours; they can reformat if they like.
-
_Maxxx_ wrote:
whatever indentation you (each) like
Edit and Notepad insist on 8. X| And, even the otherwise fine Xacc.IDE doesn't adjust indents properly. (When last I tried.)
-
_Maxxx_ wrote:
If - well sure, but if you indented everything properly then you wouldn't need to have braces at all.
Damn compiler seems to want them for some reason...!
_Maxxx_ wrote:
scanning upward your eye is still looking for 'something' in that column
When I've got multiple nesting of braces it doesn't matter whether I'm looking for a brace or an "if" - it's still hard to find the one that lines up! You're right though. It's all personal taste and what you're used to. The one thing I probably hate more than anything is someone trying to impose their own standard as gospel. If I modify someone else's code, I use their style (though I may wince a little!).
Colin Rae wrote:
If I modify someone else's code, I use their style
Me too - though I once had a contractor working for me who seemed a little slow; it turned out that he didn't like the way we formatted code (this was Delphi code) so spent half of his time re-formatting it rather than doing anything useful. This all wouldn't have been quite so bad if it wasn't for the fact that he didn't like comments.. "People never maintain them, so they're worthless" was his reasoning. So whenever he came across any comment, he removed it!! I fired him.
___________________________________________ .\\axxx (That's an 'M')
-
Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:
if(a>b)
{ // sometimes i write comment here about logic and parameters etc.
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
print "a is either either equal to or less than b"Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:
if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
print "b is less than or equal to a"
print "it means a is greater than b"
} else {
print "a is either either equal to or less than b"
}if(a>b)
{
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
{
print "a is either either equal to or less than b"
}if(a>b)
{ print "b is less than or equal to a"
print "it means a is greater than b"
}I hope I never have to maintain any code you've written. This:
nikunjbhatt84 wrote:
if(a>b)
is just wrong. Use some whitespace!
if (a > b)
{
}Pete
-
Which method do u use for curly braces to create scope of a programming structure? I mostly prefer this method:
if(a>b)
{ // sometimes i write comment here about logic and parameters etc.
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
print "a is either either equal to or less than b"Note that, I don't use braces for a single line of scope and I indent the starting brace and ending brace and it is not on the same line where the control structure is defined. I use this approach because it makes easy (just hit Enter key, no need to press Tab key) to add a new line of code after the staring brace and before the first statement of the block. I use Notepad++ and it has slightly good matching braces hilting feature and this method helps to correctly lineup and identify scope content. Here are some more methods used my many programmers:
if(a>b) { // this is Flash's ActionScript's default formatting, I hate this style the most, I feel it most unreadable, some Java programmers and web designers working on CSS also use almost similar method for writing CSS rules
print "b is less than or equal to a"
print "it means a is greater than b"
} else {
print "a is either either equal to or less than b"
}if(a>b)
{
print "b is less than or equal to a"
print "it means a is greater than b"
}
else
{
print "a is either either equal to or less than b"
}if(a>b)
{ print "b is less than or equal to a"
print "it means a is greater than b"
}I used alt 2 for many years as it made it easy to see any missing code. These days I use condition { statement } condition { statement } Occasionally I use one-liners, eg. when it's just an test before one statement (two statement and there comes the curlies flying} As there are "prettifiers" available in most editors these days the subject is mot. Decide on one standard that all code should be formatted by before checking in and use what ever that makes you comfortable while coding.