Skip to content

Instantly share code, notes, and snippets.

@kyleshevlin
Created April 10, 2020 18:47
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 kyleshevlin/671b11f8c4b9f36b91d0b841e2a8e222 to your computer and use it in GitHub Desktop.
Save kyleshevlin/671b11f8c4b9f36b91d0b841e2a8e222 to your computer and use it in GitHub Desktop.
Naming things is hard.
// 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