Untangle branch-heavy code
21 katasTurn pricing rules, eligibility trees, state machines, rollout reducers, and calculators into smaller decisions without changing an answer.
Clean-code practice with executable limits
Inherit C# that works and that nobody wants to maintain. Refactor it without changing behavior, then prove the result against tests and Roslyn-measured quality gates across 51 focused katas.
Six free preview katas. No local setup required.
Passing tests are half the job
A green suite proves that your refactor still works. It does not prove that you removed the branch maze, split the god method, or eliminated the pasted blocks. Katabench checks both halves on every submission.
The exercise defines structural limits. Roslyn reads your submitted source and measures it directly, while hidden tests keep the original behavior pinned. Clean is no longer a matter of whether the diff feels nicer.
New to refactoring C# code? Start with the test-first refactoring playbook and the catalog of code smells with C# examples. For the numbers behind the gates, read what cyclomatic complexity measures and how structural grading works.
method length
limit 25 lines
cyclomatic complexity
limit 8
nesting depth
limit 3 levels
✓ 9 / 9 tests still passing (behavior never changed)
A C# refactoring example
This is the starter for Flatten the Null Checks. It already passes its tests. Your job is to remove the nesting without changing its behavior.
using System;
public class Solution
{
// Shipping label - the arrow anti-pattern in its natural habitat.
public string ShippingLabel(string name, string street, string city, string zip)
{
if (name != null && name.Length > 0)
{
if (street != null && street.Length > 0)
{
if (city != null && city.Length > 0)
{
if (zip != null && zip.Length > 0)
{
return name + "\n" + street + "\n" + city + " " + zip;
}
else
{
return "incomplete address";
}
}
else
{
return "incomplete address";
}
}
else
{
return "incomplete address";
}
}
else
{
return "incomplete address";
}
}
}Read the full brief. The guide to guard clauses in C# covers the move that flattens this kind of nesting.
Four refactoring skill families
These are not syntax quizzes. Each family repeats a recognizable maintenance problem until you can see the shape quickly and choose a behavior-preserving move.
Turn pricing rules, eligibility trees, state machines, rollout reducers, and calculators into smaller decisions without changing an answer.
Replace arrow code, flags, nested loops, and decoder branches with guard clauses, focused queries, and a readable happy path.
Extract responsibilities from importers, scorers, writers, decoders, terminal replay, and classic katas until each method has one coherent job.
Find the stable shape behind pasted validators, renderers, calculators, and exporters, then give it one home.
Free C# refactoring exercises
Open any preview in the browser, inspect the already-green starter, and refactor until the behavioral and structural gates agree. No project download or SDK setup is required.
An address printer hiding at the bottom of four nested ifs.
Open this kata: Flatten the Null Checks Read the brief: Flatten the Null ChecksThree fields, three identical validation blocks.
Open this kata: Dedupe the Field Validators Read the brief: Dedupe the Field ValidatorsThe tennis kata: love, deuce and advantage in one long method.
Open this kata: Split the Tennis Score Read the brief: Split the Tennis ScoreLetter grades with bonuses, written as a wall of nested ifs.
Open this kata: Tame the Grade Calculator Read the brief: Tame the Grade CalculatorUntangle a copy-pasted discount calculator until every quality gate passes - without changing a single answer.
Open this kata: Tame the Discount Engine Read the brief: Tame the Discount EngineThe canonical refactoring kata, one item per call, gated on method size.
Open this kata: Gilded Rose Read the brief: Gilded RoseThe refactoring loop
1
The starter already passes its behavioral tests. Read the mess, identify the pressure point, and decide which small move will improve it.
2
Edit C# in the browser and run the visible tests before you spend a submission. The goal is better structure with exactly the same behavior.
3
Submit against hidden behavior tests and authored Roslyn limits for complexity, nesting, method length, and duplicate blocks.
Stuck after a serious attempt? Sign in and reveal the reference solution, compare its structure with yours, reset the exercise, and make the same moves from memory. That turns a solution into another rep instead of an answer to copy.
C# refactoring exercise FAQ
They are working C# programs with deliberately poor structure. You improve the design without changing the behavior, then use tests and structural measurements to prove both constraints still hold.
Yes. Katabench includes the canonical Gilded Rose behavior in C# and grades the result on behavioral tests, method length, cyclomatic complexity, nesting depth, and duplicated blocks.
Six refactoring katas are free previews, including Gilded Rose, Tennis Score, null-check flattening, and duplication removal. Katabench Pro unlocks all 51 exercises and unlimited submissions.
Yes. You can reveal a correct reference solution after trying an exercise, study the resulting structure, then reset the kata and solve it again. Revealing needs an account, and daily reveal limits depend on your plan.
The behavioral suite checks that the program still does the same work. A Roslyn-based analyzer separately measures cyclomatic complexity, nesting depth, method length, and duplicated blocks against limits authored for that exercise.
Start from code that already works and has tests. Make one small move at a time, such as extracting a method or adding a guard clause, and run the tests after each one. Katabench adds structural limits, so you can see whether the move reduced complexity, nesting, method length, or duplication.
Looking for broader practice? Browse all C# coding challenges, or see every refactoring kata in the catalog.
Start with the free Gilded Rose kata and let the tests plus structural metrics tell you when the refactor is actually done.
Try Gilded Rose free