Ternary operator or if/else?

Started by Milos, Apr 25, 2016, 05:39 PM

Previous topic - Next topic

Milos

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! :)" ) );

Coolkid

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

DizzasTeR

Obviously ternary operator whenever you seem to find it not a hassle to use.

.

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.
.

Milos