1. 5
  1.  

  2. 4

    This article feels like arguing a train wreck is good and proper because it follows from the physics.

    1. 2

      I don’t think the author does a good job of showcasing this, and likely to be an unpopular opinion here, but having quite a bit of experience developing stuff in Javascript at this point, there is a wisdom in having a falsy version of primitives. It both eliminates needless bits and when used properly can eliminate a whole tree of Maybe types.

      Let’s say you are given this code contrived (typescript) code just to illustrate what I mean:

      type Person = 
       { name: string
       ; title    : string
       } 
      const titleIsAbbreviated (person : Person) : boolean => person.title.includes(".")
      const hasTitle = (person : Person) : boolean => !!person.title
      // from some form or whatever
      const sarah = {name: "Sarah", title: ""}
      hasTitle(sarah) // => false
      titleIsAbbreviated(sarah) // => false
      

      Many google javascript SDKs are very good about being cognizant of this, even if I have have other issues with their quality. By removing undefined | null | string and always having the type be string you can vastly reduce the number of problems. This is in fact very common in core browser apis for precisely this reason, which unfortunately most people are very unfamiliar with these days.

      For example:

      const audio = document.createElement("audio")
      audio.currentSrc // => ""
      audio.currentSrc.startsWith("blob:") // this is still safe because it's a string!
      

      tldr; coercion is a powerful tool and with great power comes great responsibility

      1. 2

        There’s something disingenuous about the author’s line of reasoning, which goes something like this:

        1. If you’re complaining about type coercion in JavaScript or using strict equality ( ===) by default, you’re wrong.
        2. Type coercion can be good sometimes, e.g.: '' == false. (His opinion, not mine).
        3. Type coercion can be bad sometimes, e.g.: '' == 0. (Again, his opinion.)
        4. Instead of using strict equality, just change the language… but maybe not because such changes have a “1e-9” chance of being accepted by TC39.
        5. OK, the actual solution is for someone else to create a language called ProperScript that compiles to JavaScript and is the same as JavaScript with the addition of a "use proper" pragma that redefines type coercion to suit his opinion of how type coercion ought to work.

        This is not the first time I’ve encountered a blog article or a tweet by this author with an abrasive presumption of ignorance on the part of the reader. How convenient that our collective malady can be ameliorated with the purchase of his book.

        1. 1

          I’m not saying I agree with this, I just thought it was an interesting take on the issue.

          1. 1

            My apologies if my comment made you feel judged. I appreciate contrary opinions and I’m happy to have this article on the site.

            1. 2

              Yeah, I’m pretty new to this site. Feels like the joke about the first time asking a question on SO.

              1. 1

                Yeah, it’s not a good look. I’ll try to do better.