DEV Community

Discussion on: We don't need a ternary operator

Collapse
 
bosepchuk profile image
Blaine Osepchuk

Great post.

As I was reading it, I was just thinking about how messed up programming is.

I personally hate the ternary operator. It's a conditional disguised as an assignment and when I scan code, I often read it wrong. So, we removed it from all our code and created an inspection to make sure it never gets back in.

Totally personal preference here, but an if else is much more readable.

I never even considered all the edge cases you brought up or that it works differently in different languages. Do other programming language developers secretly hate programmers (only half kidding)?

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

If...Else statements are flow though. I don't like using them in situations where I'm alternating between expression values. I let the high-level logic usually decide whether I want an if-else or a conditional-operator. If I use the wrong one it will look odd.

Collapse
 
bosepchuk profile image
Blaine Osepchuk

That's a fair point. In my code base, the previous programmers used them indiscriminately, which was part of the reason we're got rid of them. I've seen that pattern of usage in open source projects too.

If you used them consistently in the manor you've described, I could get on board with it.

Collapse
 
dfeuster profile image
Daniel Feuster

@Blaine I like to think of all the various programming languages as exactly that, languages. Imagine if you will, that C is latin. Its the root of most modern "western" languages (for brevity sake). Each language that sprouts from it has syntax that is similar but more refined and easier to use in particular circumstances that are important to that language. Then within that language itself there are deeper refinements, these are the regional dialects. It's actually quite interesting.

Collapse
 
bosepchuk profile image
Blaine Osepchuk

Yes, I'm aware. The research shows that C-like language syntax is basically as hard to learn as random syntax. That's not good. It makes programming harder than it needs to be. And every time I see some crazy-dense and indecipherable code I wonder what we're really doing here. Are we trying to be cryptographers, jerks, or what?

Programming is already cognitively demanding. We should be saving or brain cycles for our programs, not trying to remember all the obscure edge cases of the ternary operator in C.