Skip to content
Katabench
Try free

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 Ragnaros never changes (quality 80, sellIn frozen);
  • everything else decrements sellIn;
  • a normal item degrades by 1 (2 once sellIn is negative), never below 0;
  • Aged Brie improves by 1 (2 once expired), never above 50;
  • Backstage passes to a TAFKAL80ETC concert improve 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

C#
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.

C# refactoring exercises →

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