r/badcode • • Jan 23 '23

python So I was coding and wrote this...

Post image
847 Upvotes

102 comments sorted by

View all comments

356

u/PooSham Jan 23 '23

This is a very common pattern by beginners.

127

u/TheXRTD Jan 23 '23

Even as an industry dev I'll still nearly write this when I'm on auto pilot. The only thing that's changed is I've gotten faster at noticing when I do it. Nowadays I'll notice it just as I start to write the value True!

69

u/BenjaminGeiger Jan 23 '23

Same. Except it's usually as a result of:

if condition:
    # print "It's true"
    return True
else:
    # print "Denied!"
    return False

8

u/TheXRTD Jan 23 '23

Ooh that's another good one!

4

u/Jomy10 Jan 24 '23
print(condition)
return condition

6

u/[deleted] Jan 24 '23

[removed] — view removed comment

1

u/nicpro85 Feb 10 '23

true survivor

3

u/ASentientBot Jan 24 '23

This is what I was going to say. I start with some debug output or other code in one of the conditions, then at some point realize I've removed it and I have an Embarrassing Pattern in my code.

4

u/NUTTA_BUSTAH Jan 24 '23

Time to start using linters and type annotations

1

u/TheXRTD Jan 24 '23

I do, but they usually only pick up patterns once they've been fully written.

4

u/stepbroImstuck_in_SU Jan 24 '23

I feel like it’s a very natural pattern to end up with: I wrote down the if statement because I was sure something needs to be checked, and I haven’t yet figured out that just passing the checks value is what I want to do after the check.

3

u/[deleted] Jan 24 '23

I think it’s easier to read code when it’s written basically in English and not code, so I think this is good tho unless it makes the file super long

7

u/TheXRTD Jan 24 '23

Like others have said, the function's return being checked here is ambiguous. It might not be returning a bool, so this is probably okay. But in a lot of cases, the function is more clearly named, so the pattern of just writing return is_foo(bar) is both neat and clear

0

u/[deleted] Jan 24 '23

[deleted] — view deleted comment

17

u/phord Jan 24 '23

The useless else here is just a style thing. Some prefer this for completeness and clarity.

I suppose the op is complaining that it's not simply return login(...). But that presumes that the function always returns a boolean.

4

u/kristallnachte Jan 24 '23

Well, you could cast it.

1

u/chrisnlnz Jan 24 '23

You should cast it either way for readability. Even if login always returns a bool, as a reader you wouldn't know unless you dig deeper (into the login function).

5

u/kristallnachte Jan 24 '23

or using something with strict typing and hover the function to see the types..

2

u/paulstelian97 Jan 24 '23

Static, as Python IS strictly typed. Now I forget if it allows non-booleans as conditions (if it has a concept of truthy vs falsy values).

2

u/johnnymo1 Jan 24 '23

Yes, Python has truthy and falsey values, e.g. empty lists are falsey.

1

u/paulstelian97 Jan 24 '23

Any language with dynamic typing and truthy/falsy values has this kind of code as useful.

1

u/coloredgreyscale Jan 25 '23

Also

If (getBooleanValue() == true) {...}

1

u/AlexAegis Jan 25 '23

It's hard for beginners to grasp that expressions have values. I remember me doing stuff like this too.

You can see this in excel too, instead of composing functions, temporary colums/fields being made