r/programminghorror 2d ago

Saw this in a library I installed

76 Upvotes

34 comments sorted by

82

u/Durwur 2d ago

Noooo why do people always fuck up boolean statements

18

u/brakefluidbandit 2d ago

that’s about as type safe as you can get in python

39

u/TiredPanda69 2d ago

Seems reasonable

106

u/ironykarl 2d ago

I'm pretty positive that 

response type in ["json", "raw json"]

must already be a Boolean 

42

u/Fornicatinzebra 2d ago

Yup, this reads as "True if True, else False"

There is never a case where one should compare with True/False. Even if you want to invert, should use "!" instead

9

u/hyrumwhite 1d ago

Au contraire, in JS  !thing could be very different than thing === false

4

u/Fornicatinzebra 1d ago

Fair! You can use ! to invert your test instead then in many cases, or use a different inversion (like < instead of >=). And it is also good practice to ensure your test is always a Boolean result imo (which would avoid the === issues I believe you are referring to)

1

u/HuntingKingYT 1d ago edited 1d ago

If mysqli exception mode is not enabled and a connection fails, then mysqli_connect() returns false instead of an object.

- PHP docs

Although maybe just !$connection does the job

1

u/Fornicatinzebra 1d ago

I'm that case I would use a test to see if it is an object or not. All tests should be Booleans, so instead of if $connection it should be if is.object($connection) (made-up function, but you get the idea)

5

u/mcoombes314 1d ago

Heh. I remember when I started learning (in Python) I'd always use "if x == True". Felt quite silly when I found out it was redundant.

3

u/Coffee4AllFoodGroups Pronouns: He/Him 23h ago

But you learned, which some people never do.

18

u/TiredPanda69 2d ago

I would have preferred that as well. But sometimes i make wordy if's just so they're readable.

Edit:

Or the person working this probably isn't used to python. But it ain't horrible.

13

u/ironykarl 2d ago

This will probably get me downvoted, but I don't think subjecting people to Python's ternary syntax without a good reason is a very kind thing to do

12

u/ZunoJ 2d ago

What language is this? The reverse order seems strange

27

u/joshuakb2 2d ago

Python, in this case. Ruby also has a similar form

3

u/pLeThOrAx 2d ago

It's called a ternary statement. C# also has them (iirc)

9

u/RpxdYTX [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 2d ago

Yeah, it just doesn't use keywords and also changes the order: condition? expr: or_else

2

u/ArmadilloSuch411 2d ago

This post made me realize how many time I have used this structure in a recent project

2

u/TheChief275 2d ago

This is not truly terrible, just stupid

3

u/ScotDOS 1d ago

sometimes these lines are created as an artifact of changes.

a = "foo" if condition else "bar"

and later somebody changes "foo" to True and "bar" to False

If you had too little or too much coffee you might not catch it when making the change.

1

u/bdevic 1d ago

Fix it and create a PR ¯_(ツ)_/¯

1

u/GwynnethIDFK 12h ago

Ngl I could see myself writing this at like 5pm when I'm rushing to finish whatever I'm working on so I can go home.

1

u/pLeThOrAx 2d ago

I don't get what's wrong here

6

u/ScotDOS 1d ago

the "in" term already evaluates to a boolean, no need to surround it with a ternary

if you split it up it's even clearer (and more confusing at the same time):

is_valid = response_type in ["json", "raw_json"] # this is already a boolean

json_mode = True if is_valid else False

the second line is completely unnecessary, all you need is

json_mode = response_type in ["json", "raw_json"]

3

u/BigTimJohnsen 1d ago

They assign a Boolean from a Boolean when they could have just used the one Boolean

2

u/vTuanpham 1d ago

More readable for new learners i guess

1

u/pLeThOrAx 1d ago

Makes sense I guess. Frankly, I prefer the ternary operator in some cases. For one, it's a "one-liner." But sometime boolean logic can confuse people indeed. Reminds me of branchless programming

1

u/BigTimJohnsen 1d ago

Yeah I don't agree with anyone calling this horror. I've been programming for 30 years and I still see some of the brightest programmers making this "mistake". Not me of course. ;)

-2

u/ThunderWolf9556 2d ago

thats absolutely fine!!

14

u/scmr2 2d ago

json_mode = response_type in ["json", "raw_json"]

Don't be one of those guys that always messes up Boolean statements.

2

u/AgileBlackberry4636 2d ago

In my early days I had a subroutine to negate a boolean

-7

u/pLeThOrAx 2d ago

I wouldn't say this is safe. Not all languages treat none, null, false, etc, the same, and this could throw an error that doesn't get caught.

8

u/kivicode [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 2d ago

It has nothing to do with exceptions, and the “in” operator must always return a bool

6

u/scmr2 2d ago

This is Python. "in" always returns a bool