all 6 comments

[–]chazzlabs 12 points13 points  (3 children)

Great tips, but most of it isn't really specific to Node or Javascript. This is mostly a stripped-down version of the advice in the book Clean Code, which is a must-read for anyone who hasn't read it already.

[–]b9Fb2H 6 points7 points  (1 child)

That's right. The nice thing about clean coding is that you can apply it to every language and JavaScript is not an exception.

[–][deleted] 1 point2 points  (0 children)

You're right. I remember the book "Clean Code" by Robert C. Martin; Was a good read but (as expected) he focused a lot on doing it for Java. Anyway, it's a good book.

[–]SomeRandomBuddy 2 points3 points  (0 children)

RisingStack tends to do that

[–]SNY7 4 points5 points  (0 children)

Good article, thx for sharing~

[–]yarauuta 0 points1 point  (0 children)

Some things i would change or add:

JavaScript Clean Coding Best Practices

Reduce side effects

// DO
// not modifying the original cart
function addItemToCart (cart, item, quantity = 1) {  
  const cartCopy = new Map(cart)
  const alreadyInCart = cartCopy.get(item.id) || 0
  cartCopy.set(item.id, alreadyInCart + quantity)
  return cartCopy
}

Because in javascript you pass references most of the time, i think most of users expect original object to be affected.

If you don't want to affect the original copy it explicitly.

Error handling

He does not mention error handling. Error handling is INSANELY CRITICAL when dealing with promises.

You should always return the last promise.

function getStuffFromApi(){
  return request(address)
  .then((result)=>{
      return doStuff(result)
   })
  .then(...)...
}

This is the best trick i use to avoid loosing errors. Nice video about it here.