TheSwamp

Code Red => .NET => Topic started by: MP on April 12, 2011, 11:26:30 AM

Title: Programming Style, C#
Post 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?

Code: [Select]
if ( some_condition )
{
    do_this();
}
else
{
    do_that();    
}
   

versus ...

Code: [Select]
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.
Title: Re: Programming Style, C#
Post by: Lee Mac on April 12, 2011, 12:00:29 PM
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...
Title: Re: Programming Style, C#
Post by: T.Willey on April 12, 2011, 12:00:47 PM
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.

Code: [Select]
if ( XrLock == null ) XrDb.Dispose();
else XrLock.Dispose();

Code: [Select]
foreach ( KeyValuePair<string, AnnotationScale> kvp in ScDict ) ScCntxCol.RemoveContext( kvp.Key );

Good to hear you are busy with work Michael.
Title: Re: Programming Style, C#
Post by: sinc on April 12, 2011, 12:26:27 PM
I personally think the difference is rather irrelevant, and it's perfectly fine to use whichever one you desire...
Title: Re: Programming Style, C#
Post by: JohnK on April 12, 2011, 12:59:36 PM
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.
Title: Re: Programming Style, C#
Post by: LE3 on April 12, 2011, 01:02:04 PM
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.
Title: Re: Programming Style, C#
Post by: David Hall on April 12, 2011, 02:39:35 PM
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
Title: Re: Programming Style, C#
Post by: gile on April 12, 2011, 04:34:10 PM
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:
Title: Re: Programming Style, C#
Post by: MickD on April 12, 2011, 06:46:52 PM
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 :)
Title: Re: Programming Style, C#
Post by: It's Alive! on April 12, 2011, 06:55:39 PM
For the most part I don't use the extra braces, however, I keep them symmetrical  I.e.

Code: [Select]
                if (some_condition)
                {
                    do_this();
                    do_this_too();
                }
                else
                {
                    do_that();
                }

over

Code: [Select]
                if (some_condition)
                {
                    do_this();
                    do_this_too();
                }
                else
                    do_that();
Title: Re: Programming Style, C#
Post by: Kerry on April 12, 2011, 08:25:27 PM
Hello Michael,

personal preference is the decider for me too.

Quote
For the most part I don't use the extra braces, however, I keep them symmetrical  I.e.

Ditto, but different :)

Code: [Select]

        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

Code: [Select]

        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;


Code: [Select]

        get { return doc; }
        set
        {
            doc = value;
            if( doc == null ) {
                db = null;
                ed = null;
            } else {
                db = doc.Database;
                ed = doc.Editor;
            }
        }




Title: Re: Programming Style, C#
Post by: MP on April 13, 2011, 08:41:58 AM
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.
Title: Re: Programming Style, C#
Post by: fixo on April 13, 2011, 01:30:54 PM
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
Title: Re: Programming Style, C#
Post by: dgorsman on April 13, 2011, 02:44:47 PM
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.
Title: Re: Programming Style, C#
Post by: Glenn R on April 13, 2011, 04:50:06 PM
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:

Code: [Select]
if (x >= 0)
  if (x > 0) PositiveX();
else  // Oops! Matches most recent if!
  NegativeX();

Good to hear from you again MP.

Cheers,
Glenn
Title: Re: Programming Style, C#
Post by: MP on April 21, 2011, 09:23:50 AM
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.