A version of this article originally appeared on my GitHub pages blog
Different languages have different opinions about what to treat as "truthy" or "falsy" when using a non-boolean object as an expression inside an
- Null is always falsy
- Zero and empty strings are falsy, except in Ruby
My observations and personal opinions on language design:
- Python treats zero, empty strings and collections all as 'falsy'. Personally, I find this the most intuitive convention.
- Treatment of zero and null as falsy has historical precedent, from C. False and null pointers are both represented as zeros in a register or memory location.
- Treatment of empty strings and collections is a nice convenience, given the number of times I've written conditionals like
if (foo != null and !foo.empty()). It's usually the exception that I want to distinguish between null and empty in a conditional. So it's nice that
if (foo)handles the common case, then I can write
if (not foo is None)when I really do want to distinguish null.
- Treatment of empty string as similar to null feels familiar from my Oracle experience. Also, it's consistent with treatment of an empty collection.
- Groovy is inspired by Python and adopts similar conventions for truthiness.
- Ruby takes a different opinion that all values are truthy except
false, of course). While it's not my personal preference, it's defensible and self-consistent.