3 ms·
Making anything besides booleans "truthy" strikes me as asking for a lot of trouble for very little gain. For each of these cases, how hard is this to write:
by StefanKarpinski 13y ago
Making anything besides booleans "truthy" strikes me as asking for a lot of trouble for very little gain. For each of these cases, how hard is this to write:
x == 0
isempty(x)
x == nothing
How much clearer is the intent of that code than a truthy boolean test would be? This clarity is all the more important in dynamically typed languages without type annotations since the code itself doesn't give any hint what the type of `x` might be, so the programmer doesn't know what it is the test is really checking for.
Oh the other hand, the cost of truthiness is pretty significant. Each person reading or writing code in a language with truthiness must remember all the arbitrary truthiness rules that particular language uses – and they're quite different for each of Python, Perl, Ruby, C, JavaScript, etc. As this issue demonstrates, whenever you open the door a little, even in a generally sane language like Python, at some point some weird decisions get made and you find yourself with some strange corner cases like midnight being falsey. If you wanted to know if a date was midnight, wouldn't it be easy enough to just explicitly test that?
- solutionyogi 13y agoCompletely agree. C# has this right. An expression must produce a valid boolean value, the language does not evaluate any random type as boolean based on a set of rules. E.g. you can't say following in C# if(number) //number is of type integer. if(o1) //o1 is a reference type variable. if("string") if(number = 10) // number = 10 is an assignment and produces 10. You must write: if(number == 0) if(o1 != null) if("string".Length > 0) if(number == 10)