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!

2
  • 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. Dec 22, 2010 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: stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary
    – webbiedave
    Dec 22, 2010 at 20:06

10 Answers 10

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.

2
  • 2
    +1 Although I'm not sure about "future predecessors". I think that would be called a "successor". ;)
    – netcoder
    Dec 22, 2010 at 19:19
  • Whoops! Thanks for catching that! Dec 22, 2010 at 19:21
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.

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.

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.

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.

0

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

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.

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

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.

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.

1
  • 1
    Have you seen that the question is about PHP?
    – kapa
    Oct 27, 2012 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.