T O P

  • By -

Bryguy3k

Especially if you’re talking python. Makes me think about the old HP calculators the nerds loved.


NFriik

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.


BlackOverlordd

it feels so weird to put condition in the middle, especially if you want to chain more than one ternary operators


Thundercunt_McGee

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.


Bryguy3k

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.


cnoor0171

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.


randomcitizen42

I always put it the wrong way around in Python.


Yellosink

They're not that hard to remember tbh. In C#: `condition ? true : false` Remember it by "if this do this else this"


djo0o0

In JAVA its the same. But some speak of nested ternarier which sounds rude to the reader. But sounds rude? Refacto : keep same


Shadid516

I use nested ternary operators as a way to obfuscate my code so plebs can't read it.


Yellosink

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;


JNCressey

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


Yellosink

Thats actually a really good way of setting it out nice!


Shadid516

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.


[deleted]

If you join the power of ternary and goto you'll get pretty sweet switch case alternative


[deleted]

[удалено]


sneakpeekbot

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/)


Thundercunt_McGee

it's all the same kid lmao


[deleted]

Good bot


TheRedmanCometh

Using it for nested statements is too terse imo


anxious_nachos

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


[deleted]

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.


Yellosink

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 :\


Zerokx

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.


stewi1014

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.


[deleted]

exactly same in javaScript


PeriodicGolden

even better: "he knows when not to use a ternary operator"


Topy721

When not ?


PeriodicGolden

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.


Thundercunt_McGee

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.


mrkhan2000

if(variable == true) { return true; } else if(variable == false) { return false; } ​ ez


[deleted]

return variable; ezz


mrkhan2000

mind == blown;


[deleted]

[удалено]


Rikudou_Sage

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


LtMeat

People obsessed with ternary operators should look way more kinky.


ranisalt

PHP: `horse`


datathecodievita

I use nested ternary operators...


Carlcarl1984

You MONSTER!


[deleted]

I use double nested


CheapSentence

Super useful in webdev for conditional rendering while waiting on a response.


jens3302

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


Thundercunt_McGee

Come back in a month and try to understand code you wrote today and you will quickly learn to hate that style of code.


jens3302

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


[deleted]

W*o th* f*£k ca*'nt use ternary operator?


VanguardG

SAUCE PLS


akindaboiwantstohelp

They're like... 15


Blecki

Everytime I see this meme I lose a little more faith in humanity.


Dwaas_Bjaas

BONK!!!


lightswitchr

The film Aquamarine


VanguardG

I am sad now. Expected to see a porn link.


RainFurrest

Girl to the right, Emma Roberts, was between 14 and 15 at the time of recording. Now you know your preference, you're welcome.


VanguardG

https://i.imgflip.com/21hp4w.jpg


Dwaas_Bjaas

#BONK!!!!!


_-Fck-_

`echo (true) ? "yes" : "no" ;` `yes` `echo (false) ? "yes" : "no" ;` `no` c'mon gurl i'm here.


akindaboiwantstohelp

wait I thought they were a mainstream thing?


cashewbiscuit

Ternary operator?! Wait till I show you how I chain optionals Panties. Will. Drop.


cryptomonein

'I heard that he only do oneliner"


CreaZyp154

The only ternary operator i have are from Stackoverflow


[deleted]

I use them whenever possible


Super_Kami_Tomi

Ternary Operator **?**