Gilded Rose
Hard About 40 min Free, no account needed
The canonical refactoring kata, one item per call, gated on method size.
The brief
UpdateQuality(name, sellIn, quality) advances one inventory item by one day
and returns [newSellIn, newQuality]:
Sulfuras, Hand of Ragnarosnever changes (quality 80, sellIn frozen);- everything else decrements
sellIn; - a normal item degrades by 1 (2 once
sellInis negative), never below 0; Aged Brieimproves by 1 (2 once expired), never above 50;Backstage passes to a TAFKAL80ETC concertimprove by 1, by 2 when 10 or fewer days remain, by 3 when 5 or fewer remain (checked before the decrement), and drop to 0 after the concert. Cap 50.
The starter is the famous original. Refactor without changing behavior until every gate passes: one small handler per item kind.
Starter code
using System;
public class Solution
{
// The original. Do not ask why; ask what the tests say.
public int[] UpdateQuality(string name, int sellIn, int quality)
{
if (name != "Aged Brie" && name != "Backstage passes to a TAFKAL80ETC concert")
{
if (quality > 0)
{
if (name != "Sulfuras, Hand of Ragnaros")
{
quality = quality - 1;
}
}
}
else
{
if (quality < 50)
{
quality = quality + 1;
if (name == "Backstage passes to a TAFKAL80ETC concert")
{
if (sellIn < 11)
{
if (quality < 50)
{
quality = quality + 1;
}
}
if (sellIn < 6)
{
if (quality < 50)
{
quality = quality + 1;
}
}
}
}
}
if (name != "Sulfuras, Hand of Ragnaros")
{
sellIn = sellIn - 1;
}
if (sellIn < 0)
{
if (name != "Aged Brie")
{
if (name != "Backstage passes to a TAFKAL80ETC concert")
{
if (quality > 0)
{
if (name != "Sulfuras, Hand of Ragnaros")
{
quality = quality - 1;
}
}
}
else
{
quality = quality - quality;
}
}
else
{
if (quality < 50)
{
quality = quality + 1;
}
}
}
return new[] { sellIn, quality };
}
}How it is graded
Behavioral tests have to stay green while the grader measures complexity, nesting, method length, and duplication from your source.
Related guides
What it practices
- Replace conditional with polymorphism
- Move each branch of a type switch into the type that owns it.
- Extract method
- Pull named steps and named conditions out of a long method so each piece does one thing.
Related katas
- Dedupe the Field Validators Easy Free Three fields, three identical validation blocks.
- Flatten the Game of Life Row Hard Pro Neighbor counting buried under nested loops and boundary ifs.
- Flatten the Spiral Matrix Hard Pro Spiral traversal with direction turns nested several deep.
- Split the Escaped Frame Decoder Hard Pro A serial protocol decoder mixes framing, escaped bytes, bounded buffering, checksum decisions, and chunk state in one method. Extract a recoverable parser without changing its wire behavior.