Vice City: Multiplayer

VC:MP Discussion => General Discussion => Topic started by: Milos on Apr 25, 2016, 05:39 PM

Title: Ternary operator or if/else?
Post by: Milos on Apr 25, 2016, 05:39 PM
Which is better?
Ternary have any advantage? It's faster than if/else?

if ( player.FPS.tointeger() >= 60 ) PrivMessage( player, "Nice FPS! :D" );
else PrivMessage( player, "Good FPS! :)" );
//-------------------------------------- or --------------------------------------
PrivMessage( player, format( "%s", player.FPS.tointeger() >= 60 ? "Nice FPS! :D" : "Good FPS! :)" ) );
Title: Re: Ternary operator or if/else?
Post by: Coolkid on Apr 25, 2016, 06:48 PM
I think if/else is better but I think there would be a timer used if you want to use it as function without command
Title: Re: Ternary operator or if/else?
Post by: DizzasTeR on Apr 26, 2016, 01:43 AM
Obviously ternary operator whenever you seem to find it not a hassle to use.
Title: Re: Ternary operator or if/else?
Post by: . on Apr 26, 2016, 08:29 AM
It's just a matter of personal taste. You can chose to use whichever you want. However, I recommend choosing them wisely.

Take the following example:
if (age > 18)
{
    print("your age is safe");
}
else
{
    print("your age is unsafe");}
}

The code could be be made to look more compact if that's what you wish:
print("your age is " + (age > 18 ? "safe" : "unsafe"));
Don't underestimate the ternary operator and use it if you must but don't abuse it where it's not necessary. Most likely, the ternary operator generates the same bytecode as the if/else statement. So like I said, it's only e mater of personal taste.
Title: Re: Ternary operator or if/else?
Post by: Milos on Apr 26, 2016, 02:18 PM
Ok

Thanks to all for answers 8)