Skip to content

Techniques: composing functions

Refactorings that split long functions into named pieces, or undo pieces that add nothing.

7 guides47 min in totalRefactoring

The guides, in order

Read them top to bottom: each one builds on the ones before. The ones you've read carry a check.

  1. Extract function: give a fragment a nameThe most used refactoring: turn a block of code that does one thing into a function named after what it does, step by step and without changing behaviour.4 min readStart here
  2. Inline Function: Remove Indirection That Adds NothingLearn how to safely inline a function whose body is as clear as its name, with step-by-step guidance and examples in TypeScript, Python and PHP.6 min read
  3. Extract Variable: Give a Complex Expression a NameLearn how to break a dense conditional or calculation into well-named local variables so the code explains itself, with safe steps and examples.7 min read
  4. Split Loop: One Loop Doing Two Jobs Becomes Two LoopsLearn how to split a loop that computes two unrelated results into two loops, so each one can be named, extracted and tested on its own.7 min read
  5. Slide Statements: Group Related Code Before You Extract ItLearn how to slide related statements next to each other so a long function splits cleanly into extractable blocks, with TypeScript, Python and PHP examples.7 min read
  6. Replace Loop with Pipeline: Turn Loops into map, filter and reduceLearn how to replace a loop that filters, transforms and accumulates data with a readable map/filter/reduce pipeline, step by step, in TypeScript, Python and PHP.7 min read
  7. Replace Function with Command: Turn a Long Function into an ObjectLearn how to move a long function into its own class so you can split its steps into small methods that share state through fields instead of parameters.9 min read

About this group

What you'll find
Extract function, inline function, extract variable, slide statements, split loop, replace loop with pipeline and replace function with command.
It's for you if…
you want to break up a long method in small, safe steps rather than rewriting it in one go.
How each guide works
Smell guides cover the signs, an example, why it happens, the treatment with its refactorings, what you gain and when it's not worth fixing. Technique guides show the problem, the code before and after, the steps, which smells it removes, its benefits and its cautions.