Last active
November 15, 2021 19:22
-
-
Save qntm/02a262cb692c79441e95f4637e840f3e to your computer and use it in GitHub Desktop.
My Gilded Rose entry
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
class Item { | |
constructor (name, sellIn, quality) { | |
this.name = name | |
this.sellIn = sellIn | |
this.quality = quality | |
} | |
} | |
class Shop { | |
constructor (items = []) { | |
this.items = items | |
} | |
updateQuality () { | |
// items are mutated in place! | |
this.items.forEach(item => { | |
// this item has fixed `quality` and `sellIn` | |
if (item.name === 'Sulfuras, Hand of Ragnaros') { | |
return | |
} | |
if (item.name === 'Aged Brie') { | |
// brie improves in quality with age, twice as fast once past sell-by date | |
item.quality += item.sellIn <= 0 ? 2 : 1 | |
} else if (item.name === 'Backstage passes to a TAFKAL80ETC concert') { | |
// backstage passes improve in quality until the concert, then become worthless | |
if (item.sellIn <= 0) { | |
item.quality = 0 | |
} else { | |
item.quality += item.sellIn <= 5 ? 3 : item.sellIn <= 10 ? 2 : 1 | |
} | |
} else { | |
// regular items decrease in quality with age, faster once past sell-by date | |
item.quality -= item.sellIn <= 0 ? 2 : 1 | |
} | |
// quality is clamped | |
if (item.quality <= 0) { | |
item.quality = 0 | |
} | |
if (item.quality >= 50) { | |
item.quality = 50 | |
} | |
item.sellIn-- | |
}) | |
return this.items | |
} | |
} | |
module.exports.Item = Item | |
module.exports.Shop = Shop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my preferred refactoring of this source file, which is the JavaScript version of the "Gilded Rose" refactoring exercise originally created by Terry Hughes.
Comments in-line are part of my refactoring and are addressed to hypothetical future maintainers of this code. Actual comments from me, the person participating in this exercise, to you, the person reviewing my submission, are as follows:
quality
andsellIn
are integers, and that every element ofitems
is an object of classItem
.module.exports
, I prefer to assign properties to the existingmodule.exports
object. This decreases the probability of some very troublesome behaviour in the event of circular imports.updateQuality
method both modifies the items in place and returnsthis.items
. To me, returningthis.items
gives the false impression that the returned value is a brand new array and that the originalthis.items
was left unchanged. (Array.prototype.sort
has the same problem.) I would prefer it ifupdateQuality
returnedundefined
. However, my refactoring preserves the existing behaviour.quality
outside of the closed integer range [0, 50], 60 say, and is expected to stay that way. (Again, my refactoring preserves this behaviour.) Isquality
capped or not? Is this a bug in the game? Note that factoring these values out as, say,const MIN_QUALITY = 0
andconst MAX_QUALITY = 50
would be wrong in this scenario. What would we even call these constants, in this case?