Gilded Rose Refactoring Kata
The classic exercise for learning to refactor legacy code: a short, tangled function, a requirements document and a new feature to add. Emily Bache's repository has starting code in dozens of languages, including Java, Python, TypeScript and C#, with unit test and approval test setups ready to go.
4.3k
GitHub · Sept 2026
6.1k
GitHub · Sept 2026
MIT
GitHub · Sept 2026
GitHub repo
Other
Emily Bache
2 to 4 hours per attempt; worth repeating
Intermediate
Free
What the kata is
A kata is a small exercise you repeat to build a skill. The Gilded Rose is the best-known refactoring kata. You inherit the code for a small inn's inventory system: one function that updates the quality and sell-by date of every item each day. The rules differ for ordinary items, aged cheese, legendary items and backstage passes, and the function is a deep nest of if statements that is hard to read and risky to change. Your job is to add support for a new kind of item.
The exercise was created by Terry Hughes and made popular by Bobby Johnson's article "Refactor This: The Gilded Rose Kata". Emily Bache adapted it into many programming languages and made it easy to start with a failing unit test.
What it teaches
Adding the feature directly into the tangle is possible but dangerous. The kata is designed to push you into the professional approach:
- Pin the behaviour first. Write characterisation tests, or use the ready-made approval test setup based on TextTest, so you know immediately if anything changes.
- Refactor in small steps. Extract functions, replace nested conditions, introduce a class per item type, running the tests after every step.
- Then add the feature, which should now be a small, obvious change.
It is the most direct practice you can get for code smells, Extract Function, Replace Conditional with Polymorphism and working with legacy code.
Languages
The repository has starting code in more than seventy languages, including Java, Python, C#, TypeScript, JavaScript, Go, Kotlin, Rust and many more, and the requirements document is translated into more than a dozen human languages.
How long it takes
A first attempt usually takes an afternoon. The real value comes from repeating it: do it again a few weeks later, perhaps with a different approach, and compare your commit history.
Prerequisites
You should be able to run unit tests in your language and use your IDE's basic refactorings, such as rename and extract function.
Common questions
Is there one right answer? No. Many solutions are published online, but compare them only after your own attempt.
Should I rewrite the function from scratch? Try not to. The point is to practise changing code safely in small steps, which is the skill you need in real code bases.
Best for: anyone learning refactoring, testing legacy code or preparing to work in an existing code base.
Ready to start?
Opens on Emily Bache in a new tab.
Stuck on something specific?
Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.
Follow along
New writing, resources and project ideas land here first.