Quote:
Originally Posted by Mark Sir Link
it depends on the compiler but I imagine in Graal's case, it's loading the variable (boolean), comparing the operator, then loading the global (TRUE), adding an extra step or two
it's true for variables that aren't obviously booleans (like possibly even "on") that evaluating like that makes the intention much clearer, but you could try naming the var something like "isOn", "isActive", etc
|
To get technical, GS2 doesn't have booleans (
true is a compile-time constant for
1). The
if statement accepts an integer (coercing anything else to an integer), and executes the first branch for any non-zero integer, and the second otherwise.
Some useful snippits supporting this:
PHP Code:
temp.x = "test" == "test";
echo(temp.x.type()); // type 0, an integer
echo(temp.x) // 1
// ---- //
echo(true.type()); // type -1, because true is rewritten at compile-time, meaning the real expression is (1).type(), which evaluates to -1
// ---- //
if (2) {
echo("success"); // it echos, meaning that the first branch executes for any non-zero integer
}
Checking something like
if (temp.x == 1) would be useful in the case that
temp.x can be
2 or
3.
However, in the case that
temp.x will only ever be
0 or
1, it would be a useless operation. Look at how it evaluates:
When temp.x = 1:
NPC Code:
if (temp.x == 1) => if (1 == 1) => if (1)
When temp.x = 0:
NPC Code:
if (temp.x == 1) => if (0 == 1) => if (0)
So in this case that
temp.x is either
0 or
1, that
== 1 is redundant.
My styling recommendation would be to do
if (temp.x) in the case that you assuming
temp.x is limited to
0 and
1, but
if (temp.x == 1) in any other case.
The message being that we don't need to restrict our styling to GS2's type system, because it is not strongly enforced and we can be more expressive by breaking these bounds.
Note: Technically, GS2 doesn't even have integers, it only has floats. But that's a headache for another day...