3 ms·
JavaScript gets a lot of flak about weird coercions but I never found them a big source of errors compared to say more basic things like forgetting function arg
by seanwilson 8y ago
JavaScript gets a lot of flak about weird coercions but I never found them a big source of errors compared to say more basic things like forgetting function arguments or getting them in the wrong order. Coercions are way less of a problem in TypeScript as well because coercions that change the type will get flagged at compile time.
- shawnz 8y agoAgreed, These corner cases are weird only because there's basically no sensible thing to do in these situations which would be consistent with the rest of the rules. Since there's no real reason you'd be doing any of these weird coersions I think it's pretty unlikely that they'd result in bugs. It's not fail-fast, but I don't see this stuff resulting in any subtly incorrect behaviors.
- seanwilson 8y ago> there's basically no sensible thing to do in these situations Are there any better options for dynamic languages when a weird coercion is going to be done? Throw an exception? Exit with an error? Convert to some special invalid_coercion value similar to NaN? It's another reason static type checking (i.e. don't let the code run) is a no-brainer to me.
- gruez 8y ago>Throw an exception? Seems like the best choice rather than plowing ahead with assumptions
- jimmaswell 8y agoI feel like it's fine for languages in a web context to take a "the show must go on" attitude as they do. Partially working webpages (slightly broken html/js/etc) are generally much better than a blank page/error message or none of the js working at all.
- shawnz 8y agoDynamic languages are all about letting the caller decide what makes sense and what doesn't, rather than the callee. If you are too conservative with the input types you allow then it can create unnecessary coupling.
- ficklepickle 8y agoWhat about calling toString() on an object? I think serializing the object would be more useful than [object Object]. I'm sure JSON.stringify didn't exist originally, so it wasn't an option. I guess it must be for backwards compatibility, but I can't think of much of a use case. Maybe something somewhere uses it instead of typeof?
- edflsafoiewq 8y agoForget to convert a string to a number for example, and you'll get very surprising results for arithmetic expressions, eg. (x+1)*2. Discovering how the sort function works was also not fun.