Gilded Rose Kata

In the process of teaching my oldest daughter Smalltalk, we finished working through the Gilded Rose Code Kata, for now the code can be found on Smalltalkhub .

The given code is a D&D inventory system, the task is to change the update method so conjured items degrade twice as quickly as normal items. For this we wrote a simple test that degraded a conjured vest by 1 day and compared its value to a normal vest degraded by two days.

One problem which threw us off was GildedRoseImaginaryTest>>#nextDay: takes an argument, but this item must be the same item which is most-recently created by #givenItem:sellIn:quality:. Each time this is sent replaces the inventory.

Its clear that the creators wanted the task to modify GildedRose>>#updateInventory based on the comment that Item should not be modified. An interesting exercise is to support the same functionality by modifying Item and adding subclasses. This means adding a message to express how items lose value:

  • Item
    • AgedItem
    • TicketItem
    • ConjuredItem
    • LegendaryItem

Several advantages are provided by this:

  • The original inventory update method hard-coded the names of the items, the new code changes this to invoking #nextDay on each item. The message Item>>named:... is now a factory for choosing a subclass
  • The original inventory update had two variables to manage, and for some items these had an interconnected relationship (for example tickets to events that have passed have no quality)
Posted by John Borden at 14 October 2020, 1:52 am link