I do like Ternaries a lot, but you have to evaluate each in context: Does it help readability and organization to have ?: instead of "if"? If you can make code more compact without hurting readability, please do.
Me too- I never nested them anyway (!) but now I've had to replace them with a normal if too many times.
I can clearly see op needing to do more than change the styleClass based on whether the element is selected and changing this into if rather than having multiple ternaries, because I've been there 100 times...
Your problem is the lack of brackets more than anything. If statements written that way can also be confusing. if(this.isSmart) if(this.isUseful) return "great"; else return "useless"; else return "horrible";
Readability first. Remeber you read code 1000 times more than you write. Code is not good because it looks smart or because it is compact. In fact i would argue that compact code is never a goal. You make standards in your codebase that adheres somewhat to best practice and be consistent in that. Of course thereās a sweetspot you need to find, but its a team decision. but maybe this example will do:
You can compact your code enough so for example you could make it into a oneliner and thus spare away the curlybraces if you are evaluating something inside an if statement. Then you have your ifstatement followed by a one-liner. That could be a ternary operator, but would that make your code more readable? My bet is no, but more compact. Instead you extract your evaluations away into methods where such name is human readable. You incoorporate the domain into the code so it becomes understandable. Itās easier to follow a requirement written as a message call rather than some lambda expression or ternary operator.
Another one is variable names. Var p = 42; what the f is p? Just write the full domainname and get it overwith. Less compact. More readable. After all you write the code once, and read it 1000 times over the years to follow.
Good points, all. I should have been more clear when I said "compact". What I mean is that it's generally easy to understand a block of code (method, class, etc) if you can fit it all neatly on your screen together. As soon as something is off your screen you are depending on your memory, and there's only so many ideas that you can keep in your working memory at once.
So, readability being absolutely the #1 concern, it can help in some cases to compact code a little bit so you can fit more onto the screen at once. You have to determine on a case-by-case basis if it helps or not. I frequently do an "if to ?:" refactoring, and then Ctrl+Z to roll back and Ctrl+Y to roll forward again, seeing which I like better. Sometimes the ternary wins.
Also it's worth iterating here that readability is determined by people who didn't write the code, so even if I think something seems readable to me, it might not be readable to others. I usually like to go through some kind of code review or solicit some feedback to see if changes actually are improvements.
Your ternaries are wrong... why are you writing condition1 ? condition 2 ? expr1 : expr 2 : expr3, nesting conditionals? Just write it like a series of if-elses: condition1 ? expr1 : condition2 ? expr2 : expr 3. Read it as is this true ? then do this : or is this true ? then do this : otherwise do this.
I assume you wanted [2,3,1] to be sorted, and then for each integer to be converted to the corresponding digit character. In that case, this is what you want:
[2,3,1].sort((x,y) => x < y ? -1 : x > y ? 1 : 0).map(x => x == 1 ? '1' : x == 2 ? '2' : '3');
Seems perfectly legible to me š¤·āāļø It's effectively a functional programming style. You'd do things like this all the time in something like Haskell:
map (\x -> if x == 1 then "1" else if x == 2 then "2" else "3") $ sortBy (\x y -> if x < y then LT else if x > y then GT else EQ) [2,3,1]
Haskell syntax is intended to resemble mathematical function notation, so function composition usually ends up looking like C code rather than OOP code, i.e. using the names intToString and standardComparator in place of the lambda expressions:
If you like, you can fairly easily change this to resemble a piping style using (|>) and (&), which allows you to write f x (applying the function f to x) as x |> f or x & f (thought of as passing x through f). The difference between them is that (|>) is right-associative, whereas (&) is left-associative, which allows you to use them both to avoid using parentheses when piping multiple arguments through multiple functions. However, their use is not common, and for polyadic functions, they effectively flip the order of arguments, requiring you to use (|>) for each argument. This is a consequence of (|>) expecting only a single argument to the function, and currying.
So using them, you could flip the entire expression and write:
[2,3,1] |> (\x y -> if x < y then LT else if x > y then GT else EQ) |> sortBy & (\x -> if x == 1 then "1" else if x == 2 then "2" else "3") |> map
That doesn't read particularly well at all, though; we might like to write f x y as y |> x |> f, but we usually prefer to write f (g x) or f $ g x as x |> f g rather than x |> g & f, and thus f1 g1 $ f2 g2 $ f3 g3 x as x |> f3 g3 & f2 g2 & f1 g1, thus only using (|>) for the actual data. This yields something resembling OOP code:
[2,3,1] |> sortBy (\x y -> if x < y then LT else if x > y then GT else EQ) & map (\x -> if x == 1 then "1" else if x == 2 then "2" else "3")
Not a video that's really relevant to our discussion, but it's about some of the mathematical concepts relevant to functional programming, and I thought the title was appropriate š
It's technically not repeated, in that the two occur on opposite sides of the first ternary statement. That being said, they should absolutely refractor this so that the categoryName ternary is appended to the result of the first ternary.
Sorry, but I fail to see how that would mean that they're technically not repeated. They're literally the same expression, and evaluate exactly to the same thing. I didn't mean that they're repeated in the sense that both sides of the expression would evaluate to the same result (ie: 1 || 1), but just that they're the same expression repeated at different places.
I love them, but usually as an easy one liner if/else.
Didnāt really learn about them until I did a hardware descriptive language course, because all previous professors just said āDonāt use the ternary operator, theyāre bad practiceā
Ternaries are a thing where few people set out with "I'm going to use a multi path nested ternary to get the result".
Its more a "oh, it's just one condition. A ternary is great! Oh we need another conditional check? Uh, I guess we can just add in another ternary..." And so on
66
u/Gem2578 Feb 22 '22
One of the Ternary Operators can be removed by resolving one part at a time
87
u/wknight8111 Feb 22 '22
I do like Ternaries a lot, but you have to evaluate each in context: Does it help readability and organization to have ?: instead of "if"? If you can make code more compact without hurting readability, please do.
14
u/ftgander Feb 23 '22
Just needs some line breaks.
24
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 22 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
11
u/dc0650730 Feb 23 '22
Looks like the top answer on any algorithm practice site
22
u/anth096 Feb 22 '22
Thatās the only good way to use ternary operators. Use it to make code hard to refactor so it never changes.
10
u/turtle_genie Feb 23 '22
I like them, but I refuse to do nested ternaries
8
u/Ellasandro Feb 22 '22
This is my coworkers ordinary coding style X-(
4
u/suppow Feb 23 '22
What offends me the most is that
this.categoryName === "Attract Loop" ? "attract" : ""is repeated twice.3
u/andyinnie Feb 23 '22
Not anymore
3
u/CrazySD93 Feb 23 '22
I love them, but usually as an easy one liner if/else.
Didnāt really learn about them until I did a hardware descriptive language course, because all previous professors just said āDonāt use the ternary operator, theyāre bad practiceā
3
u/lookatmycode Feb 23 '22
Pros don't use braces.
3
u/nan-000 Feb 23 '22
/r/badScreenshots
2
u/AlexLee1995 Feb 23 '22
You can like them without liking this
2
u/Jomy10 Feb 23 '22
I like ternary operators actually, but you should know when to use them
2
u/kristallnachte Feb 23 '22
Ternaries are a thing where few people set out with "I'm going to use a multi path nested ternary to get the result".
Its more a "oh, it's just one condition. A ternary is great! Oh we need another conditional check? Uh, I guess we can just add in another ternary..." And so on
2
u/taichouk Feb 23 '22
Not when theyāre used like this
2
u/FaeTheWolf Feb 23 '22
This isn't programming horror, just poorly designed code.
The "correct" way to do this would be
This is an incredibly trivial change (factoring out one repetition via parenthesis), that immediately clarifies the intent of the code.
1
u/[deleted] Feb 23 '22
Is there any real reason that in languages like C++ some consider the ternary operator to be bad?
1
u/MisterOnsepatro Feb 23 '22
I use them for simple conditions in angular templates
1
u/[deleted] Feb 23 '22
Welcome to terraform
1
u/ivannovick Feb 23 '22
I liked it before this
1
u/IAlwaysFeelFlat Feb 23 '22
You could use this for captchas
1
u/causticacrostic Feb 23 '22
ah, i remember writing stuff like this in college. i thought it was cool
1
u/the_monkey_of_lies Feb 23 '22
Why use many line when one line will do trick?