Skip to main content
CleanCodeMastery

Code Refactoring Techniques

Practice safe code refactoring techniques with step-by-step C# examples that improve readability, reduce duplication, and clean up messy production code.

61 lessons · follow them in order, or jump to what you need.

beginner

Extract Method: Turn One Giant Function into Small Named Helpers

Learn Extract Method step by step. Pull a messy block out of a long function, give it a clear name, and make your code read like a clean to-do list.

26 minrefactoring · composing methods
beginner

Inline Method: Remove the Shortcut That Was Never a Shortcut

Learn Inline Method step by step. When a tiny method's body is clearer than its name, fold it back into the caller and remove one useless hop from your.

22 minrefactoring · composing methods
beginner

Extract Variable: Solve the Big Sum with Small Named Steps

Learn Extract Variable step by step. Break one giant, confusing expression into small named parts, just like showing your working in a maths copy.

22 minrefactoring · composing methods
beginner

Inline Temp: Throw Away the Rough Note You Used Only Once

Learn the Inline Temp refactoring with a simple rough-paper story, TypeScript and C# examples, safe steps, IDE shortcuts, and when not to inline.

23 minrefactoring · inline temp
beginner

Replace Temp with Query: Ask Fresh, Don't Trust Yesterday's Chit

Learn Replace Temp with Query with a canteen story, TypeScript and C# examples, and safe steps.

24 minrefactoring · replace temp with query
beginner

Split Temporary Variable: One Bucket Cannot Do Two Jobs

Learn Split Temporary Variable with a two-buckets story, TypeScript and C# examples, and safe steps.

25 minrefactoring · split temporary variable
beginner

Remove Assignments to Parameters: Never Scribble Over a Borrowed Notebook

Learn the Remove Assignments to Parameters refactoring with a borrowed notebook story, TypeScript and C# examples, and safe steps for beginners to follow.

29 minrefactoring · remove assignments to parameters
beginner

Replace Method with Method Object: Give the Big Dish Its Own Kitchen Station

Learn Replace Method with Method Object through a wedding kitchen story, TypeScript and C# examples, and safe steps to untangle long methods for beginners.

29 minrefactoring · replace method with method object
beginner

Substitute Algorithm: Take the New Straight Road to School

Learn the Substitute Algorithm refactoring with a cycle-route story, TypeScript and Python examples, and the test-first safety rules every beginner needs.

29 minrefactoring · substitute algorithm
beginner

Move Method: Shift Work to the Class Where It Truly Belongs

Learn the Move Method refactoring through a simple school story. Shift a method into the class whose data it uses most so behaviour and data stay together.

25 minrefactorings · moving-features
beginner

Move Field: Keep Data in the Room Where It Is Used

Learn the Move Field refactoring with an easy school story. Move a piece of data to the class that really uses it, so state and behaviour live side by.

24 minrefactorings · moving-features
beginner

Extract Class: Give an Overworked Class a Helping Partner

Learn the Extract Class refactoring with a fun school office story. Split one overloaded class into two focused classes, each with a single clear job.

23 minrefactorings · moving-features
beginner

Inline Class: Merge a Class That Does Too Little

Learn the Inline Class refactoring through a school committee story. Merge a class that does too little back into its user and remove useless indirection.

22 minrefactorings · moving-features
beginner

Hide Delegate: Ask the Monitor, Let the Monitor Do the Running

Learn the Hide Delegate refactoring with a story about a class monitor who finds your homework for you.

20 minrefactorings · hide-delegate
beginner

Remove Middle Man: When the Peon Only Forwards, Meet the Principal Directly

Learn the Remove Middle Man refactoring with a story about a school peon who forwards every question to the principal without adding anything.

20 minrefactorings · remove-middle-man
beginner

Introduce Foreign Method: The Stapler You Keep in Your Own Bag

Learn the Introduce Foreign Method refactoring with a story about a school photocopy machine that has no stapler.

20 minrefactorings · introduce-foreign-method
intermediate

Introduce Local Extension: Build a Cabin Next to the Rented Shop

Learn the Introduce Local Extension refactoring with a story about building an attached cabin beside a rented shop you cannot modify.

22 minrefactorings · introduce-local-extension
beginner

Self Encapsulate Field: Let One Gatekeeper Guard Your Data

Self Encapsulate Field explained simply — why a class should read and write its own fields through getters and setters, with safe steps, TypeScript and C#.

23 minrefactoring · self encapsulate field
beginner

Replace Data Value with Object: Give Your Data a Proper Home

Replace Data Value with Object explained simply — how to grow a plain string or number into a small class with validation and behaviour, with TypeScript.

22 minrefactoring · replace data value with object
intermediate

Change Value to Reference: One Office File, Not Twenty Photocopies

Change Value to Reference explained simply — why duplicate copies of the same entity go stale, and how a shared single instance via a registry.

23 minrefactoring · change value to reference
intermediate

Change Reference to Value: Any ₹10 Note Is As Good As Another

Change Reference to Value explained simply — how to turn a shared, mutable reference object into a small immutable value object with content-based.

24 minrefactoring · change reference to value
beginner

Replace Array with Object: Give Every Slot a Name

Replace Array with Object explained simply — why an array with secret positions like row[0], row[1], row[2] causes bugs, and how a class with named fields.

24 minrefactoring · replace array with object
beginner

Encapsulate Field: Let the Object Guard Its Own Data

Encapsulate Field explained simply — why public fields let any code corrupt an object's data, and how private fields with getters and setters put.

25 minrefactoring · encapsulate field
intermediate

Encapsulate Collection: Stop Handing Out the Live List

Learn the Encapsulate Collection refactoring with before/after C# examples, safe steps, trade-offs, and clean-code guidance for maintainable software.

24 minrefactoring · encapsulate collection
beginner

Replace Type Code with Class: Give Every Magic Number a Proper Badge

Learn the Replace Type Code with Class refactoring with a school house story, before/after TypeScript, C# smart enums, and a clear guide on when to pick.

26 minrefactoring · type code
beginner

Replace Type Code with Subclasses: When Each Kind Truly Behaves Differently

Learn the Replace Type Code with Subclasses refactoring with a day-scholar/boarder/hosteller story, switch-removal in TypeScript and C#, and the decision.

26 minrefactoring · type code
intermediate

Replace Type Code with State/Strategy: When the Type Itself Can Change

Learn the Replace Type Code with State/Strategy refactoring with a prepaid-to-postpaid SIM story, swappable plan objects in TypeScript and C#.

27 minrefactoring · type code
beginner

Decompose Conditional: Turn a Confusing Rule into a Simple Name

Learn the Decompose Conditional refactoring with a school circular story, simple TypeScript and C# examples, safe steps, and handy IDE shortcuts.

23 minrefactoring · simplifying conditionals
beginner

Consolidate Conditional Expression: Many Small Checks, One Clear Question

Learn the Consolidate Conditional Expression refactoring with a school-gate story, TypeScript and C# examples, safe steps, and the side-effect rule.

21 minrefactoring · simplifying conditionals
beginner

Consolidate Duplicate Conditional Fragments: Move the Dessert Counter Outside

Learn the Consolidate Duplicate Conditional Fragments refactoring with a canteen story, TypeScript and C# examples, safety rules, and easy step-by-step.

21 minrefactoring · simplifying conditionals
beginner

Remove Control Flag: Stop Searching Once You Have Found It

Learn the Remove Control Flag refactoring with a watchman story, TypeScript and C# examples, break and return steps, plus the single-exit debate explained.

24 minrefactoring · simplifying conditionals
beginner

Replace Nested Conditional with Guard Clauses: Flatten the Arrow

Learn the Replace Nested Conditional with Guard Clauses refactoring with a temple queue story, early returns that flatten arrow-shaped code, and safe.

23 minrefactoring · guard clauses
intermediate

Replace Conditional with Polymorphism: Give Every Kind Its Own Desk

Learn the Replace Conditional with Polymorphism refactoring with C# examples, safe steps, trade-offs, and practical clean-code guidance.

23 minrefactoring · polymorphism
intermediate

Introduce Null Object: Give 'Nothing' a Polite Stand-In

Learn the Introduce Null Object refactoring with before/after C# examples, safe steps, trade-offs, and clean-code guidance for maintainable software.

23 minrefactoring · null object pattern
intermediate

Introduce Assertion: Taste the Dal Before You Serve It

Learn the Introduce Assertion refactoring with a careful cook's story, hidden assumptions turned into executable checks, Debug.Assert in C# and asserts.

25 minrefactoring · assertions
beginner

Rename Method: Fix the Shop Board So It Tells the Truth

Rename Method explained simply — why a method's name must say what the method really does, how to rename safely with a delegating old method, and how IDEs.

24 minrefactoring · rename method
beginner

Add Parameter: One More Detail on the Tiffin Order Slip

Add Parameter explained simply — how to give a method a new piece of information it now needs, why an explicit parameter beats hidden global state.

24 minrefactoring · add parameter
beginner

Remove Parameter: Delete the 'Telegram Address' Field from the School Form

Remove Parameter explained simply — how to safely delete a parameter the method no longer uses, why dead parameters mislead readers and burden every.

23 minrefactoring · remove parameter
intermediate

Separate Query from Modifier: Asking Your Bill Must Not Add Chai to It

Separate Query from Modifier explained simply — split a method that both answers a question and changes state into a pure query and a separate command.

25 minrefactoring · separate query from modifier
beginner

Parameterize Method: One Juice Recipe with a Size Input

Learn the Parameterize Method refactoring with before/after C# examples, safe steps, trade-offs, and clean-code guidance for maintainable software.

26 minrefactoring · parameterize method
beginner

Replace Parameter with Explicit Methods: Name Boards Instead of Secret Codes

Learn the Replace Parameter with Explicit Methods refactoring with a bank counter story, TypeScript and Python examples, safe mechanics, and the seesaw.

25 minrefactoring · replace parameter with explicit methods
beginner

Preserve Whole Object: Show the Whole ID Card

Learn the Preserve Whole Object refactoring with a school ID card story, TypeScript and C# examples, safe step-by-step mechanics, and an honest look at.

25 minrefactoring · preserve whole object
beginner

Replace Parameter with Method Call: Don't Tell the Shopkeeper His Own Prices

Learn the Replace Parameter with Method Call refactoring with C# examples, safe steps, trade-offs, and practical clean-code guidance.

26 minrefactoring · replace parameter with method call
beginner

Introduce Parameter Object: Hand Over One Address Card, Not Five Answers

Introduce Parameter Object explained simply — why the same group of parameters travelling together through many methods is a hidden concept.

24 minrefactoring · introduce parameter object
beginner

Remove Setting Method: Some Things Are Written in Ink, Not Pencil

Remove Setting Method explained simply — why a field that should never change after construction must not have a setter, and how read-only fields.

25 minrefactoring · remove setting method
beginner

Hide Method: The Secret Masala Stays Inside the Kitchen

Hide Method explained simply — why a method that only the class itself uses should not sit on the public menu, and how lowering visibility to private.

25 minrefactoring · hide method
beginner

Replace Constructor with Factory Method: Order a Named Thali, Let the Kitchen Decide

Learn the Replace Constructor with Factory Method refactoring with a thali restaurant story, before/after TypeScript and C#, safe step-by-step migration.

26 minrefactoring · factory method
beginner

Replace Error Code with Exception: Stop Whispering Failures, Announce Them

Learn the Replace Error Code with Exception refactoring with C# examples, safe steps, trade-offs, and practical clean-code guidance.

27 minrefactoring · exceptions
beginner

Replace Exception with Test: Read the Board Before You Walk In

Learn the Replace Exception with Test refactoring (Replace Exception with Precheck) with a canteen story, before/after TypeScript and C#, TryParse-style.

26 minrefactoring · exceptions
beginner

Pull Up Field: One Notice Board for Information Everyone Shares

Learn the Pull Up Field refactoring with before/after C# examples, safe steps, trade-offs, and clean-code guidance for maintainable software.

26 minrefactoring · pull up field
beginner

Pull Up Method: One Instruction Sheet for the Whole School

Learn the Pull Up Method refactoring with before/after C# examples, safe steps, trade-offs, and clean-code guidance for maintainable software.

26 minrefactoring · pull up method
beginner

Pull Up Constructor Body: One Shared Morning Routine, Then Your Own First Period

Learn the Pull Up Constructor Body refactoring with a school morning-assembly story — move duplicated initialization from subclass constructors into.

26 minrefactoring · pull up constructor body
beginner

Push Down Field: Swimming Pool Timings Belong on 7C's Board Only

Learn the Push Down Field refactoring with a school notice-board story — move a superclass field that only some subclasses use down into exactly those.

27 minrefactoring · push down field
beginner

Push Down Method: Move a Method to the Subclass That Really Uses It

Learn the Push Down Method refactoring with a school-office story, honest superclass contracts, safe step-by-step moves in TypeScript and C#, and how it.

23 minrefactoring · push down method
beginner

Extract Subclass: Give the Special Case Its Own Class

Learn the Extract Subclass refactoring with a tailor-shop story about urgent orders, flag-removal in TypeScript and C#, safe step-by-step moves.

22 minrefactoring · extract subclass
beginner

Extract Superclass: One Common Rulebook for Twin Classes

Learn the Extract Superclass refactoring with a science-lab/computer-lab story, pull-up moves in TypeScript and C#, the superclass-vs-interface decision.

23 minrefactoring · extract superclass
beginner

Extract Interface: One Common Form, Many Different Workers

Learn the Extract Interface refactoring with before/after C# examples, safe steps, trade-offs, and clean-code guidance for maintainable software.

23 minrefactoring · extract interface
intermediate

Collapse Hierarchy: When Parent and Child Classes Become the Same

Learn the Collapse Hierarchy refactoring with a housing-society committee story, step-by-step merging of a superclass and subclass in TypeScript and C#.

24 minrefactoring · inheritance
intermediate

Form Template Method: One Recipe Card, Many Biryanis

Learn the Form Template Method refactoring with a two-aunties biryani story, step-by-step extraction of a shared algorithm skeleton in TypeScript.

25 minrefactoring · template method
intermediate

Replace Inheritance with Delegation: Rent the Counter, Don't Inherit the Shop

Learn the Replace Inheritance with Delegation refactoring with a sweet-shop inheritance story, the honest meaning of composition over inheritance.

27 minrefactoring · inheritance
intermediate

Replace Delegation with Inheritance: When the Helper Should Become the Apprentice

Learn the Replace Delegation with Inheritance refactoring with a tailor-shop helper story, the Middle Man smell, the strict is-a conditions that must hold.

26 minrefactoring · delegation