- Home /
How does Unity reads the "If" conditions?
Hello guys, I'm trying to optimize my game, and I do a lot of conditions like this in the update function:
if(boolean == true && variable > 5)
Let's say that the boolean is false. I assume that unity will not even check the variable, because the boolean is false, am I right?
Or I have to do in separate If's?
if ( boolean == true )
{
if (variable > 5)
{}
}
this second case, i'm sure that unity won't check the variable, but most of my code I wrote usign the first exemple to prevent poluting the lines... I'm asking this, because i saw lots of scripts that people wrote just like the second exemple, and I want to know wich is better, because i want unity to avoid checking unnecesary variables every time.
Answer by robertbu · Dec 12, 2013 at 05:32 AM
Both C# and Javascript use short-circuit boolean. That means that if the first condition is false, the second will not be executed. Short-circuit boolean is done in a majority of languages, but it's not true of all programming languages.
Note you don't have to do 'boolean == true' you can just 'boolean' So you code would be:
if (boolean && variable > 5)
Note you can test a language by using a function that returns a boolean in an 'if' statement:
function BoolTest() : boolean {
Debug.Log("Testing");
return false;
}
Then to test:
if (BoolTest() && BoolTest()) {
//
}
If you only get one 'Testing', then it is Short circuit boolean.
It's useful to know this, because it can help in optimising programs later. For example, if you have a condition like this:
if(someList.Contains(someElement) && someBool)
it may run slower than this:
if(someBool && someList.Contains(someElement))
in the average case. This is because someList.Contains
is an expensive operation, while boolean equivalence testing is not. By putting the cheaper operations first, you reduce the number of times the more expensive ones are evaluated, which can save some time. Having said that, it is not always possible to do this if the more expensive operation has side effects.
Answer by KellyThomas · Dec 12, 2013 at 05:35 AM
"&&
" and "`||`" are shortcut boolean operators, working from left to right the program will only evaluate as much as is strictly necessary.
The Wikipedia page on "short circuit evaluation" will go into further depth.
If you wanted to evaluate the expression completely (e.g. to trigger side effects) then you could use "`|`" and "`&`".
update:
Generally speaking chaining functions with side effects into complex boolean statements is discouraged. Separating functions that query state from methods that change state will usually result in code that is easier to understand and maintain.
However as an example I offer:
bool strengthRoll() {
if (Random.Range(0,11) < 8 ) {
strengthXP++;
return true;
}
return false;
}
bool dexterityRoll() {
if (Random.Range(0,11) < 8 ) {
dexterityXP++;
return true;
}
return false;
}
//then in the update:
if (buttonPressed()) {
if (strengthRoll() & dexterityRoll()) {
//special attack
}
}
Here each of the stat rolls has the sideeffect of incrementing a separate XP counter.
strengthRoll
and dexterityRoll
are joined by the "`&`" so they will both be called even if the first returns true, as a result their XP will increase evenly.
If they were joined by the "`&&`" then dexterityRoll
would only be called when strengthRoll
returns true, in that scenario dexterityXP
would increase at a lower rate to strengthXP
.
As you can see the impacts of operator selection can have subtle effects on program behavior. While it is important to be able to recognize it when reading other peoples code , I would try to favor a more overt style when writing. One of best measures of code quality is that it does what you expect of it.
Thanks for the awser! But in that case, can you give me a exemple in wich its better to use "&" ins$$anonymous$$d of "&&"? I can't think of any!
Answer by YoungDeveloper · Dec 12, 2013 at 06:25 PM
This also might bu useful: http://answers.unity3d.com/questions/537514/if-else-how-the-program-reads-it.html
Your answer
Follow this Question
Related Questions
ANOTHER Boolean Problem 1 Answer
Store Multiple values in one variable 2 Answers
Set a global bool to true? 1 Answer