airbnb / javascript

JavaScript Style Guide
MIT License
145.32k stars 26.52k forks source link

Proposal. Simplify point 4.5 (Filter) #1448

Open hadson19 opened 7 years ago

hadson19 commented 7 years ago

In this part of best practice I found a part of code

// bad
inbox.filter((msg) => {
  const { subject, author } = msg;
  if (subject === 'Mockingbird') {
    return author === 'Harper Lee';
  } else {
    return false;
  }
});

// good
inbox.filter((msg) => {
  const { subject, author } = msg;
  if (subject === 'Mockingbird') {
    return author === 'Harper Lee';
  }

  return false;
});

But, it seems that we can write this part of code better:

inbox.filter((msg) => {
  const { subject, author } = msg;
   return subject === 'Mockingbird' && author === 'Harper Lee';
});

Isn't it?

Thanks, Andrii

ljharb commented 7 years ago

In this specific case, you're totally right - however, rather than applying that change, I'd rather alter the bad/good examples so that they show complex logic.