3

I was reading about ternary shorthand if/else and am wondering if it would make sense or be more beneficial to replace all (or most) of my traditional if/else statements with the ternary shorthand? Would that make it run faster? Or would the benefits all lie in the fact that it's less code?

Thanks for your insights!

orbit82
  • 513
  • 3
  • 6
  • 17
  • 2
    There are no benefits in the ternary operator. As well as there is no benefits in the apple over an orange. It's just damn another way to do it. Don't be a fool, stop looking for the "perfect" method of doing something. There are not such a thing in the world. – Your Common Sense Dec 22 '10 at 19:03
  • 4
    Don't mind Col. Shrapnel. His parents used to lock him in the closet for hours on end. For best use of ternary, check out this post: http://stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary – webbiedave Dec 22 '10 at 20:06

10 Answers10

7

Ternary if/then statements should be used for basic logic when there is a clear cut choice either way. When you start to nest ternary if/then statements, you're just making more headaches for yourself (or your future successors) when changes need to be made or bugs need to be fixed.

As for a speed boost, you will not see a difference between ternary or normal statements. You will however shave a few bytes off of the file size, though it is probably not enough to be remotely obvious.

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
6

That would be pointless. Ternary is useful only in very "primitive" conditions and most of the ifs are more complicated.

You'll lose a lot of time to gain absolutely nothing.

Crozin
  • 43,890
  • 13
  • 88
  • 135
3

Code is written for people, not computers and some people find use of the ternary operator to be hard to read.

Personally, I think as a programmer you would want your emphasis to be on readability.

Fake Code Monkey Rashid
  • 13,731
  • 6
  • 37
  • 41
1

No, they are compiled/interpreted into the same code.

In the end, all you will accomplish is making your code harder to read and less maintainable.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
1

There is no performance gain at all. Its really what's easier to read. If you have a bunch of if-else statements then it would be easier if you leave it that. Ternary statments are usually used for condition assignments like $var = $bool ? "IF TRUE" : "IF FALSE". If you are doing an if statement to branch execution of code then ternary doesn't really make sense.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
0

Absolutely not, what if you wanted to add additional else ifs? Plus the ternary method actually is not any faster

Babiker
  • 18,300
  • 28
  • 78
  • 125
0

Ternaries don't make your code run faster - they're just a shortcut (and a little harder to read). You should be very careful about replacing if/else statements with ternaries.

If an if/else chain has a single line in each clause (and it isn't another if statement), you can probably safely reduce the code to a ternary and trust that whoever comes after you will be able to understand it. However, if your code has multiple lines or nested if statements, you shouldn't convert those to ternaries - all you're doing is adding complexity in that case.

girasquid
  • 15,121
  • 2
  • 48
  • 58
0

I dont think that changing older ifs would be something i recommend on a working project. Think about backwards-compatibility, if you dont deploy your project on a brand new system, it wont work.

And if your trying to speed up things, i think there are better practice to this matter.

So, to sum up, i think you dont

guiman
  • 1,334
  • 8
  • 13
0

There are well defined instances of when to refactor existing code.

Changing from regular if then statements to ternary is not one of them.

They provide absolutely zero performance benefit and are there purely to help make code more terse and sometimes more readable.

NotMe
  • 87,343
  • 27
  • 171
  • 245
0

This an older conversation, but I'm using VS2010, .NET 4.0 and the ternary operator consistently performs between 18% and 22% better than an equivalent if-then statement.

KDM
  • 1