...

View Full Version : Advantage of the ternary operator instead of the if statement



Glass Casket
08-15-2006, 12:52 PM
Like the topic says, is there a time when you should use the ternary operator instead of the if statement? I've Googled a bunch, but can't find a good answer to my question.

Thank you.

Mwnciau
08-15-2006, 01:44 PM
Basically use it if you are assigning a string to a variable based on if something is true or false.

Kid Charming
08-15-2006, 03:05 PM
Personally, I don't like them that much (I find if's more readable), but I do occasionally use them. It's more about personal preference than best practice.

NancyJ
08-15-2006, 03:09 PM
I use them whenever possible, once you understand them they're far more readble than if {}else{}. I dont know about performance wise but they're much easier and neater to type ;)

Fumigator
08-15-2006, 03:11 PM
Yeah KC, all you have to do is understand them and you'll like em better! :) haha.

I don't like them either; if's are more readable to me. Just a personal preference is all.

NancyJ
08-15-2006, 03:24 PM
Yeah KC, all you have to do is understand them and you'll like em better! :) haha.

I don't like them either; if's are more readable to me. Just a personal preference is all.
They just take getting used to. Your fingers will love you for it ;)

Kid Charming
08-15-2006, 03:28 PM
I'm offering no rational reason for disliking them; I just do. They're too...crowded for my tastes.

I do tend to use them when pulling values out of a POST form, so I can do some basic validation at the same time.

Glass Casket
08-16-2006, 03:22 PM
Thanks for all of your replies. :)

I'll just stick with what I was using them for!

felgall
08-16-2006, 09:29 PM
The two ways of coding are equivalent. Which you use would be a matter of personal preference. The ternary operator version is shorter and so is faster to type and would be about 0.0005 milliseconds faster to process due to being faster to read in (since it is shorter).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum