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"
}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.
-
Heh. You'll probably get as many responses as there are developers. Or religions. :) I prefer the Java style because it allows me to see more code using a shorter display. But when I'm editing code written by another developer, I adhere to his/her style. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
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:
I, too, align left and right braces under their base keyword. I have done this since I learnt Algol 60 which predates the C family of languages and had
begin
andend
rather than{
and}
. When I learnt Pascal, which overloaded theend
keyword, I added another convention: Add an end-of-construct comment. Labels (e.g. forswitch
cases) are aligned with the braces and the code that they point to is on another line indented from the brace. Typical fragments (not working code):if (discr > 0)
{
// Work out real roots
...
} // if
else if (discr < 0)
{
// Work out imaginary roots
...
} // else if
else // Equal roots
switch (dayOfWeek)
{
case Sat:
case Sun:
alert('Equal roots at the weekend - take the day off');
break;case Wed:
alert('Mid week with equal roots - take a nap');
break;default:
alert('Do unpaid overtime');
break;
} // switchNo, I do not brace single statement; and yes, I do treat
else if
as though it were a keyword even though it is not in most languages. Let the flame wars commence. Does anyone know how to insert blank lines in code samples? -
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"
}This doesn't relate to the curly braces, but your logic is incorrect. If a>b, b cannot be equal to a. Thus, "b is less than or equal to a" does not mean the same thing as "a is greater than b".
-
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:
it's obvious why. The braces are used to mark the start and end of a block, and you have them here in the same column (so your eye knows where to go to look for it) and without hiding them in other text. This is obviously a good style for this language. If you, or the person who taught you, or (the person who taught)^n you comes from a language where keywords delimit blocks, then you could end up with the style where braces are placed on the same line as the keyword. So if (a>b) ... blah ... else ... blah ... endif becomes if (a>b) { ... blah ... } else { ... blah ... } This is clearly inappropriate behaviour in the latter case because the keywords here do not delimit the block. The braces do, but they are made more difficult to find. It is appropriate in the other language, because the keywords which delimit the block are in the same column and are not hidden in other text.
-
If your tool is f*cked it's time to get a new tool.
___________________________________________ .\\axxx (That's an 'M')
The tool is fine. I just need to use it in accordance with its rules. edlin, on the other hand...
-
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:
-
Yes, exactly! I still have my circa 1981 copy of K&R! :) /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
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"
}Alt 2, since this is what the if snippet produces. I try to use all the snippet templates. As mentioned it can produce auto indenting on some editors, e.g. Komodo. Komodo also has code folding that helps emphasize the scope areas.
"Courtesy is the product of a mature, disciplined mind ... ridicule is lack of the same - DPM"
-
_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.)
Notepad++ has replaced my notepad for all but the simplest views.
SS => Qualified in Submarines "We sleep soundly in our beds because rough men stand ready in the night to visit violence on those who would do us harm". Winston Churchill "Real programmers can write FORTRAN in any language". Unknown
-
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 vote for getting rid of braces and using indentation exclusively. Using braces the way many people do causes too many wasted lines (empty of anything useful).
SS => Qualified in Submarines "We sleep soundly in our beds because rough men stand ready in the night to visit violence on those who would do us harm". Winston Churchill "Real programmers can write FORTRAN in any language". Unknown
-
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"
}quote:
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"
}I indeed like that style the most. In my opinion it is't unreadable at all as it seperates clearly nested code from the control flow statements. For me, being a Basic 'programmer' in my very young days stigmatized me in being really comfortable with this style of code. As is Basic I find just utilizing indentions much more readable and banish the braces into the flow statements. regards Thomas
-
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:
For me, this is the easiest way to determine functional blocks. But, for the example of if..else, I seem to vacillate on the else part. Lately I have been using this style -
if (a > b)
{
print "b is less than or equal to a";
print "it means a is greater than b";
}
else
{
print "a is greater than b";
}But, sometimes, I find myself typing this -
if (a > b)
{
print "b is less than or equal to a";
print "it means a is greater than b";
} else {
print "a is greater than b";
}No idea why.
-
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')
> I always use braces, even with a single line. I'm with you there. The only time I end up having something like -
if (a == b)
printf("a is equal to b");Is when I'm copying/using someone elses code and forget to add the braces. Usually due to laziness.
-
I, too, align left and right braces under their base keyword. I have done this since I learnt Algol 60 which predates the C family of languages and had
begin
andend
rather than{
and}
. When I learnt Pascal, which overloaded theend
keyword, I added another convention: Add an end-of-construct comment. Labels (e.g. forswitch
cases) are aligned with the braces and the code that they point to is on another line indented from the brace. Typical fragments (not working code):if (discr > 0)
{
// Work out real roots
...
} // if
else if (discr < 0)
{
// Work out imaginary roots
...
} // else if
else // Equal roots
switch (dayOfWeek)
{
case Sat:
case Sun:
alert('Equal roots at the weekend - take the day off');
break;case Wed:
alert('Mid week with equal roots - take a nap');
break;default:
alert('Do unpaid overtime');
break;
} // switchNo, I do not brace single statement; and yes, I do treat
else if
as though it were a keyword even though it is not in most languages. Let the flame wars commence. Does anyone know how to insert blank lines in code samples?I also put each brace on its own line (although I will also put comments on that line), aligned with the statement begining the block, and indenting lines within that block. Likewise, I DO brace single line statements. Why? Because it makes adding console output debugging statements (like TRACE()) to the code far less perilous. I absoulutely hate the java convention of the first brace at the end of the invoking statement's line, but the end brace on its own line. If only C++ hadn't insisted on being a superset of C, much good would have been accomplished. Maybe it could ditch across-the-board C compatibility when the NEXT standard is released, and do things like REQUIRE braces around single line blocks. Maybe then we could answer the question once and for all whether
int* pInt;
long& rLong;or
int *pInt;
long &rLong;, or even
int * pInt;
long & rLong;should be the one and only correct way to decalre a pointer or reference.
-
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.
> Thus, the most important thing for me is CONSISTENCY. Amen, to that, brother. One thing I find strange is that coding standards aren't as ubiquitous or enforced as they should be. It's like they are suggestions rather than mandatory.
-
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 like to use a compact form with braces in control statements on the same line, but also don't like the shorthand without braces. However, I switch for namespace and class declarations, as well as class member functions. 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" } namespace name { class dummy : public base { public: dummy( void ) { } }; } "Luck is the residue of Design"