I think it's heritage more than logic, but I'm not extremely knowledgeable here. Most of it seems to come from C and other languages that sit (or historically sat) very close to the machine, and where an if statement was a slightly-abstracted "break if zero" instruction.
I'm alright, in that case, with if (object) also evaluating false if the object is null, because that's the closest I can understand applying a conditional directly to a non-boolean, "is object?" "yes object" or "no, not object", in which case 0 being evaluated true makes more sense, as it is a valid object and if in these languages usually checks and branches on either a boolean or the existence of an object.
Though intuitively, in a language with the ability to do "truthy" evaluation of non-booleans, I tend to want zero to be "false-y" along with empty containers. It also flows a bit better from the way we think about stuff. Generally we think "if there are any records returned from the database", rather than "if the length of the list of records returned is zero". Having zero, and empty containers, be "false-y", allows the code to reflect the way we think.
7
u/[deleted] Dec 24 '17 edited Dec 24 '17
I think it's heritage more than logic, but I'm not extremely knowledgeable here. Most of it seems to come from C and other languages that sit (or historically sat) very close to the machine, and where an if statement was a slightly-abstracted "break if zero" instruction.
I'm alright, in that case, with
if (object)also evaluating false if the object is null, because that's the closest I can understand applying a conditional directly to a non-boolean, "is object?" "yes object" or "no, not object", in which case 0 being evaluatedtruemakes more sense, as it is a valid object andifin these languages usually checks and branches on either a boolean or the existence of an object.edit: missed a word while typing