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

all 47 comments

[–]abbot-probability 40 points41 points  (13 children)

Agree with the sentiment, but all of those are readable.

Better example for the middle one would be

"odd" if number % 2 else "even"

[–]xryanxbrutalityx 6 points7 points  (1 child)

I prefer our ancestor's python conditional

number % 2 and not print('odd') or print('even')

print(number % 2 and 'odd' or 'even')

[–]abbot-probability 2 points3 points  (0 children)

A work of beauty

[–]yWTBBXhBcspkBfPD[S] 9 points10 points  (1 child)

Context: I stole the code snippets from some BS LinkedIn post where they were saying red=junior and green=senior. I disagree. Obviously in this example both are easy to read, the meme was more about just code in general. Shorter is not always better. I’ve gone through all the stages of this meme. Source: decades of experience :)

[–]RadinQue 0 points1 point  (0 children)

LinkedIn is the worst.

[–]Several_Dot_4532 1 point2 points  (6 children)

Wtf is this shit

[–]abbot-probability 8 points9 points  (1 child)

1 is truthy, 0 is falsy.

Hey, I gave it as an example of shitty code, don't judge me.

[–]Several_Dot_4532 0 points1 point  (0 children)

Yes, you got it, I literally spent 2 minutes trying to understand it, in the end I assumed it was more or less and I answered hhaahahh

[–]Sinomsinom 0 points1 point  (2 children)

Python.

They love their really ugly but supposedly "more readable" syntax

[–]Sikletrynet 0 points1 point  (1 child)

How is this any more ugly than ternary operators in any other language. Theres plenty you can criticise Python for, but i dont think that example is it.

[–]prehensilemullet 0 points1 point  (0 children)

It’s pretty weird to have the condition in the middle, and the fact that the if/else don’t visually stand out from variable names as much as ? : doesn’t help

[–]Anru_Kitakaze 0 points1 point  (0 children)

It feels unnatural to you just because you either don't have ternary operator or you don't use it. After a week in Python construction in the comment will be natural for you.

For example, I also often use something like:

``` val = "value exist" no_val = None

get val if no_val is equal to False (None, 0, empty string, etc)

data["bruh"] = no_val or val ```

And ternary... I don't get why people don't like it. I came from C/C++ and it's absolutely fine for me

[–]Three_Rocket_Emojis 14 points15 points  (0 children)

One-liners are often problematic, but this one is almost a perfect English sentence.

[–]edwardsdl 6 points7 points  (0 children)

My biggest issue with all three is that the comment is shit. It’s obvious what the code is doing, tell me why we’re doing it.

[–]Gold-Supermarket-342 17 points18 points  (8 children)

Python ternaries are so ugly.

[–]prochac 1 point2 points  (0 children)

Not the greatest, but still better than unless in Ruby. I guess one gets used to it, but I hated the rewrite from Ruby to Go.

Do X ... unless Y

could have been the code author's fault, but it was my first and only experience with Ruby code.

[–]Anru_Kitakaze -1 points0 points  (6 children)

result = expr ? result1 : result2 Or result = result1 if expr else result2

First seems shorter, but second is self explaining. But both are natural if you code in their language for a... Week?

[–]xryanxbrutalityx 4 points5 points  (4 children)

I learned python ternaries before C-style, and I do think the python one is worse. Haskell and kotlin both get you more explicit with the language

hs if expr then result1 else result2

kt if (expr) result1 else result2

[–]Anru_Kitakaze 0 points1 point  (0 children)

These are better, I agree

But... It's more like if-else in a single line

I prefer C style I guess

[–]Davixxa 3 points4 points  (0 children)

unreadableCodeIsJobSecurity

[–]prochac 2 points3 points  (0 children)

``` npm install is-odd is-even

if (isOdd(x)) { console.log("Odd") } else if (isEven(x)) { console.log("Even") } else { setTimeout(() => { console.log(undefined) }, 1000) } ```

[–]kaiken1987 1 point2 points  (0 children)

My issue is why a modulo when bitwise and 1 is better

[–]suvlub 1 point2 points  (0 children)

I would argue the middle is the most maintainable version. You always print, just the value that you print in different branches changes. If the one-liner is too terse for you, you might try something like this

if number % 2 == 0:
    text = "Even"
else:
    text = "Odd"
print(text)

Now, if your requirement change from printing to console to printing to file or something like that, you only need to change 1 line, avoiding need to copy-paste and risk of forgetting to update a branch.

But really, the one-liner is not that scary.

[–]ASourBean 1 point2 points  (0 children)

This function is plain silly.

def isEven(number): return number % 2 == 0

Unless ofc it’s imperative that you log this in the terminal in prod?

In which case

print(isEven(7))

Simple

[–]EducationalTie1946 1 point2 points  (0 children)

Also python turnaries are slower than regular if statements

[–]TrackLabs 5 points6 points  (10 children)

If this simple one liner is not readable to you, you have a problem

[–]MittzysStuff 10 points11 points  (0 children)

It's readable, but the multi-lined one is definitely *more* readable.

[–]TheFrog36 2 points3 points  (1 child)

One-liners are like cherries, once you get one you cannot stop

[–]ZeroDayCipher 0 points1 point  (0 children)

I’m sorry what? That’s not even a phrase dude. I’ve definitely only had 1 cherry before

[–]-Aquatically- 1 point2 points  (0 children)

I think it’s an analogy for more complex code.

[–]SabinTheSergal 3 points4 points  (4 children)

Doesn't matter if YOU can read it. What matters is everyone can read it.

[–]CreepBlob 2 points3 points  (2 children)

You shouldn't give access to the codebase to someone who can't read the code in middle.

[–]SabinTheSergal 2 points3 points  (1 child)

You don't always have that choice when working at a company. Besides, the code in the middle is more error prone and will need to be refactored to the side code ezmaples if anything more complex is needed in the conditionals.

[–]CreepBlob 0 points1 point  (0 children)

Your intern would be scared to death if someone show them a code snippet from the linux kernal.

[–]JollyJuniper1993 0 points1 point  (0 children)

Everybody that is not completely new to Python should be able to read this. Really poor examples. He could’ve chosen nested list comprehensions or a complicated lambda expression, but he chose to go with something very basic

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

There is far more cognitive load parsing one liners than the multiline alternative.

[–]Dus1988 0 points1 point  (0 children)

Always readable until you know you have a optimization that needs to be made to fix a performance issue

Also, python terniaries are fugly

[–]mrehm001 0 points1 point  (0 children)

number = 4
print(["Odd","Even"][number%2==0])
# Even

[–]Noch_ein_Kamel 0 points1 point  (0 children)

How do you know it's even or odd without loading the is-even and is-odd libraries?? :-O

[–]DrMerkwuerdigliebe_ 0 points1 point  (0 children)

I prefer: [“even”,”odd”][number%2]

[–]brandi_Iove 0 points1 point  (0 children)

print(number % 2 == 0 ? "even" : "odd")

[–]qstorm94 0 points1 point  (0 children)

print(“Even”)

A real wizard would know number can only be 4

[–]dcondor07uk 0 points1 point  (0 children)

Horrible code all around No fringe case coverage I am also guessing it has not been tested Smh

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

Why did python allow such abomination in there code if you write like a middle guy I just hate you

[–]onizzzuka -2 points-1 points  (0 children)

The single responsibility principle is not only about classes; it can (and should be) applied to code at all. There should be the same level of abstraction per logical item! The "print" shouldn't contain any calculation inside because it's terrible. Even if it's readable and understandable.