I don't think what the author says is the intent of the code. isNaN() returns true not just for NaN but for anything that is "not a number"[1]. For example, it returns true for things like "hello". So it just canonicalizes everything that is not a number into NaN. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
This is my issue with js; isNaN looks related to NaN in a 1-1 mapping as in, testing ‘x is NaN’ but it doesn’t. So the naming is confusing as it does more than that, isNotNumeric() or so.
Writing code for both computers and humans
21–30 of 32 posts
Re: Writing code for both computers and humans
#22Earlier quoted context omitted.
Funny. If this is effectively so, the code is actually fairly ok and clearly expresses the intent for humans yet the OP claiming exactly that managed to still misunderstand the code because they made up a precondition ('defaultValue is going to be a number') probably exactly because they refused to 'To answer this question, you need to start looking around for clues' so they missed the clue [1]; or maybe rather the c…
> dynamically typed so[...] People accustomed to strongly typed languages and thinking mostly of monolithic apps always pin things on dynamic typing even where it's not the cause. It's not about dynamic typing in this case. What's going on here is partially because of separate compilation and partially because of mobile code (as Lars Bak explains[1]). Even if developing in a strongly typed language, the machinery wou…
The point of typing is to make sure this situation doesn't happen.
I write a method to accept Number inputs. I don't want to do the work (whether it's thinking or writing tests) to make it a pleasant experience for someone to pass non-Number inputs to my function (intentionally by mistake).
Re: Writing code for both computers and humans
#23Re: Writing code for both computers and humans
#24This communicates an important idea, Well-written code is not only correct and efficient, but it is also readable, maintainable, and understandable to other programmers. This culture should be encouraged more to make other developer's life easier, The thing I do nowadays is that I would often switch perspectives now and then, When switching perspectives, if i become confused, i would work on making the code more mean…
Re: Writing code for both computers and humans
#25I don't think what the author says is the intent of the code. isNaN() returns true not just for NaN but for anything that is "not a number"[1]. For example, it returns true for things like "hello". So it just canonicalizes everything that is not a number into NaN. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
This is my issue with js; isNaN looks related to NaN in a 1-1 mapping as in, testing ‘x is NaN’ but it doesn’t. So the naming is confusing as it does more than that, isNotNumeric() or so.
I'd much rather call this "normalizeNaN" or something.
Re: Writing code for both computers and humans
#26Earlier quoted context omitted.
This is my issue with js; isNaN looks related to NaN in a 1-1 mapping as in, testing ‘x is NaN’ but it doesn’t. So the naming is confusing as it does more than that, isNotNumeric() or so.
I am ready to oppose any `isFoo` function that doesn't return a boolean. If you ask "Is this a Nan", why would you expect 42 as an answer? Or, "hello". Kinda feels like "Are you at home?" - "Kitchen". It eventually becomes a "yes" through more thought, but eh... I'd much rather call this "normalizeNaN" or something.
Re: Writing code for both computers and humans
#27I don't think what the author says is the intent of the code. isNaN() returns true not just for NaN but for anything that is "not a number"[1]. For example, it returns true for things like "hello". So it just canonicalizes everything that is not a number into NaN. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Even if the author was right about the intent, I would disagree with them that “this is how to write code with empathy for other programmers”, because the code would just make me think “I must be missing something” (and indeed the author did miss something). My theory, by the way, was that the expression is normalizing the possible NaN representations to a definite one, possibly to prevent transporting information vi…
Anyway, if they’re trying to coerce non-numbers into NaN, I’d write it like this:
typeof defaultValue === “number” ? defaultValue : NaN
I think that’s much more clear about the intent. I wouldn’t be confused by that code like I was confused by the code cited in the article.Re: Writing code for both computers and humans
#28Earlier quoted context omitted.
> dynamically typed so[...] People accustomed to strongly typed languages and thinking mostly of monolithic apps always pin things on dynamic typing even where it's not the cause. It's not about dynamic typing in this case. What's going on here is partially because of separate compilation and partially because of mobile code (as Lars Bak explains[1]). Even if developing in a strongly typed language, the machinery wou…
> because it will of course evaluate to NaN anyway when undergoing the number treatment. The point of typing is to make sure this situation doesn't happen. I write a method to accept Number inputs. I don't want to do the work (whether it's thinking or writing tests) to make it a pleasant experience for someone to pass non-Number inputs to my function (intentionally by mistake).
Re: Writing code for both computers and humans
#29Earlier quoted context omitted.
> because it will of course evaluate to NaN anyway when undergoing the number treatment. The point of typing is to make sure this situation doesn't happen. I write a method to accept Number inputs. I don't want to do the work (whether it's thinking or writing tests) to make it a pleasant experience for someone to pass non-Number inputs to my function (intentionally by mistake).
Did you take note of the context in which this conversation is taking place? Because it doesn't make sense as a response within that context.
> Writing code for both computers and humans
Show me what you got.
> isNaN(defaultValue) ? NaN : defaultValue
Yuck.
> This made me pause for a moment.
Of course it did. I still pause every time I re-read it!
> This ternary is much better. This is expert-level programming. This is how to write code with empathy for other programmers.
No, it makes me pause every time I read it, and I cannot follow the path from "made me pause" to "expert-level programming".
Back to your comment:
>> dynamically typed so[...]
> It's not about dynamic typing in this case.
It is about dynamic typing. isNaN should not exist (except for specific cases such as machine floats where NaN means something concrete).
> If you saw that, your spidey-sense might start tingling, “This is a number—did they consider the NaN case?”
I want tingle-free programming. Let the compiler do the tingling for me. A Number-which-may-or-may-not-be-a-Number is not statically typed. I don't care if it's in a .ts file.
There are 15 more references to NaN in that module and they do not make for easy reading:
// Clamp to min and max, round to the nearest step, and round to specified number of digits
let clampedValue: number;
if (isNaN(step)) {
clampedValue = clamp(parsed.current, minValue, maxValue);
} else {
clampedValue = snapValueToStep(parsed.current, minValue, maxValue, step);
}
Why doesn't the above code match its comment? Why doesn't it look something like: // Clamp to min and max, round to the nearest step, and round to specified number of digits
let output = input |> roundNearest(step)
|> clamp(min, max)
|> roundToDigits(numDigits)
Their code either calls into clamp() or snapValueToStep() based on whether 'step' is a number. Does that imply 'step' needs to be a number for the snapValueToStep case? If so, 'step' will need to be checked again inside that method. Why didn't they accept a typed number as input? Or - if they were forced to accept a string - check it at the top of the method? Oh wait - they did! Twice in two lines! let clampStep = !isNaN(step) ? step : 1;
if (intlOptions.style === 'percent' && isNaN(step)) {
clampStep = 0.01;
}
So now 'step' and 'clampStep' are both in scope. They may or may not be numbers (insofar as the coder is willing to keep scrolling back up and re-reading code that at first "makes them pause", but then later makes them feel like it's "expert code".)Why not:
const clampStep =
case parseNum strStep of
Just step -> step
Nothing
| intlOptions.style == Percent -> 0.01
| otherwise -> 1
* One definition site, so you can read all possible values from the leaves {step, 0.01, 1}* The possibly-a-String step has been demoted to 'strStep :: String', so that 300 lines later, the coder cannot not accidentally pass 'step' to another function (and if he did, the compiler would stop it immediately).
* clampStep is actually a number, so no more NaN checks.
But hey, that's all par for the course: Java programmers don't think they hit NPEs, C++ programmers don't think they have ("modern") memory issues, and dynamic programmers don't think they have type problems.
What really prompted me to comment was your suggestion that it would have been better to skip the confusing checks, keep passing through an untyped object, and let some downstream expression blow up.
Re: Writing code for both computers and humans
#30Earlier quoted context omitted.
Did you take note of the context in which this conversation is taking place? Because it doesn't make sense as a response within that context.
(My top-level reaction to TFA, for background. I left it out because no-one asked.) > Writing code for both computers and humans Show me what you got. > isNaN(defaultValue) ? NaN : defaultValue Yuck. > This made me pause for a moment. Of course it did. I still pause every time I re-read it! > This ternary is much better. This is expert-level programming. This is how to write code with empathy for other programmers. N…
> Java programmers don't think they hit NPEs, C++ programmers don't think they have ("modern") memory issues, and dynamic programmers don't think they have type problems
Aside from the obvious category error, you aren't talking to a "dynamic programmer" who "[doesn't] think they have type problems". (It may also shock you to learn that I'm not an advocate of NodeJS/NPM, the programming style exemplified by the Adobe project identified in the blog post, or even TypeScript. That doesn't change the fact that I'm right about the things I said before—and the fact that I am right means it wouldn't matter even if I were a "dynamic programmer", anyway.)
The _entire_ point of what I wrote is laid out in my first paragraph: "People accustomed to strongly typed languages and thinking mostly of monolithic apps always pin things on dynamic typing even where it's not the cause." I even made it more explicit. This problem is not because of dynamic typing. This is a problem because of code mobility and separate compilation. I even provided references, FFS. You'd get the _same_ problem in languages that are not JS or TypeScript—including static languages. Why? Because this is a problem arising from code mobility and separate compilation—not static versus dynamic typing.
> What really prompted me to comment was your suggestion that it would have been better to skip the confusing checks
Not what I said, Holmes. The strength of your powers of divination don't merit the confidence you seem to have in them.
This is where I exit.