Hacker News new | past | comments | ask | show | jobs | submit login

>type-checking can help

If we use Python as a strongly typed language, it makes no difference which one you use.

If we don't (i.e. use Python as it is: a dynamically-typed language), then this is just a preference.

Using (or exploiting, depends on how you think) Truthiness this way is actually an intentional choice in lots of case, especially if you have "else" condition.

Think it this way: you're going to split the conditions into two: `users` is non-empty, which is the "good" condition; and `users` is empty, which is the "bad" condition.

Then you have unexpected condition that "users" is something that shouldn't be, most commonly being None. In most of cases, this is a "bad" condition. So it makes sense it's grouped together with `users == []`.

If `users` is "True" or "False" as you said (which you should ensure to not happen in other ways anyway), then indeed it will not be captured by `users == []`, but it would still be broken/unmanaged in "else" side.




> If we use Python as a strongly typed language, it makes no difference which one you use. If we don't (i.e. use Python as it is: a dynamically-typed language),

Nitpick: Python is strongly typed, it's also dynamic. The strongly-weakly typed axis is different from the static-dynamic axis.


Thanks for letting me know


> Think it this way: you're going to split the conditions into two: `users` is non-empty, which is the "good" condition; and `users` is empty, which is the "bad" condition.

In a lot of cases though, an empty collection isn't a "bad" condition at all, e.g. it's a valid collection to apply filters/maps to.

Similarly when people get used to doing "if not i" for ints, but then forget about the times that zero is a valid value.

It's true that dynamic coercion is a feature of the language, but coding conventions generally are often about enforcing "least surprise" to remove a burden from the person reading the code.


> an empty collection isn't a "bad"

Then you don't need to check if it's empty to begin with.


I think they're making the distinction between special cases and error cases.

An empty list could be valid(e.x. a search of users providing no results) so you still need to differentiate between "special cases that need special logic" and "bad input".

Lumping the two together in one `if` block makes any code less readable imo, because they're not the same thing.


Yes agreed but the trouble being that if you see "if not users", you've to second guess the intent of behind it.

Is an empty list being routed to the else branch because it is an error in this instance or because it's an error in 90% of the codebase so the author forgot to handle it explicitly here?

Or is the author always expecting users will be a full or empty list and that other falsy values will never occur?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: