Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Does Unity use short circuiting for || and && operators?

Discussion in 'Scripting' started by Nyanpas, Mar 15, 2020.

  1. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Greetings, I have just read this article in particular here:

    https://stackoverflow.com/questions/4820610/is-relying-on-short-circuiting-safe-in-net

    This makes me wonder if Unity follows this exact pattern, and if I can use this in my code, to do all inexpensive checks first and if they pass do the more expensive ones:

    Code (CSharp):
    1. // Will this stop at CheapCheck() if it evaluates to true?
    2. bool example1 = CheapCheck() || ExpensiveCheck();
    3.  
    4. // Will this continue from CheapCheck() if it evaluates to true?
    5. bool example2 = CheapCheck() && ExpensiveCheck();
    Thank you very much in advance for helping out. ;w;
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,454
    Last edited: Dec 27, 2021
    Bunny83, Nyanpas and SparrowGS like this.
  3. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Thank you. I am also a little concerned since apparently the C#-level/.NET-standard outside Unity is higher and it also seems Unity perhaps will use less of that and more of its own gonig forward with regards to the DOTS-oriented technological system.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,925
    The more common use of short-circuit is to fake a series of IF's where the early one's need to be true to avoid an error in the latter:
    Code (CSharp):
    1. if(i>=0 && i<A.Length && A[i]>0) x=A[i];
    2.  
    3. if(p==null || p.idDead()) p=getNewTarget();
    If you want to check in your examples, add some Debug lines and see if they print.
     
    Nyanpas likes this.
  5. JaredBGreat

    JaredBGreat

    Joined:
    Oct 28, 2017
    Posts:
    13
    The answer seems to be no -- it would if it's C# was completely standard, but it is not.

    Code (CSharp):
    1.                 if ((health != null) && CheckSight(collider))
    2.                 {
    3.                     health.BeHitByPhysicalAttack(damage);
    4.                 }
    Will call CheckSight(collider) even if health is null (tested, yes, it does).

    A bit surprising and disappointing when I found out -- but not too surprising, since I already know Unity doesn't support updated .NET standards, so that some data structures are not available.
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    StarManta and Bunny83 like this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    This conclusion doesn't make much sense :)

    First of all Unity does use standard C#, just not the latest standard. The Unity ecosystem does have some counter-intuitive structures, however they are purely implemented though normal C#. So as exiguous said, it's probably the overloaded == operator for UnityEngine.Object derived types.

    We don't know what "health" is in your code. If it's a type derived from UnityEngine.Object the overloaded == operator should apply. However if it's an interface type it would not.
     
    VolodymyrBS and exiguous like this.
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,925
    In other words, your test probably gave funny results because the first clause was too complicated -- !=null is tricky in Unity. For a real test try:
    Code (CSharp):
    1. int n=8;
    2. if(n>10 && checkSight(collider)) print("will never get here");
    3.