Skip to content

Instantly share code, notes, and snippets.

@Kjaer
Last active April 26, 2022 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kjaer/2dede8557fa506424011e08e078522c0 to your computer and use it in GitHub Desktop.
Save Kjaer/2dede8557fa506424011e08e078522c0 to your computer and use it in GitHub Desktop.
Good bad and ugly Code
// Bad
// Using another condition inside the ternary operator a.k.a nested ternary is bad.
products.filter(({ availableStock, quantity }) => availableStock !== undefined ? quantity <= availableStock : true)
// Ugly
// too many understanding requires, you need to be fully comprehend the double-pipe operator, then read the condition,
// yet code still feels uncertain taste in the mouth.
products.filter(({availableStock, quantity}) => availableStock === undefined || availableStock >= quantity
// Good
// You read it, you understand it. No tricks.
products.filter (( availableStock, quantity )) => {
// if there's an stock check with quantity
// and include product item only
// if stock higher than quantity
if (availableStock) {
return availableStock >= quantity;
}
// if there's no stock then include
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment