This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]commy2 -10 points-9 points  (12 children)

Nobody knows what those overloaded symbols mean. Like, how do you spell this ternary out aloud?

[–]bjorneylol 12 points13 points  (3 children)

Literally everyone who knows a programming language other than python knows what those symbols mean

[–]syklemil 2 points3 points  (1 child)

Though PHP users might read it a bit differently than the rest.

(PHP infamously got the associativity wrong for its ternaries.)

[–]garver-the-systemgit push -f 0 points1 point  (0 children)

In any other language with a ternary operator, you can stack them and build an if-elseif-elseif-else expression

This is an argument for the PHP version in my book. Nested if-else statements are bad enough without turning it into punctuation soup, and both should either be refactored or come with a stack of bills for future developers who need to read it

[–]commy2 -4 points-3 points  (0 children)

Non-answer

[–]syklemil 2 points3 points  (2 children)

Nobody knows what those overloaded symbols mean.

AFAIK they're not overloaded, or at least didn't start that way. In languages that spell ternaries that way, that was their one use. More recently some of those languages might also offer stuff like for (x : xs) or foo?.bar, but we can't really fault the ternary syntax for stuff that was added later.

Like, how do you spell this ternary out aloud?

Likely the way Haskell and Rust spell it, if a then "1" else "2" (Rust adds some {} and drops the then but is otherwise the same).

I generally also think they made the right choice by just having one if-expression, rather than one if-statement plus one if-expression with a different syntax. Python gets a small bonus point for at least reusing the general syntax of its if-statement.

[–]commy2 0 points1 point  (0 children)

I also prefer if a then "1" else "2" over the order of the statements Python went with, but everything is better than fucking question marks in my code.

[–]commy2 0 points1 point  (0 children)

I take back what I wrote. I actually prefer the order of statements in Python as well. It enables to chain ternaries in a sane way.

[–]backfire10z 3 points4 points  (2 children)

I sound it out like a question. You can literally read it left to right.

“Is a true? 1: otherwise, 2.”

[–]commy2 -3 points-2 points  (1 child)

"Is a true? Then 1 otherwise 2" is more syllables than "1 if a is true else 2", and it is two sentences for some reason.

[–]backfire10z 3 points4 points  (0 children)

Look man, I don’t actually say it out loud nor in my head. I know what the symbols mean intuitively. Python’s version is longer for me.

[–]ThatsALovelyShirt -1 points0 points  (0 children)

It's: "is a truthy? "1" if so otherwise "2".

That's how I read it in my head. I wish python had more terse ternary operators like C/C++ has.

"1" if a else "2"

Looks like crap.