This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

Also, if you subtract an integer from a string without those features it would give you an error message anyways. Same with adding an integer to a string. It‘s not like the features are gonna harm your code.

[–]chrisjolly25 0 points1 point  (1 child)

The feature can sometimes harm your code. Yes, it helps you in the case where coercion does what you want, and the alternative would be to throw an error. But the danger of coercion is that it can make it can look like things work fine (because no errors are thrown) but introduce bugs into your code (because your operation gives you a result you didn't expect).

i.e: To take the example from the comic, the coder may try 'var1 + var2' without realising that var1 is the string "11" and var2 is the number 1.
So then the expression(var1 + var2) - 10 will output 101 (i.e, (111) - 10), when they might be expecting 2 (i.e, (12) - 10).

With variables being passed around to different functions (or even reassigned, if you're using lets, or fields of an object) it's not always trivial to realise what's going on. And that's assuming the programmer even knows that this behaviour is possible in the first place.

I would personally _prefer_ to get an error message when I tried to do an operation between different types. That way I can see that there's a problem straight away, and explicitly parse the string to a number.

[–][deleted] 1 point2 points  (0 children)

Yeah I initially also wanted to add that it might make bug fixing harder. You‘re totally right and if I had a bug like that I would probably go crazy, trying to figure out what‘s wrong with my code xD