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

Six free preview katas. No local setup required.

Katabench results for a refactoring kata. The Code quality list shows 1 of 4 rules passing: ShippingLabel has nesting depth 4 against a limit of 2, cyclomatic complexity 9 against a limit of 8, and 32 lines against a limit of 30, while the duplicate-block rule passes.
The Code quality rules after a Run on the Flatten the Null Checks starter, the code shown further down.
refactoring exercises
51
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.

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

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.

A C# refactoring example

What refactoring C# code looks like here

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.

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

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

21 katas

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

Flatten deep nesting

12 katas

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

Split long methods

13 katas

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

Remove duplication

5 katas

Find the stable shape behind pasted validators, renderers, 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. Easy

    Flatten the Null Checks

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

    Open this kata: Flatten the Null Checks Read the brief: Flatten the Null Checks
  2. Easy

    Dedupe the Field Validators

    Three fields, three identical validation blocks.

    Open this kata: Dedupe the Field Validators Read the brief: Dedupe the Field Validators
  3. Easy

    Split the Tennis Score

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

    Open this kata: Split the Tennis Score Read the brief: Split the Tennis Score
  4. Easy

    Tame the Grade Calculator

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

    Open this kata: Tame the Grade Calculator Read the brief: Tame the Grade Calculator
  5. Medium

    Tame the Discount Engine

    Untangle 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 Engine
  6. Hard

    Gilded Rose

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

    Open this kata: Gilded Rose Read the brief: Gilded Rose

The refactoring loop

Read the code, make one move, verify the evidence

1

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.

2

Refactor without a rewrite

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

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? 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

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 51 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. Revealing needs an account, and 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.

How do I practice refactoring C# code?

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.

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