Search Unity

noob question, nested if statement vs

Discussion in 'Scripting' started by Zoneman, Aug 4, 2011.

  1. Zoneman

    Zoneman

    Joined:
    Feb 6, 2010
    Posts:
    42
    Is there a best practice for this situation? They both work, but the "nested if" seems less clear.

    if ((this == true) (thisOther == true)){
    DoStuff();
    }

    Or

    if (this == true) {
    if (thisOther == true){
    DoStuff();
    }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's the same thing, so do what's more clear.

    --Eric
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    lots of nested statements are generally looked down upon. Commonly know as the "arrow(head) anti pattern": http://lostechies.com/chrismissal/2...d-worst-practices-the-arrowhead-anti-pattern/

    you obviously don't want a huge conditional statements either. If you get into the situation where you can't seem to avoid it, there might be an underlining problem with your structure.

    For the record you don't need == true
    Code (csharp):
    1. if (foo  bar) { }
    is a lot cleaner

    You can also invert the whole thing, which some people don't like but can completely remove nesting.

    Code (csharp):
    1. if (!foo) return;
    2. if (!bar) return;
    3. DoStuff();
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Code (csharp):
    1. if (!(gor  gar)) return;
    2. MeGotYou();
    or
    Code (csharp):
    1. if (!gor || !gar) return;
    2. MeGotYou();
     
  5. Deadguy71

    Deadguy71

    Joined:
    Jul 14, 2011
    Posts:
    10
    You missed the two closing brackets.. I don't know if you care, but just in case you were having trouble with that.

    if (this == true) {
    if (thisOther == true){
    DoStuff();
    }
    }
     
  6. Zoneman

    Zoneman

    Joined:
    Feb 6, 2010
    Posts:
    42
    Thanks everyone for your input.
    Eric, I was implying that: Nested "if statements" in this case, is less readable than "comparison logic". I understand that the results will be identical. Just looking for professional direction.

    Tertle Jessy, answers your comments and surplus examples were very helpful, and exactly clarified what I wanted to know : )
    Deadguy thanks for the syntax correction, although, it was just about the concept.

    Just searching for the difference between "style" and "best practices"…

    BTW when will I know when I am no longer a n00b…just kidding...
     
  7. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    Does a conditonal statement check every condition?
    Is this
    Code (CSharp):
    1. if(someVal && slowfunction())
    slower than this
    Code (CSharp):
    1. if(someVal)
    2. if(slowfunction())
    Does changing the order matter? Is
    Code (CSharp):
    1. if(someVal && slowfunction())
    faster than
    Code (CSharp):
    1. if(slowfunction() && someVal)
    What I mean is, if I have 3 conditions in one IF statement all joined with && and some of those are just checking BOOL values from static variables while the others are slower function tests, should I move the BOOL checks to the front (if it matters) or should I use nested IF statements?

    If I then added an OR comparison at the end of multiple AND comparisons, would it have to check all of those before hitting the OR statement because it is at the end? meaning that I should move the quicker ones to the left.
    Code (CSharp):
    1. if(slowfunction() && slowfunction2() || fastfunction())
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I believe you are spending too much time typing. I searched "&&", and the second hit was the one that we're all using. It takes time to type so many thoughts; it is really much faster to just read the answers.
     
  9. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    thanks! I searched for ages and only got confused.