Search Unity

code writing difference

Discussion in 'Scripting' started by Shonysky, Dec 22, 2014.

  1. Shonysky

    Shonysky

    Joined:
    Mar 29, 2014
    Posts:
    44
    what's the difference between writing:

    if (blabla <= 0) {
    //code;
    }

    and:

    if (blabla <= 0)
    //code;

    ?
     
  2. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    No difference aside from if you don't use { and } the if statement will apply to 1 line of code only. If you got the {} it applies to everything in the { }

    It is just a shorthand way to use a if, for when your scope is only 1 line
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    With {}, you can put more than one instruction underneath.
     
  4. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    As a general "best practices" guide, I'd recommend *always* putting braces around the contents of an if statement - even though it's not required if there's only a single line. That way, if you edit the code later and add some additional logic, the braces are already there and things function as intended.
     
    toreau and Kiwasi like this.
  5. Kirk Clawson

    Kirk Clawson

    Joined:
    Nov 4, 2014
    Posts:
    65
    The difference is the second one will eventually cause you numerous hours of tracking down a bug that you could have prevented by using braces in the first place.
     
    toreau, Kiwasi and Crayz like this.
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Nah, I disagree. I often find it quite useful to avoid the curly braces like so:
    Code (csharp):
    1. if (transform.position.y < 50)
    2.    return;
    I often take it to the next level and go all one line:
    Code (csharp):
    1. if (transform.position.y > 50) Die();
    2. SomeStatement();
    It's just personal preference (obviously) because the compiler accepts both ways 100%. It's purely for readability, comprehension, and compactness.

    Another way:
    Code (csharp):
    1. // very readable
    2. if (transform.position.y > 50) return;
    3. if (transform.position.y < -50) return;
    4. if (rigidbody.velocity.y > 100) return;
     
    Last edited: Dec 22, 2014
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Code (csharp):
    1.  
    2. if(tooManyPosts)
    3. UnwatchThread();
    4.  
     
  8. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    The only time I have ever, ever found one line if statements useful and acceptable is when you bail out of a function because of an input parameter:

    Code (CSharp):
    1. // stupid example
    2. bool MethodName(int value)
    3. {
    4. // always false when value is < 0, we dont have
    5. // to do any calculations or anything else.
    6. if(value < 0) return false;
    7.  
    8. // otherwise other stufff...
    9.  
    10. }
    However, I have found these situations very very rare.
     
  9. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    I find myself doing this somewhat often, but even on a single line I'll still use brackets.
    Code (csharp):
    1.  
    2. if(noExecute) { return; }
    3. // do some stuff
    4.  
    I just don't really like the way if statements look without them I suppose.

    Ternary operators are also a type of shorthand for if-else statements, but I think I read somewhere they are a tad bit slower to compute. Regardless though I do like to use the Ternary when possible
    Code (csharp):
    1.  
    2. bool newBool = (integer > 0) ? true : false;
    3.  
     
    Korno likes this.
  10. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Yeah I always use tenary operators instead of traditional if statements when the if statement is only used to assign a value to a variable. Doesnt the compiler just generate an if statement behind the scenes?
     
  11. Kirk Clawson

    Kirk Clawson

    Joined:
    Nov 4, 2014
    Posts:
    65
    The only time I use one-liners is in the case of exception throwing. If I'm returning, I'll use curly braces and put it on the next line. example:
    Code (CSharp):
    1. public bool SomeMethod(string myParam)
    2. {
    3.     if (myParam == null) throw new ArgumentNullException("myParam");
    4.     if (myParam == "...")
    5.     {
    6.         return false;
    7.     }
    8. }