Primitive Obsession

Chris Coyier Chris Coyier on

I found this essay Primitive Obsession by Andrew Cairns interesting. The big example in it is this:

$user = new User('abc123', 'email@address.com');Code language: JavaScript (javascript)

Here the User function takes two string params, string being a primitive type, and here, quite easy to screw up by passing them in the wrong order and/or not pre-validating the values. If you agree, there are a variety of ways to tackle this problem which generally boil down to passing in a typed object. TypeScript and Zod fans will be like: yeah duh.

It's time to take your JavaScript to the next level

One response to “Primitive Obsession”

  1. Andrew says:

    Glad you found the article interesting! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

$966,000

Frontend Masters donates to open source projects through thanks.dev and Open Collective, as well as donates to non-profits like The Last Mile, Annie Canons, and Vets Who Code.