orbit82
orbit82

Reputation: 513

Should I replace all my if/else statements with ternary shorthand if/else? (PHP)

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!

Upvotes: 3

Views: 1657

Answers (10)

KDM
KDM

Reputation: 1

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.

Upvotes: 0

Michael Irigoyen
Michael Irigoyen

Reputation: 22947

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.

Upvotes: 7

ChrisLively
ChrisLively

Reputation: 88072

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.

Upvotes: 0

guiman
guiman

Reputation: 1334

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

Upvotes: 0

Fake Code Monkey Rashid
Fake Code Monkey Rashid

Reputation: 14567

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.

Upvotes: 3

Amir Raminfar
Amir Raminfar

Reputation: 34169

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.

Upvotes: 1

Crozin
Crozin

Reputation: 44386

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.

Upvotes: 6

Jason McCreary
Jason McCreary

Reputation: 73011

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.

Upvotes: 1

girasquid
girasquid

Reputation: 15526

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.

Upvotes: 0

Babiker
Babiker

Reputation: 18798

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

Upvotes: 0

Related Questions