Created
April 10, 2020 18:47
-
-
Save kyleshevlin/671b11f8c4b9f36b91d0b841e2a8e222 to your computer and use it in GitHub Desktop.
Naming things is hard.
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
// Naming things based on what they do means we don't have to change | |
// the name when we change the implementation details | |
function applyDiscountToItems(items) { | |
return items.map(item => ({ | |
...item, | |
price: item.price * .75 | |
})) | |
} | |
// vs... | |
function mapDiscountOverItems(items) { | |
const result = [] | |
let i | |
for (i = 0; i < items.length; i++) { | |
const item = items[i] | |
results.push({ ...item, price: item.price * .75 }) | |
} | |
return result | |
} | |
// The name no longer accurately represents _how_ the discount is applied, | |
// thus, it wasn't a good name in the first place. Doesn't matter if I map, | |
// or forEach, or for loop, it only matters _what_ is accomplished, not _how_. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment