35

I'm looking for reasons to use/not to use it and for original ideas (in their use and to replace them).


Duplicate:

Related (but does not address the question being asked):

Community
  • 1
  • 1
  • 2
    what next? usefulness of if statement? – SilentGhost Mar 29 '09 at 15:36
  • Sure it is, just replace Ternary with whatever. – Samuel Mar 29 '09 at 15:39
  • 1
    @SilentGhost and Samuel: I don't agree. Nobody really consideres if/then/else a bad practice while very often I hear people discouraging the use of ternary operator. –  Mar 29 '09 at 15:47
  • All coding practices can be abused. From conditional operators. to ifs, to loops, and gotos. – Samuel Mar 29 '09 at 15:49
  • People will argue about anything, given the chance. I've heard arguments against the use of if/else, switch, while, return... Thing is, there are folks who will take any possible scenario where problems could result as evidence that the associated construct must be abandoned. – Shog9 Mar 29 '09 at 15:51
  • @Samuel: Please note the conventions for marking duplicates. http://stackoverflow.com/questions/610728/how-to-handle-duplicate-questions – Bill the Lizard Mar 29 '09 at 15:51
  • @the post closers: I was waiting for my reply, that is "tell me a way to replace it". If you read Wikipedia, there's a link to a Python workaround, for example. I was looking for similar ideas but you decided to close this question. –  Mar 29 '09 at 15:57
  • @vyger: We linked to a duplicate, which has a lot of answers. – Bill the Lizard Mar 29 '09 at 16:02
  • @Bill: but you haven't neither given the time to reply... and I only partially see the "duplicate"... anyway, the Q/A website is yours... –  Mar 29 '09 at 16:04
  • @vyger: There are two edits to your question. One giving a link to a duplicate and one giving a link to a related question. Up in the body of the question. – Bill the Lizard Mar 29 '09 at 16:07
  • @vyger: If your question gets closed as duplicate there *should* always be a link to the original question, either in the comments or as an edit to the question. It's only common courtesy for us to make sure you know where to go to get your answer. – Bill the Lizard Mar 29 '09 at 16:09
  • @Bill: none of them is what I was looking for. –  Mar 29 '09 at 16:11
  • @vyger the first link should give you what you are after (there are some examples of ?: and ways around it). The second link is useless to answering your question however. Can you say why the first link doesn't help? – TofuBeer Mar 29 '09 at 16:21
  • @vyger: if you read the linked questions, you'll find what you're asking, and more. Not everyone agrees with your premise, and if/else is a pretty obvious replacement for those that do. – Shog9 Mar 29 '09 at 16:31

5 Answers5

63

For the sake of readability, I only use a ternary if it fits into one 80-char line.

Ted Dziuba
  • 2,495
  • 1
  • 22
  • 16
11

Good for short tags in templating languages like PHP, e.g.:

<input type='radio' name='gender' value='m' <?= ($gender == 'm') ? "checked" : "" ?>>Male
<input type='radio' name='gender' value='f' <?= ($gender == 'f') ? "checked" : "" ?>>Female

Good for switches in JavaScript / jQuery:

let el = $("#something");
$(el).is(':visible') ? $(el).hide("normal") : $(el).fadeIn("normal");

Good for assignments, especially where a particular variable name can take different types:

$var = ($foo->isFoo()) ? 'Success!' : false;
informatik01
  • 16,038
  • 10
  • 74
  • 104
karim79
  • 339,989
  • 67
  • 413
  • 406
9

The conditional ternary operator can definitely be overused, and some find it quite unreadable. However, I find that it can be very clean in most situations that a boolean expression is expected, provided that its intent is clear. If the intent is not clear, it is best to use a temporary variable with a clear name whose value is assigned using an if-statement, or to use a function with a good name that returns the expected value.

Dustin Campbell
  • 9,747
  • 2
  • 31
  • 32
5

It's something like the for loop. Makes sense for what it's made for but when you try to stick more stuff in it, it becomes unreadable.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
-1

Which ternary operator are you talking about?

A ternary operator is any operator that takes three arguments.

If you're talking about the ? : operator, this is called the conditional operator. I can't live without it anymore, personally. If - else statements look so messy to me, especially when doing a conditional assignment.

Some complain that it looks messy, but it is still possible (especially if using Visual Studio or another intelligent-formatting IDE) to make things easily readable, and you should be commenting all your conditionals anyway.

informatik01
  • 16,038
  • 10
  • 74
  • 104
JoshJordan
  • 12,676
  • 10
  • 53
  • 63
  • 7
    It's usually called the ternary operator because it's the only operator that takes three arguments. – Samuel Mar 29 '09 at 15:43
  • I'm talking about ? : operator (I always called it ternary operator: am I wrong?) –  Mar 29 '09 at 15:49
  • 5
    +1 for accuracy. Yes, it's called the conditional operator. It happens to be *a* ternary operator, but that only describes the number of operands, not its purpose/behaviour. If the C# team every introduces another ternary operator, people are really going to have to learn the right name... – Jon Skeet Mar 29 '09 at 15:50
  • 21
    Commenting ALL your conditionals? Really? – Restore the Data Dumps Mar 29 '09 at 16:05
  • Yes, commenting all your conditionals. It is one thing to read through one logical flow of a method and find out what it does, but ascertaining WHY something happens is much harder, and that question comes up much more with conditionals. – JoshJordan Mar 29 '09 at 17:48
  • 29
    Many years ago, I used to comment each of my if statements. I felt it made the code easier to read... and it did to some extent. I changed my thinking, when I found a better way. _Clean Code_ book by Robert C Martin essentially says name every method, variable name very clearly, and use small functions. When applying this, my need for comments was reduced and I feel my code is even more readable! – Michael R Jun 25 '15 at 15:56