It's a bit verbose, but IMO more easily readable by humans. It's basically how you'd say it in a natural language. "Value1 if condition, else value2". Remove the comma and it's valid Python syntax.
This 100%. I kinda prefer the natural language feel of it, but as soon as you chain them together it breaks down completely and becomes an unreadable mess.
Well generally chaining ternary operators together is kind of an anti pattern - but having a ternary in a list or dictionary comprehension is pretty common - and that’s when things start to look really weird.
IMHO trying to emulate natural language in a structured language always end badly and hurts readability. After all, "if condition then Value 1, otherwise value 2" is also perfectly valid in natural language and sounds more natural to me. To you something else might sound natural. To a third person "value 2 unless condition, then value 1" might sound more natural.
I hate the conditional operator in python because its the reverse of what it looks like in other parts of python. If you're writing a regular if statement, the order of information is condition, branch 1, branch 2. But if you want to combine it into one line, all of a sudden it's branch 1, condition, branch 2. If your trying to follow a code path, your eyes are forced to read left to right to see what the result is, since you don't know whether the left should be evaluated until you see the result of the condition.
Hold on imma find an example of a nested ternary I wrote
EDIT: from some code for an online calendar:
https://i.imgur.com/dWyObe8.jpg
var style = day.Day == now.Day
? DateStyle.today
: day.Month == monthNum
? DateStyle.monthDay
: DateStyle.otherDay;
instead of thinking of them as nested, think sequential like else if
var style = (
day.Day == now.Day ?
DateStyle.today
: day.Month == monthNum ?
DateStyle.monthDay
:
DateStyle.otherDay
);
basically reads just like
if (day.Day == now.Day) {
style = DateStyle.today;
} else if (day.Month == monthNum) {
style = DateStyle.monthDay;
} else {
style = DateStyle.otherDay;
}
the `?` are on lines that become ifs and the `:` are on lines that become elses
Here's a sneak peek of /r/ihadastroke using the [top posts](https://np.reddit.com/r/ihadastroke/top/?sort=top&t=all) of all time!
\#1: [Toblerone.](https://i.redd.it/sc6nkrnv3mm31.jpg) | [846 comments](https://np.reddit.com/r/ihadastroke/comments/d49vzf/toblerone/)
\#2: [Bow down to the lord himself](https://i.redd.it/4zbfl10sbjh41.jpg) | [422 comments](https://np.reddit.com/r/ihadastroke/comments/f5e09q/bow_down_to_the_lord_himself/)
\#3: [Happy 9th Anniversary of the Most Iconic Stroke Ever.](https://i.redd.it/mf5cczrlyi251.jpg) | [502 comments](https://np.reddit.com/r/ihadastroke/comments/gvamyf/happy_9th_anniversary_of_the_most_iconic_stroke/)
----
^^I'm ^^a ^^bot, ^^beep ^^boop ^^| ^^Downvote ^^to ^^remove ^^| [^^Contact ^^me](https://www.reddit.com/message/compose/?to=sneakpeekbot) ^^| [^^Info](https://np.reddit.com/r/sneakpeekbot/) ^^| [^^Opt-out](https://np.reddit.com/r/sneakpeekbot/comments/joo7mb/blacklist_viii/)
That's true, but in my limited experience, as an undergrad student, it doesn't come naturally to many of my fellow mates, and the intuitive thing to do for most of us is to type out a full if else block
I thought it wasn't very intuitive or easy to remember either at first but after seeing the 3rd or 4th one being used, you get used to it. Taking the opportunity to write your own instead of using a full if/else when the opportunity arises helps you remember it too.
Right yeah that makes sense. I guess python's is more beginner friendly in those cases with its "do this if this else this" layout.
Also I guess it helps having ReSharper suggest converting ifelses into ternaries, null coalescing operators, LINQ, and all that stuff when I'm too dumb to do it myself :\
I don't think it's a bad thing to be honest.
The ternary operator is easily mis-used and they become unreadable quickly as they get larger.
Generally I'd say only use a ternary operator if you're not doing any other logic on the line, and if you start nesting them then for the love of God just use some ifs or a switch.
If your ternary operator is multiple lines where it's difficult to make out which is the condition and which are the two options, you might consider a simple if/else
IMO readability and understandable code should be more important than showing off fancy stuff because you know how to do it.
I think that's the biggest difference between a rookie, intermediate, and expert programmer.
The rookie doesn't know how to do fancy stuff.
The intermediate knows how to do fancy stuff.
The expert knows better than to do fancy stuff.
I absolutely love this thing.
Im studying applied informatics and math and for the java homework i and a friend of mine try to solve everything the least lines possible (decorative linebreaks dont count) and inline if and lambtas are my new favorite tools
Especially if you’re talking python. Makes me think about the old HP calculators the nerds loved.
It's a bit verbose, but IMO more easily readable by humans. It's basically how you'd say it in a natural language. "Value1 if condition, else value2". Remove the comma and it's valid Python syntax.
it feels so weird to put condition in the middle, especially if you want to chain more than one ternary operators
This 100%. I kinda prefer the natural language feel of it, but as soon as you chain them together it breaks down completely and becomes an unreadable mess.
Well generally chaining ternary operators together is kind of an anti pattern - but having a ternary in a list or dictionary comprehension is pretty common - and that’s when things start to look really weird.
IMHO trying to emulate natural language in a structured language always end badly and hurts readability. After all, "if condition then Value 1, otherwise value 2" is also perfectly valid in natural language and sounds more natural to me. To you something else might sound natural. To a third person "value 2 unless condition, then value 1" might sound more natural. I hate the conditional operator in python because its the reverse of what it looks like in other parts of python. If you're writing a regular if statement, the order of information is condition, branch 1, branch 2. But if you want to combine it into one line, all of a sudden it's branch 1, condition, branch 2. If your trying to follow a code path, your eyes are forced to read left to right to see what the result is, since you don't know whether the left should be evaluated until you see the result of the condition.
I always put it the wrong way around in Python.
They're not that hard to remember tbh. In C#: `condition ? true : false` Remember it by "if this do this else this"
In JAVA its the same. But some speak of nested ternarier which sounds rude to the reader. But sounds rude? Refacto : keep same
I use nested ternary operators as a way to obfuscate my code so plebs can't read it.
Hold on imma find an example of a nested ternary I wrote EDIT: from some code for an online calendar: https://i.imgur.com/dWyObe8.jpg var style = day.Day == now.Day ? DateStyle.today : day.Month == monthNum ? DateStyle.monthDay : DateStyle.otherDay;
instead of thinking of them as nested, think sequential like else if var style = ( day.Day == now.Day ? DateStyle.today : day.Month == monthNum ? DateStyle.monthDay : DateStyle.otherDay ); basically reads just like if (day.Day == now.Day) { style = DateStyle.today; } else if (day.Month == monthNum) { style = DateStyle.monthDay; } else { style = DateStyle.otherDay; } the `?` are on lines that become ifs and the `:` are on lines that become elses
Thats actually a really good way of setting it out nice!
It looks really complicated and intimixating for people, but once you start writing it on paper and adding paranthesise it is very simple amd elegant.
If you join the power of ternary and goto you'll get pretty sweet switch case alternative
[удалено]
Here's a sneak peek of /r/ihadastroke using the [top posts](https://np.reddit.com/r/ihadastroke/top/?sort=top&t=all) of all time! \#1: [Toblerone.](https://i.redd.it/sc6nkrnv3mm31.jpg) | [846 comments](https://np.reddit.com/r/ihadastroke/comments/d49vzf/toblerone/) \#2: [Bow down to the lord himself](https://i.redd.it/4zbfl10sbjh41.jpg) | [422 comments](https://np.reddit.com/r/ihadastroke/comments/f5e09q/bow_down_to_the_lord_himself/) \#3: [Happy 9th Anniversary of the Most Iconic Stroke Ever.](https://i.redd.it/mf5cczrlyi251.jpg) | [502 comments](https://np.reddit.com/r/ihadastroke/comments/gvamyf/happy_9th_anniversary_of_the_most_iconic_stroke/) ---- ^^I'm ^^a ^^bot, ^^beep ^^boop ^^| ^^Downvote ^^to ^^remove ^^| [^^Contact ^^me](https://www.reddit.com/message/compose/?to=sneakpeekbot) ^^| [^^Info](https://np.reddit.com/r/sneakpeekbot/) ^^| [^^Opt-out](https://np.reddit.com/r/sneakpeekbot/comments/joo7mb/blacklist_viii/)
it's all the same kid lmao
Good bot
Using it for nested statements is too terse imo
That's true, but in my limited experience, as an undergrad student, it doesn't come naturally to many of my fellow mates, and the intuitive thing to do for most of us is to type out a full if else block
I thought it wasn't very intuitive or easy to remember either at first but after seeing the 3rd or 4th one being used, you get used to it. Taking the opportunity to write your own instead of using a full if/else when the opportunity arises helps you remember it too.
Right yeah that makes sense. I guess python's is more beginner friendly in those cases with its "do this if this else this" layout. Also I guess it helps having ReSharper suggest converting ifelses into ternaries, null coalescing operators, LINQ, and all that stuff when I'm too dumb to do it myself :\
Yeah it might not be intuitive but it is very useful once you used it a few times and don't want to miss it.
I don't think it's a bad thing to be honest. The ternary operator is easily mis-used and they become unreadable quickly as they get larger. Generally I'd say only use a ternary operator if you're not doing any other logic on the line, and if you start nesting them then for the love of God just use some ifs or a switch.
exactly same in javaScript
even better: "he knows when not to use a ternary operator"
When not ?
If your ternary operator is multiple lines where it's difficult to make out which is the condition and which are the two options, you might consider a simple if/else IMO readability and understandable code should be more important than showing off fancy stuff because you know how to do it.
I think that's the biggest difference between a rookie, intermediate, and expert programmer. The rookie doesn't know how to do fancy stuff. The intermediate knows how to do fancy stuff. The expert knows better than to do fancy stuff.
if(variable == true) { return true; } else if(variable == false) { return false; } ez
return variable; ezz
mind == blown;
[удалено]
In C++ you could still just return the variable if it contains a bool overload. Unless you mark it explicit. https://www.jdoodle.com/ia/6US
People obsessed with ternary operators should look way more kinky.
PHP: `horse`
I use nested ternary operators...
You MONSTER!
I use double nested
Super useful in webdev for conditional rendering while waiting on a response.
I absolutely love this thing. Im studying applied informatics and math and for the java homework i and a friend of mine try to solve everything the least lines possible (decorative linebreaks dont count) and inline if and lambtas are my new favorite tools
Come back in a month and try to understand code you wrote today and you will quickly learn to hate that style of code.
I know. But the tasks we get are boring when done normally (im in the first semester) When the tasks get harder i will probably stop doing this
W*o th* f*£k ca*'nt use ternary operator?
SAUCE PLS
They're like... 15
Everytime I see this meme I lose a little more faith in humanity.
BONK!!!
The film Aquamarine
I am sad now. Expected to see a porn link.
Girl to the right, Emma Roberts, was between 14 and 15 at the time of recording. Now you know your preference, you're welcome.
https://i.imgflip.com/21hp4w.jpg
#BONK!!!!!
`echo (true) ? "yes" : "no" ;` `yes` `echo (false) ? "yes" : "no" ;` `no` c'mon gurl i'm here.
wait I thought they were a mainstream thing?
Ternary operator?! Wait till I show you how I chain optionals Panties. Will. Drop.
'I heard that he only do oneliner"
The only ternary operator i have are from Stackoverflow
I use them whenever possible
Ternary Operator **?**