locked
If Statement Block of code best practice RRS feed

  • Question

  • I have a silly question but I am now paying attention to the small stuff because I plan on getting certified by the end of this year or sometime next year.

    I read its best practice to use opening and closing brackets for if statements in the event you or someone else adds code later.  This makes sense to me but I do not remember where I read this but when re-factoring code you can uses compound statements and omit the brackets to make code look cleaner.  Which method is best practice according to Microsoft?

    // THIS if(boolean) // DO something else // OR ELSE

    // OR THIS if(boolean) {

    // DO Something } else {

    // OR ELSE }


    Friday, May 31, 2013 1:03 PM

Answers

  • Its just the argument if you want simple readable code or a short one. Its always a good thing to keep it simple and add the braces than break your head over how to turn everything into one line and make it impossible to read. At least thats my opinion.

    Please Mark as Answered If this answers your question Or UnMark as Answered if it did not.
    Happy to Help :)
    My Site

    • Proposed as answer by CoolDadTx Friday, May 31, 2013 2:08 PM
    • Marked as answer by LluvianBlack Friday, May 31, 2013 4:31 PM
    Friday, May 31, 2013 1:13 PM
    • Marked as answer by LluvianBlack Friday, May 31, 2013 4:32 PM
    Friday, May 31, 2013 1:36 PM

All replies

  • Its just the argument if you want simple readable code or a short one. Its always a good thing to keep it simple and add the braces than break your head over how to turn everything into one line and make it impossible to read. At least thats my opinion.

    Please Mark as Answered If this answers your question Or UnMark as Answered if it did not.
    Happy to Help :)
    My Site

    • Proposed as answer by CoolDadTx Friday, May 31, 2013 2:08 PM
    • Marked as answer by LluvianBlack Friday, May 31, 2013 4:31 PM
    Friday, May 31, 2013 1:13 PM
  • Problems arise when one forgets and removes the braces and expects the second statement to be executed with the first...but it is not. When in doubt, place the braces, specially in tricky if statements.

    if(boolean) {

    // DO Something } else {

    // OR ELSE }

    Friday, May 31, 2013 1:23 PM
  • Problems arise when one forgets and removes the braces and expects the second statement to be executed with the first...but it is not. When in doubt, place the braces, specially in tricky if statements. 

    if{
    //Do Something
    }
    else{
    //Do Something
    }


    Friday, May 31, 2013 1:26 PM
  • Problems arise when one forgets and removes the braces and expects the second statement to be executed with the first...but it is not. When in doubt, place the braces, specially in tricky if statements.

    Friday, May 31, 2013 1:27 PM
    • Marked as answer by LluvianBlack Friday, May 31, 2013 4:32 PM
    Friday, May 31, 2013 1:36 PM
  • There is no specific standard or best practice for this.

    There may be so many arguments.

    As vijaykamat said, consider readability

    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Friday, May 31, 2013 1:50 PM
  • This seems just to be a matter of preference or style to development rather than best practice.  I can see the argument for using either.
    Friday, May 31, 2013 4:33 PM
  • This seems just to be a matter of preference or style to development rather than best practice.  I can see the argument for using either.

    The two main arguments FOR the usage of braces, at least in C#, in my mind are:

    1) This is the standard used by MSDN, and many/most of the C# books, so it'll make the code more consistent with a lot of sample code.

    2) Tools like StyleCop (http://stylecop.codeplex.com/) enforce always using braces, which means there is a tool you can use to force consistency in the code.

    That being said, it is purely a style preference.  I personally prefer to use braces, as I don't mind a little extra typing for the clarity they bring, but that's a personal preference.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, May 31, 2013 4:42 PM
  • Dude are you still using If Then?

    JP Cowboy Coders Unite!

    Friday, May 31, 2013 4:46 PM