3 ms·
zero being false is fine in statically typed languages like C, because there's still only one false value available, and you wouldn't be checking for it if you
by smegsicle 4y ago
zero being false is fine in statically typed languages like C, because there's still only one false value available, and you wouldn't be checking for it if you weren't expecting zero to be false
in dynamic languages, wanting to take advantage of brevity tricks with number zero or empty list or empty string being falsy can seem like a good idea, but gets confusing fast-
different languages are going to have different falsy values, and you can easily get caught off guard when legitimately wanting to accept an 'empty' value
common lisp has NIL as its definition of the end of a list, which ends up working out fine since lists are so deeply embedded in the language design that it's hard to forget that they're there (any other kind of object is 'truthy', including any size of regular array)
scheme has a separate #f that is taken as the only falsy value, which is probably also fine
python and javascript are a mess, with different kinds of 'empty' or 'nothing' values being falsy whether you want them to be or not, which is the kind of helpfulness we don't need- where evaluating in boolean context means "if this value is present but not 'empty', in a way that is dependent on the objects type, and may change over time"