NaN is confusing

NaN is used in javascript to check that a variable is not numeric. A colleague of ours found this confusing and was using it in an incorrect way.

if (!NaN(input) || !NaN(input2) {

The above code was supposed to find out if the two user inputs were not numbers and if so, show an error message to the user. Closely looking at the code you may have noticed that it is actually checking that both inputs are numbers and doing the opposite of what was intended.

So what NaN is doing is the opposite of a normal check. What we should have is a function that checks if a variable IS A number or have a function that is named better. PHP has the function is_numeric which is quite easy to understand. So why is NaN confusing.

First of all, readability which is a strong attribute of good programming is reduced as it is an acronym. A function should tell the user what it does, NaN does not. Secondly as the acronym mentions the word Number, programmers will inevitably (especially after hours and hours of programming) skip the first part and just think Number which is what our colleague did.

If we were to write a story and we wanted to achieve a sense of darkness, the last thing we would want to do is this. "Looking up into the night sky, I could not see the moon at all". Reading that passage you may not have thought of darkness but the light that comes from the moon even though the passage says that the person could not see it (read it again if you are confused). You should not write stories like this.

Not a Number is not something we would say in normal speech. It is robotic and confusing and until robots write computer code for us, we must write functions that are easy to understand.