1

I am reading source codes written in a game. Some lines are written as follows:

0 ? player.y > global.screenHeight/2 : global.screenHeight/2 - player.y

Assume that player is a sprite with position and global is just an import from other files which contains some properties. What does the above code do? I thought the ternary operator will be something like this:

c ? a : b 

where a and b are of the same type and c is the condition.

But the game demo runs smoothly so the above code should be fine. I just don’t get the meaning of the code.

The code is extracted from here:
https://github.com/huytd/agar.io-clone/blob/master/src/client/js/app.js

dakab
  • 5,379
  • 9
  • 43
  • 67
newguy
  • 5,668
  • 12
  • 55
  • 95

3 Answers3

1

In the above ternary statement 0 is the condition.

Because Javascript treats 0 as falsy, the statement is evaluated as if written as:

false ? player.y > global.screenHeight/2 : global.screenHeight/2 - player.y

Therefore, global.screenHeight/2 - player.y will be returned.

It is possible the author put the ternary there to act as an on/off switch. By replacing the 0 with a 1, the ternary statement would return player.y > global.screenHeight/2 instead.

Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
  • So this `player.y > global.screenHeight/2 ` is useless ? Why would it be written like this? – newguy Jul 19 '16 at 04:48
  • 1
    Maybe the author wanted to keep a switch for testing etc, so that the author could replace the `0` with a `1` and have the other statement be returned instead. – Moishe Lipsker Jul 19 '16 at 04:51
  • 1
    It should also be added that in `c ? a : b`, `a` and `b` *do not* need to be the same type since JavaScript is [loosely typed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Dynamic_typing). In fact in this case it will either return a number (or `NaN`) if the condition is `false` or a boolean if the condition is `true` because `player.y > global.screenHeight/2` will evaluate to a boolean. I think this is actually a bug in that code, the author probably accidentally swapped the condition and the "if true" expression. – Useless Code Jul 19 '16 at 09:16
  • Using a ternary as an on/off switch is kind of an interesting idea. I normally would just write it as two lines and comment one out because: a) unnecessarily evaluating the condition could cause a slight performance hit and more importantly: b) it makes the intent of the code more clear to anyone reading it. That said, point a might be moot because most modern JS engines would probably optimize away the unreachable code path since the condition is a primitive. – Useless Code Jul 19 '16 at 09:20
0
c ? a : b

a and b can evaluate to values of any type, and c will evaluate to true or false.

0 will evaluate to false, "b" will run: global.screenHeight/2 - player.y This will happen regardless of anything else - the first expression will never run.

If I had to guess why that's in there, I'd say it may be a placeholder for a planned feature or improvement (however I obviously can't know for sure).

Useless Code
  • 12,123
  • 5
  • 35
  • 40
VortixDev
  • 965
  • 1
  • 10
  • 23
  • 1
    `0` will not *likely* be `false`, it will **always** be `false` since `0` is a [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) value and will always be coerced to `false`. – Useless Code Jul 19 '16 at 09:26
  • Yeah, only put the likely in there since I haven't used JS in a while and didn't want to assume the behavior (even though 0 should evaluate to false for most uses) – VortixDev Jul 19 '16 at 12:18
0
0 ? player.y > global.screenHeight/2 : global.screenHeight/2 - player.y

Here 0 is false , So the global.screenHeight/2 - player.y will be always executed

Venkat
  • 2,549
  • 2
  • 28
  • 61