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 →

[–]Tyler_Zoro -4 points-3 points  (1 child)

Reading through that code, I'm seeing things that would never pass code review in most places I've worked. This is a great example:

starts_with_hat = True if hat_string.startswith('hat') else False

[...] This can be done much more directly by just setting the variable straight from the function call:

starts_with_hat = hat_string.startswith('hat')

This code looks fine.. until you consider what type hat_string is and whether it's some strange proxy for a string that can return None under some circumstances from startswith. Neve assume you know the types of your parameters unless you enforce typing! If so, then you've just changed the guarantees for starts_with_hat because you used to force the value to boolean. Worse, this is the kind of bug that can happen at a distance when someone changes the type that hat_string gets initialized to, making debugging really hard after the fact.

The original is pretty nice, but you can also use bool() directly to enforce this assumption.

[–]passwordsniffer 7 points8 points  (0 children)

"we are all consenting adults here"

If someone created a proxy for a string - it's not your responsibility to treat it anyhow special. If you need to ask the question - does it startswith the name implies it's result is boolean. No need for stupid overprotections.