What are your curly-bracing style?
-
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.
-
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:
-
The proper way.
if (something){ do stuff do more stuff } else{ do something different }
Am I showing my age if I mention Kernighan and Ritchie? :)
-
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"
}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"
}With the exception of single-statement getters/setters, in which case
public int SomeProperty
{
get{return somePropertyValue;}
set
{
if (value != somePropertyValue)
{
someProperty = value;
// raise value changed event
}
}
}I like that putting the open brace under the function / operation name makes it easy to spot where braces match up, but think it looks clunky when you only have a single line in a getter / setter.
-
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"
} -
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've seen so many developers (especially newer ones) obsess over code syntactic readability, but I have found that I can get used to any style. My mental focus is mainly on code design (class, method, static definitions, etc.) more than the syntactic details. I would write try { myField = new Field(...); } catch { report error } because at a design level, it is only one logical statement. The error handing in this case is related to that statement, so I kept it on the same line. If it takes two steps to do something, I'll spread out the code to more lines: try { myField = new Field(...); myField.Event += event handler; } catch { report error }; Still, my focus is not on seeing (or obsessing over) curly braces, but rather on seeing the logical steps going on. I want the curly braces to drift into the background. I'm a contractor, so I match the style used by each employer, and I adapt pretty quickly to whatever they do, no matter how illogical. I don't let myself obsess over syntax, so once I get used to a style, those curly braces just disappear from my view, leaving the logical structure. When code has inconsistent syntax (such as each developer using a unique style), I can't help but notice the syntax, distracting me from higher-level design. Thus, the most important thing for me is CONSISTENCY.
-
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"
}First of all, I'm happy to read this forum and not see too many "religious" arguments for styling one way or another! There are good, objective reasons to use different styles. I always go back to McConnell's "Code Complete." Chapter 31 is all about layout and style. Basically, the layout style needs to address these. I'm quoting headings from the book: Accurately represent the logical structure of the code Consistently represent the logical structure of the code Improve readability Withstand modification I think the first two are obvious. The latter two are what require the thought. Personally, I'm a white-space-supremacist bracist (good pun, earlier poster!), and like:
// Check for matching data results
if(a == b)
{
// The data match.
doSomething();
}
else if (a == c)
{
// Something else matched
doSomethingElse();
}
else
{
// Something may have gone wrong.
doErrorCode();
}The blocks are easily identifiable. It's almost impossible to screw up when you add code. It's also easy to comment; the comments naturally identify the blocks.
-
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"
}if (you_had_asked_me_10_years_ago)
{
I_would_have_done_it = this_way;
}
else
{
see = below;
}if (you_ask_me_now) {
that = what_I_prefer_now;
}
else {
what=ever;
}My main reasons are: 1. optical readability through indentation - all the actual code belonging to the block is indented 2. the ending '}' is easier to find when it's not indented with the block 3. there is no reason to start a new line for an opening '{' since the statement causing the new control block already is signal enough, and optically starts the block because of the indentation in the following line(s) 4. I always use curly brackets, even if there's only one line. (a) This way I better recognize that there is some kind of control flow even when the indentation is messed up. (b) Also, in my experience most one-line blocks won't stay one-liners for long. (c) And I also get some consistentency with control commands, as they'll always take one line to start a block and one line (the '}' on a seperate line) to end. Of course, much of this is a matter of taste, but after trying several styles over the past 25 years, this is what pleases me most.