Last active
April 26, 2022 06:55
-
-
Save Kjaer/2dede8557fa506424011e08e078522c0 to your computer and use it in GitHub Desktop.
Good bad and ugly Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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