TheSwamp
Code Red => .NET => Topic started by: MP on April 12, 2011, 11:26:30 AM
-
Hi all -- Long time no see!! Hope everyone is doing well. I've been completely swamped at the new job (started late January) but that's not necessarily a bad thing.
Anyway ...
I've been doing a fair amount of C# programming and I should have asked this simple style question long ago ... do you think it is better to use braces to host child clauses, example an if statement, even if there's only one term? i.e. the braces are superfluous?
if ( some_condition )
{
do_this();
}
else
{
do_that();
}
versus ...
if ( some_condition )
do_this();
else
do_that();
I prefer the brevity of the second version and if I were the only one to maintain the code I wouldn't ask but I'll be turning the source code over to others down the road. Would the former version be preferred? Is there an industry norm?
Thanks for your thoughts.
Michael.
-
Nice to hear from you Michael - I hope all is well with you :-)
FWIW, when studying C, I was taught to leave them out since they weren't needed - but it might've just been a personal preference of my lecturer...
-
I prefer the second. And if the statement is short, then I'll put them on a single line. You can do this with ' foreach ' statements also. Just an FYI.
if ( XrLock == null ) XrDb.Dispose();
else XrLock.Dispose();
foreach ( KeyValuePair<string, AnnotationScale> kvp in ScDict ) ScCntxCol.RemoveContext( kvp.Key );
Good to hear you are busy with work Michael.
-
I personally think the difference is rather irrelevant, and it's perfectly fine to use whichever one you desire...
-
I realize this isnt answer isnt C# nor a direct answer but i was reading this doc this morning (I may have to go get those books) and i thought it made some very good points to consider (as well) which could help answer via CODE FLOW solutions.
http://llvm.org/docs/CodingStandards.html#hl_earlyexit
However, to answer the question (directly); I would vote #2 but i dont see any problem with #1 either.
-
I use both.
But lately started to leave the braces, since end up adding more lines in between, something that can be done very easy later, but also to keep all on the same.
But that's me.
-
I use #1 all the time, even for 1 liners. Usually by the time I think to remove the braces, im further down the road and it doesn't matter
-
Most of the time I to use #2 (as I don't use a progn in LISP when it's not requiered).
But these days, I tend to replace braces with identation using F#. :evil:
-
I use both.
But lately started to leave the braces, since end up adding more lines in between, something that can be done very easy later, but also to keep all on the same.
But that's me.
No 1 for the same reasons as Luis, it just becomes habit anyway and if they're not there they are a pain to put in later...but that's just me.
For .net I use BOO as often as I can, no braces required :)
-
For the most part I don't use the extra braces, however, I keep them symmetrical I.e.
if (some_condition)
{
do_this();
do_this_too();
}
else
{
do_that();
}
over
if (some_condition)
{
do_this();
do_this_too();
}
else
do_that();
-
Hello Michael,
personal preference is the decider for me too.
For the most part I don't use the extra braces, however, I keep them symmetrical I.e.
Ditto, but different :)
if (some_condition){
do_this();
do_this_too();
}else {
do_that();
}
If the conditional action is a simple one liner, I don't use braces.
If there is a chance that I'll need to add statements I use braces at the outset
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if( !bt.Has(blockName) ) {
ed.WriteMessage("\nBlock '{0}' is not available in the current drawing.", blockName);
return;
}
PromptPointResult ppr = ed.GetPoint("\nSpecify insertion point: ");
if( ppr.Status != PromptStatus.OK )
return;
get { return doc; }
set
{
doc = value;
if( doc == null ) {
db = null;
ed = null;
} else {
db = doc.Database;
ed = doc.Editor;
}
}
-
Thanks for the well wishes and feedback folks. I guess personal preference > industry norm. Having said that the minimalist approach in concert with symmetry personally appeals to me the most. Braces and statements on their own lines provide the best clarity to me as well, especially when scanning / perusing pages and pages of code. Thanks for the discussion.
-
Hi, Michael
I have read in "Best code practice" article , don't remember where is it,
but written by one of the giants ( I think by Petzold, not sure about )
"Better yet to use the brackets always"
Have a nice all
Oleg
-
I find when going over code I haven't worked on in a while, its easy to skip right over a single line of unbraced "else" code when there is a large block of "if-positive" code right above it. For internal consistency I only omit the braces where both are single-statements. If either require multiple lines they both get braced.
-
I purchased 'The Elements of C# Style' (http://www.amazon.com/Elements-C-Style-Kenneth-Baldwin/dp/0521671590) a few years ago and it's an excellent book - I generally live by it when writing C# code.
As far as this discussion is concerned, Rule No. 12 is invoked (this is an extract) - 'Always use Block Statements in Control Flow Constructs' to avoid the 'dangling else' problem:
if (x >= 0)
if (x > 0) PositiveX();
else // Oops! Matches most recent if!
NegativeX();
Good to hear from you again MP.
Cheers,
Glenn
-
I purchased 'The Elements of C# Style' (http://www.amazon.com/Elements-C-Style-Kenneth-Baldwin/dp/0521671590) a few years ago and it's an excellent book - I generally live by it when writing C# code.
As far as this discussion is concerned, Rule No. 12 is invoked (this is an extract) - 'Always use Block Statements in Control Flow Constructs' to avoid the 'dangling else' problem ...
I forgot about that book; good example too. Thanks Glenn. :)
I find when going over code I haven't worked on in a while, its easy to skip right over a single line of unbraced "else" code when there is a large block of "if-positive" code right above it. For internal consistency I only omit the braces where both are single-statements. If either require multiple lines they both get braced.
Solid approach, thanks dgorsman. :)
Hi, Michael
I have read in "Best code practice" article , don't remember where is it, but written by one of the giants ( I think by Petzold, not sure about )
"Better yet to use the brackets always"
Following sage Petzold advice could never be a bad thing (very much enjoy his writing too). :)
Bottom line ... whatever scheme one employs ... do it consistently.