7 ms·
Writing code for both computers and humans
- rco8786 3y agoI don't know if that was the real intent of this code, considering all the weird nuance around NaN in Javascript - however I agree with the point the author makes, and will take it one step further: We should be building these semantics directly into our languages, not relying on programmers to strictly follow a "best practice". In this case, it would be making values non-nullable and baking in Result/Option/etc style types that force programmers into handling the null (or NaN) case.
- lordwiz 3y agoThis 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 meaningful. Even if we are doing a solo project, if we come back to the code after a long while, the code should be greeting us with wide open hands rather than looking like an unexplored jungle.
- cratermoon 3y agoWhen I'm programming I try to keep in mind what my future self would need. I take notes when I'm programming, and put those notes into the repo if it's a personal project, or in a separate repo just for the project. Mostly those notes are of little value to anyone by myself, but having them around lets me condense my thoughts down to comments or documents useful for other team members. With a little work, I can turn my notes into a variety of other kinds of useful documentation: references, explanations, FAQs, runbooks, etc.
- deleted 3y ago[deleted]
- orn688 3y agoI agree with the author that this is a reasonable way to indicate their intent. But I've seen so many accidentally ineffectual code snippets that if I saw this code I'd be inclined to delete it unless there was also a comment expressing its purpose.
- HuangYuSan 3y agoAnd a comment on its own would be enough anyway
- earthboundkid 3y agoYeah, ideally you’d have some kind of static typing to restrict the code to only use Number, and then a comment that says what the function does in case of NaN.
- carapace 3y agoYeah! There was a thread the other day about a linter that flagged useless code and all the odd bugs it caught, and I think it would have flagged this snippet, eh? "Interesting bugs caught by no-constant-binary-expression" https://news.ycombinator.com/item?id=38196644 https://news.ycombinator.com/item?id=38196644 https://eslint.org/blog/2022/07/interesting-bugs-caught-by-no-constant-binary-expression/ https://eslint.org/blog/2022/07/interesting-bugs-caught-by-n...
- jinwoo68 3y agoI 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/Reference/Global_Objects/isNaN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- anonzzzies 3y agoThis 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.
- jalk 3y agoSeems like the author fell into this trap
- eloisant 3y agoYes, even better "isNumber()" because a function that tests a negative can cause confusion and mistakes.
- tetha 3y agoI 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.
- Jtsummers 3y ago`isNaN` does return a boolean though, it doesn't return a normalized value.
- stinos 3y agoFunny. 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 clue is: dynamically typed so expecting a number is a wrong assumption. Still I'd argue a comment explaining why it's in this particular case fine that any non-sane input translates to NaN and not an error might be worth it Well, unless that is clear from the surrounding code :)