Skip to content
Katabench
Try free

Clean-code practice with executable limits

C# refactoring exercises that grade the structure

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 54 focused katas.

Six free preview katas. No local setup required.

refactoring exercises
54
free previews
6
quality gates
4

Passing tests are half the job

Make the code better without moving the behavior

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.

Read the full test-first refactoring playbook or see how structural grading works.

method length

limit 25 lines

the starter: 78 ✗ yours: 18 ✓

cyclomatic complexity

limit 8

the starter: 14 ✗ yours: 6 ✓

nesting depth

limit 3 levels

the starter: 5 ✗ yours: 2 ✓

✓ 9 / 9 tests still passing (behavior never changed)

Measured directly from your source. The tests stay green; the mess has to go.

Four refactoring skill families

Practice the smell, the move, and the proof

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.

Untangle branch-heavy code

23 katas

Turn pricing rules, eligibility trees, state machines, and calculators into smaller decisions without changing an answer.

Flatten deep nesting

11 katas

Replace arrow code, flags, and nested loops with guard clauses, focused queries, and a readable happy path.

Split long methods

10 katas

Extract responsibilities from importers, scorers, writers, and classic katas until each method has one coherent job.

Remove duplication

10 katas

Find the stable shape behind pasted validators, renderers, templates, calculators, and exporters, then give it one home.

Free C# refactoring exercises

Start with a real mess, including Gilded Rose

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.

  1. 01 Easy

    Flatten the Null Checks

    An address printer hiding at the bottom of four nested ifs.

    Open this kata →
  2. 02 Easy

    Dedupe the Field Validators

    Three fields, three identical validation blocks.

    Open this kata →
  3. 03 Easy

    Split the Tennis Score

    The tennis kata: love, deuce and advantage in one long method.

    Open this kata →
  4. 04 Easy

    Tame the Grade Calculator

    Letter grades with bonuses, written as a wall of nested ifs.

    Open this kata →
  5. 05 Medium

    Tame the Discount Engine

    Untangle a copy-pasted discount calculator until every quality gate passes - without changing a single answer.

    Open this kata →
  6. 06 Hard

    Gilded Rose

    The canonical refactoring kata, one item per call, gated on method size.

    Open this kata →

The refactoring loop

Read the code, make one move, verify the evidence

01

Inherit working code

The starter already passes its behavioral tests. Read the mess, identify the pressure point, and decide which small move will improve it.

02

Refactor without a rewrite

Edit C# in the browser and run the visible tests as often as you need. The goal is better structure with exactly the same behavior.

03

Prove both sides

Submit against hidden behavior tests and authored Roslyn limits for complexity, nesting, method length, and duplicate blocks.

Stuck after a serious attempt? 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

Before you open the first kata

What are C# refactoring exercises?

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.

Can I practice the Gilded Rose kata in C#?

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.

Are the C# refactoring exercises free?

Six refactoring katas are free previews, including Gilded Rose, Tennis Score, null-check flattening, and duplication removal. Katabench Pro unlocks all 54 exercises and unlimited submissions.

Do the refactoring exercises include solutions?

Yes. You can reveal a correct reference solution after trying an exercise, study the resulting structure, then reset the kata and solve it again. Daily reveal limits depend on your plan.

How does Katabench grade clean code?

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.

Refactor the mess. Keep every behavior. Beat every gate.

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

Get new puzzles and .NET tips in your inbox

A short note when fresh kata land, plus the C# and performance tricks behind the grading. No spam, unsubscribe anytime.