3 ms·
The equality and truthiness oddities are the worst. One of my biggest pet peeves about languages is weird equality rules. PHP is one of the worst, there is a hu
by gleenn 12y ago
The equality and truthiness oddities are the worst. One of my biggest pet peeves about languages is weird equality rules. PHP is one of the worst, there is a huge table of crazy rules that I suppose expert PHP people can remember, but it just seems crazy.
Languages like Ruby and Clojure have it best: nil and false are falsy, everything else is truthy.
- mikeash 12y agoSwift's rules are pretty simple too: true is true, false is false, and nothing else is any sort of boolean. If you want to check whether an array is empty, do so explicitly. If you want to test an integer for zero, you have to do == 0. Even testing for nil requires an explicit != nil. As for equality, I think it's just old bugs from an old prerelease compiler. These posts are ancient history, relatively speaking. I just tested on the latest public release and a == [1], [1] == a, and [1] == [1] all produced true, as one would expect. str === str won't compile, also as one would expect (String is a value type, and as such comparing object identity is nonsensical).
- AnkhMorporkian 12y agoPHP is pretty bad, but I think JS really takes the crown for insane equality comparisons. Basically every weird equality comparison from PHP is in JS, along with some other humdingers like 0 == []. And my favorite, '\n\r\t' == 0
- quarterto 12y agoThere is never a good reason to use ==.
- AnkhMorporkian 12y agoI totally agree, but they were bringing up the insane equality rules in PHP using the == operator, so I did likewise in Javascript. Both languages luckily have fantastically logical === operators.