Robert Hess said:My own preference (which does not appear to be shared by others) is for:
for (;{
}
and:
if () {
} else {
}
-Robert
+1 for this one ...
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Robert Hess said:My own preference (which does not appear to be shared by others) is for:
for (;{
}
and:
if () {
} else {
}
-Robert
+1 for this one ...
if ( ) {
} else {
}
...for me. I think using up an extra line for it is just waste of good space. Also, when you read it, you get "more code" into your viewing point without needing to move the eye up and down too much. New line look a little sloppy imho.
turrican said:if ( ) {
} else {
}
...for me. I think using up an extra line for it is just waste of good space. Also, when you read it, you get "more code" into your viewing point without needing to move the eye up and down too much. New line look a little sloppy imho.
I agree with you all, I like that style better. But it is easier to comment out statements the other way so the way I typically work is that I only correct the style when I think I'm done with the code.
turrican said:if ( ) {
} else {
}
...for me. I think using up an extra line for it is just waste of good space. Also, when you read it, you get "more code" into your viewing point without needing to move the eye up and down too much. New line look a little sloppy imho.
This thread really sucks with half the symbols in all of the posts being converted to smilies.
fknight said:turrican said:*snip*This thread really sucks with half the symbols in all of the posts being converted to smilies.
I always use column new line style just makes the code so much more readable, I never got the same line argument at all.
Another one is leaving out the braces for one statement conditions i.e.
if (condition)
statement;
I do this now but I never used to, I think reading in code complete there is an argument to always put in braces no matter what in case you revisit and need to add another statement you may forget to but the braces in and introduce an error, but I grew out of this way of thinking. My main reason was keeping the code small and concise, for one line statements it wont impact readability because how complicated can a one line statement be? (obvously within reason)
I've never encountered any maintenance problems using the no braces method and it can reduce a method to something smaller and readable IMO, nothing worse than lots of one statement if elses cluttering up a method body.
Sabot said:Hey I'm a column man just cos I'm old and my eyes don't work so well.
I'm with you Sabot - column - but I gave up my strong opinion long ago. I go with whatever my client prefers.
ScottWelker said:Sabot said:*snip*I'm with you Sabot - column - but I gave up my strong opinion long ago. I go with whatever my client prefers.
1TBS here, for no particular reason except that I'm too lazy to change. But I can still adapt, when there is a requirement to do so... too bad that the C++ editor in VS doesn't have all the options of the C# one.
Blue Ink said:ScottWelker said:*snip*1TBS here, for no particular reason except that I'm too lazy to change. But I can still adapt, when there is a requirement to do so... too bad that the C++ editor in VS doesn't have all the options of the C# one.
The C/C++ editor doesn't have the same autoformat features as the C# editor, so less options.
I'm still waiting for a native edition of C# from Microsoft, a man can dream.
Blue Ink said:ScottWelker said:*snip*1TBS here, for no particular reason except that I'm too lazy to change. But I can still adapt, when there is a requirement to do so... too bad that the C++ editor in VS doesn't have all the options of the C# one.
This thread is incredibly old, but what the heck. I personally put each brace on its own line. I do this because I find it more visually pleasing (opening and closing braces match up properly), and because the extra whitespace aids legibility for me. I honestly have real trouble reading code where everything is condensed and right on top of each other. I need the space so I can mentally process the structure.
For another controversial topic, MS seems to have changed the preferred whitespace rules for if-statements. All the old MSDN C++ examples that I learned to code from used "if( expression )", while the default settings for the C# autoformatter in VS2008 use
"if (expression)". The same is true for while, for, switch, any control flow statement with brackets. This latter style just looks wrong to me, so I stubbornly keep using the old one. ![]()
Sven Groot said:Blue Ink said:*snip*This thread is incredibly old, but what the heck. I personally put each brace on its own line. I do this because I find it more visually pleasing (opening and closing braces match up properly), and because the extra whitespace aids legibility for me. I honestly have real trouble reading code where everything is condensed and right on top of each other. I need the space so I can mentally process the structure.
For another controversial topic, MS seems to have changed the preferred whitespace rules for if-statements. All the old MSDN C++ examples that I learned to code from used "if( expression )", while the default settings for the C# autoformatter in VS2008 use "if (expression)". The same is true for while, for, switch, any control flow statement with brackets. This latter style just looks wrong to me, so I stubbornly keep using the old one.
I think I adopted my brace style from Flash's ActionScript: back in the Flash 5/MX days you 'wrote' code by literally dragging and dropping keywords and method names from the library on the right into the editor window, although it did have an "Expert Mode" where you could type it. Because all editing was done with the mouse it meant the Flash AE enforced its coding style, which had braces on the same line as the statement/expression.
...and also slightly from VB, which has its brace-like keywords ("Then", "Begin", etc) on the same line as the statement.
It seems Microsoft's style is to put braces on the same line for control flow statements, but different lines for declarations (classes, methods, etc). To me this just seems inconsistent.
Later in January I'll be working on a team project as part of my CS course, fortunately Java seems to have standardised on 1TBS, which closely matches what I do anyway, so I don't need to worry about people complaining about my coding style.
And those wondering about my style:
Note that I don't always indent a block when it otherwise ordinarily would, for instance if an entire method's body is wrapped in a using(){} or lock(){} I wouldn't give it the extra indent.
Sven Groot said:Blue Ink said:*snip*This thread is incredibly old, but what the heck. I personally put each brace on its own line. I do this because I find it more visually pleasing (opening and closing braces match up properly), and because the extra whitespace aids legibility for me. I honestly have real trouble reading code where everything is condensed and right on top of each other. I need the space so I can mentally process the structure.
For another controversial topic, MS seems to have changed the preferred whitespace rules for if-statements. All the old MSDN C++ examples that I learned to code from used "if( expression )", while the default settings for the C# autoformatter in VS2008 use "if (expression)". The same is true for while, for, switch, any control flow statement with brackets. This latter style just looks wrong to me, so I stubbornly keep using the old one.
Oh, whilst we're at it, I think this is a pretty clever syntax hack:
int x = 100;
while( x --> 0 ) {
// do something
}
W3bbo said:Sven Groot said:*snip*Oh, whilst we're at it, I think this is a pretty clever syntax hack:
int x = 100;
while( x --> 0 ) {
// do something
}
it's not, and it's ugly
Ion Todirel said:W3bbo said:*snip*it's not, and it's ugly
No, he's right: it's a clever syntax hack. However, quality code should not be clever, it should not be a hack. It should be clean and easy to understand/maintain.
TommyCarlier said:Ion Todirel said:*snip*No, he's right: it's a clever syntax hack. However, quality code should not be clever, it should not be a hack. It should be clean and easy to understand/maintain.
We should all put more effort in trying to make our code more readable and maintainable... out of respect for those who will have to maintain it a few years later. Working on someone else's code is not a very exciting experience to begin with, at least we should try hard to prevent it from being miserable.
Hacks are fascinating (and so is obfuscated code if you are so inclined), but they are dangerous. In the case at hand, it might not be immediately evident the difference between:
while (x-->0) and while (0<--x)
Use both in the same stretch of code and you can be sure to get enough curses to last you for the next decade.
W3bbo said:Sven Groot said:*snip*Oh, whilst we're at it, I think this is a pretty clever syntax hack:
int x = 100;
while( x --> 0 ) {
// do something
}
It's even slower than incrementing until you reach 100...
littleguru said:W3bbo said:*snip*It's even slower than incrementing until you reach 100...
Yea at my company we are required to do same line curlies and an if/else with a single statement must be enclosed in curlies.
Personally, I find those two requirements interesting: The reason why they want curlies on a a single statement line is because you "might" add a second statement later, and thus you might mistakenly not add the required braces at that time. OK, now for my comment on that: I have never in my life made that particular mistake, since the IDE automatically indents the second statement in such a way that it is painfully obvious that the second statement is not part if the if/else.
On the other hand, I have looked at other's code with multiple nested blocks of code, each with its own set of curly braces (opening on the same line), and made many mistakes due to not being able to clearly match up the braces. Sometimes I even have a hard time finding the damn opening brace in complex code.
That is why I am scratching my head over those two different coding style requirements. The point is that I am way more likely to make mistakes with same-line braces than with a single statement after if/else that has no curly braces.
And every single example of same-line opening brace that everybody provided in this thread use only very simple code. What about when you start getting to complex code? For me at least, readability goes down the drain quickly at that point.
Just saying.
PS: I did find though that the closer you can get the code together, the faster the application runs...
for (int i=0; i<x; i++)
DoStuff();
for (int i=0; i<x; i++) {
DoStuff();
DoMoreStuff();}
DoOtherStuff(); // You can tell you're out of the loop by indentation. People underutilize indentation.
in C# I do
if (blah)
{
}
else
{
}
Which I guess is the "Java" syntax which seems to be commonly used in C#.
Basically curly bracket on each line:
in C I do
if (blah) {
} else {
}
Which is the Linux/K&R syntax used in most POSIX software.
Basically I just follow the convention I see in most C# code, and most C code. Although, Mono C# code has it's own distinct style which is based on K&R syntax. To some C# developers reading Mono code looks almost like an alien language. ![]()
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.