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 hidden or 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 |
Author
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:
qualityandsellInare integers, and that every element ofitemsis an object of classItem.module.exports, I prefer to assign properties to the existingmodule.exportsobject. This decreases the probability of some very troublesome behaviour in the event of circular imports.updateQualitymethod both modifies the items in place and returnsthis.items. To me, returningthis.itemsgives the false impression that the returned value is a brand new array and that the originalthis.itemswas left unchanged. (Array.prototype.sorthas the same problem.) I would prefer it ifupdateQualityreturnedundefined. However, my refactoring preserves the existing behaviour.qualityoutside of the closed integer range [0, 50], 60 say, and is expected to stay that way. (Again, my refactoring preserves this behaviour.) Isqualitycapped or not? Is this a bug in the game? Note that factoring these values out as, say,const MIN_QUALITY = 0andconst MAX_QUALITY = 50would be wrong in this scenario. What would we even call these constants, in this case?