DEV Community

Discussion on: Stop Writing JavaScript Like This

Collapse
 
ravavyr profile image
Ravavyr

These are good tips:

  1. The performance difference is irrelevant, the array includes way feels cleaner but is actually harder to read because you don't immediately see what you're comparing the value to, and the more items in the list, the farther away the variable is visually so it actually makes the code harder to understand for even slightly more complex lists.

  2. Optional chaining is terrible terrible and let me say it again, terrible.
    See. If obj?.address?.postalCode fails, you don't know if obj doesn't exist, or obj.address doesn't exist or if it's just the obj.address.postalCode that's missing.
    This WILL [not can] lead to problems where no error happens even though an important piece of information is missing and debugging it is a mess where you end up splitting it up anyway so it's better to validate each part of the object exists before using it.

  3. You'll never do second=first....if so you would just use "first" in the first place.
    That scenario literally never happens unless you're writing extra code and creating extra variables you don't need.

  4. I like this one, i usually use arrays for it, but objects work too.

  5. If a function has one line in it...it shouldn't be a damn function. That level of abstraction makes systems a bloody nightmare to debug because you're jumping 20 functions deep to find the culprit and often in 20 different files. Just KISS dammit.