# The When, Why, and How of Refactoring in Agile | LinearB Blog

> Refactoring in agile isn’t only a nice activity, but a vital technique to ensure code quality. Learn more about it in this post.

_This is a markdown rendering of a live HTML page on linearb.io, generated for AI/LLM consumption — it is not a markdown-only site. To get the full HTML page instead, request this URL with an explicit `Accept: text/html` header (no wildcard, no markdown preference)._

[Blog](https://linearb.io/blog)

/

The When, Why, and How of Refactoring in Agile

# The When, Why, and How of Refactoring in Agile

![Photo of Carlos Schults](https://assets.linearb.io/image/upload/c_limit,w_2560/f_auto/q_auto/v1/logo-mark-lg?_a=BAVMn6ID0)

By [Carlos Schults](https://linearb.io/blog/refactoring-in-agile#carlos-schults)

|

January 10, 2022

![valery_fedotov_Cq_X6_Ih_Vj2_TI_unsplash_scaled_f0340aabb8](https://assets.linearb.io/image/upload/c_limit,w_2560/f_auto/q_auto/v1/valery_fedotov_Cq_X6_Ih_Vj2_TI_unsplash_scaled_f0340aabb8?_a=BAVMn6ID0)

Newcomers to the concept of refactoring in agile are often confused by it. Isn’t it dangerous to change code that works? What’s the value in it? If you do it right, refactoring is not only safe but also beneficial.

This post will give you a comprehensive introduction to refactoring in agile. You’ll learn more about what it is, what its benefits are, and how and when you should do it.

## What Is Refactoring?

Let’s start by briefly covering some fundamentals of refactoring.

Martin Fowler literally wrote the book on refactoring—called, appropriately enough, _Refactoring_. Here’s how[ he defines the practice](https://refactoring.com/):

_“Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.”_

In other words, it is changing the code in a way that doesn’t change what it does. Additionally, refactoring can be a noun that refers to every small transformation you can do to your code.

## Why Refactor Code?

Why on Earth would you want to change code that already works? In short, you refactor code to improve it: to make it easier to understand and change.

When refactoring, you can:

* remove[ code duplication](https://linearb.io/blog/code-duplication/%29),
* make code[ less complex](https://linearb.io/blog/what-is-code-complexity/) (e.g., by splitting a large class into smaller ones), and
* pay off[ technical debt](https://linearb.io/blog/handling-technical-debt-in-scrum) before starting a new feature.

Agile preaches continuous and incremental delivery of working software, so refactoring fits it perfectly.

In fact, it is a core tenet of extreme programming (XP). On the other hand, scrum doesn’t mandate that you refactor since it’s not prescriptive about engineering practices. But you can, of course, apply refactoring if your team uses scrum.

## How to Refactor Code?

With the “what” and “why” out of the way, the next big question is “how.”

### Learn the Existing Patterns

It’s important to remember that refactoring doesn’t mean simply changing code at random. Quite the contrary: there are refactoring patterns, each with a name and a clear set of instructions. They’re almost like small “recipes” you can use to make your code better!

Some of the most popular refactoring patterns include

* extract method,
* extract class,
* inline method,
* rename variable,
* consolidate conditional expression,
* remove dead code, and
* replace conditional with polymorphism.

The first step to refactoring is learning about those “recipes.” For that, you can read the book mentioned above by Martin Fowler or consult the[ online catalog](https://refactoring.com/catalog/).

### Have a Comprehensive Suite of Tests

Every time you make a change to a codebase, there’s a risk you’ll break something. Such is the nature of software. Such risk exists when you’re fixing a bug, introducing a new feature, and, yes, when you’re refactoring.

So, to refactor safely, you must have a safety net to protect you in case anything goes wrong. And the most trustworthy safety net you can have is a suite of automated tests, composed mostly of fast and reliable [unit tests](https://www.waldo.com/mobile-testing/types/automated-unit-testing).

With a comprehensive suite in place, you’ll be notified if you break something, making the refactoring process way safer.

Additionally, tools like LinearB can detect branches that contain a high level of code changes and a high level of rework or refactor. You should check out the high-risk work during the iteration in order to see which branches need your attention as a manager, more review, and more testing.

### Rely on Your IDE

As a software engineer, you should always strive to use automation to make your life easier. There’s no reason this shouldn’t apply to the refactoring process as well.

Nowadays, most modern IDEs can perform many of the most common refactoring patterns, such as extracting methods; renaming variables, functions, and classes (and automatically updating all references to said names); extracting classes; and so on.

The[ productivity boost](https://linearb.io/blog/ways-to-improve-developer-productivity/) you can obtain by using such features is considerable, so there’s no reason not to use them if they’re at your disposal.

## When Is the Ideal Time for Refactoring?

The last big question we have left regarding refactoring in agile is “when.” More specifically, people disagree on whether you should refactor during specific times or do it constantly. What’s correct?

I can understand why some people might prefer to refactor in batch—for instance, to allow finishing features earlier. But in my experience, reserving time for certain activities—be they refactoring, testing, or documentation writing—simply doesn’t work. More pressing issues will appear, and the other activity will be abandoned.

If it’s important, do it constantly. In his new book _Code That Fits In Your Head_,[ Mark Seemann](https://blog.ploeh.dk/) says:

“_In software development, later is never._“

I couldn’t agree more. Refactor constantly.

### The Role of Refactoring in TDD

I’ve already covered the close relationship between refactoring and unit tests—tests are essential for safe refactoring. Another way they’re are related is through test-driven development (TDD).

TDD is a software development methodology in which automated unit tests are used to drive the development of new features. It happens in an iterative cycle composed of three phases, usually called the red-green-refactor cycle:

* You start by writing a test for the feature you’ll implement. The test will fail since the feature doesn’t exist yet.
* Then, you write the simplest amount of code necessary for the test to pass.
* Finally, if needed, you refactor the code to remove duplication and fix other problems.

If you don’t have any experience working with TDD, I recommend you at least give it a try. By following the TDD cycle, you’re guaranteed to perform refactoring at least a few times a day.

### Is Refactoring a User Story?

Finally, one last question. Should refactoring sessions get their own separate task? I’ll assume you use scrum. Should you create refactoring stories, have the developers[ assign them points](https://linearb.io/blog/story-points-vs-hours/), and implement them during the sprint?

My opinion on this should be clear at this point. No, you shouldn’t. Refactoring doesn’t get to be a separate programming task. Much like unit testing, refactoring in agile is an essential activity in the software development process.

Here’s what[ Martin Fowler says](https://refactoring.com/):

“_Refactoring isn’t a special task that would show up in a project plan. Done well, it’s a regular part of programming activity._“

## Refactor Your Code; Improve Its Health!

Refactoring in agile is arguably more important than refactoring in other software development methodologies. After all, agile is all about constantly delivering software that works at a sustainable pace. Refactoring is a powerful enabler of constant delivery of working software.

In this post, we’ve discussed the definition of refactoring. You’ve learned more about how it’s done and why it’s valuable. I’ve also shared my views on when you should do it (all of the time).

Before I go, I’ll share another reason to prefer constant refactoring—especially if done in the context of TDD. It prevents excessive[ churn (or rework)](https://linearb.io/blog/what-is-code-churn/) after a file is initially created. How? If your process has refactoring built in, you’ll keep code quality high, avoiding the issues that cause unnecessary rework.

Is there something more agile than avoiding waste and focusing on what delivers the most value? I don’t think so.

[![Improve your engineering organization at every level with LinearB](https://assets.linearb.io/uploads/GenericLineaRB3Pillars-1024x497.png)](https://linearb.io/get-started/)Want to improve your engineering processes at every level? [Get started with a LinearB free-forever account today!](https://linearb.io/get-started/)

## Improve developer productivity with LinearB

Find us on

[](https://www.linkedin.com/company/linearb)
[](https://devinterrupted.substack.com/)

## Your next read

[![Cover image for Slack turns channels into the context engine for agentic AI](https://assets.linearb.io/image/upload/c_limit,w_2560/f_auto/q_auto/v1/Blog_Post_Name_2400x1256_077524ba8b?_a=BAVMn6ID0)](https://linearb.io/blog/slack-jaime-delanghe-mcp-agent-context-channels)

Workflow

[Slack turns channels into the context engine for agentic AI](https://linearb.io/blog/slack-jaime-delanghe-mcp-agent-context-channels)

Slack Chief Product Officer Jaime DeLanghe breaks down how channels serve as the foundational context layer for human-agent collaboration. Learn why Slack is...

[![Cover image for Code generation is faster than ever, but shipping value isn't](https://assets.linearb.io/image/upload/c_limit,w_2560/f_auto/q_auto/v1/Blog_AI_code_review_bottleneck_2400x1256_d73333ea46?_a=BAVMn6ID0)](https://linearb.io/blog/code-generation-faster-shipping-isnt)

Workflow

[Code generation is faster than ever, but shipping value isn't](https://linearb.io/blog/code-generation-faster-shipping-isnt)

Your team writes more code than ever, but less reaches production. AI moved the bottleneck to code review. Here's how to measure it and unblock your pipeline.&n...

[![Cover image for AI as a value multiplier: a human-centric approach to engineering leadership](https://assets.linearb.io/image/upload/c_limit,w_2560/f_auto/q_auto/v1/Blog_Servant_Leadership_2400x1256_0cfd4e2a0c?_a=BAVMn6ID0)](https://linearb.io/blog/ai-as-value-multiplier-human-centric-leadership)

Workflow

[AI as a value multiplier: a human-centric approach to engineering leadership](https://linearb.io/blog/ai-as-value-multiplier-human-centric-leadership)

Super.com's Matt Culver explains why AI should be used as a value multiplier, not a cost-cutter, advocating for a human-centric approach to engineering...

## Structured data

_Machine-readable metadata (JSON-LD) embedded in the page for search/AI context — not content rendered on the page itself._

```json
{
  "@context": "https://schema.org",
  "@type": "Organization",
  "name": "LinearB",
  "url": "https://linearb.io",
  "logo": "https://assets.linearb.io/image/upload/v1715628027/logo-mark-lg.svg",
  "description": "LinearB is the engineering productivity platform that helps engineering leaders prove AI is improving throughput without sacrificing delivery confidence, flow efficiency, or developer experience.",
  "sameAs": [
    "https://www.linkedin.com/company/linearb"
  ],
  "award": [
    {
      "@type": "Award",
      "name": "LinearB is a Leader in the 2026 Gartner® Magic Quadrant™ for Developer Productivity Insight Platforms",
      "dateAwarded": "2026",
      "awardedBy": {
        "@type": "Organization",
        "name": "Gartner®"
      }
    },
    {
      "@type": "Award",
      "name": "Great Place to Work Certification",
      "dateAwarded": "2025-2027",
      "awardedBy": {
        "@type": "Organization",
        "name": "Great Place to Work"
      }
    },
    {
      "@type": "Award",
      "name": "America's Best Startup Employers 2025",
      "dateAwarded": "2025",
      "awardedBy": {
        "@type": "Organization",
        "name": "Forbes Magazine"
      }
    }
  ],
  "hasCertification": [
    {
      "@type": "Certification",
      "name": "SOC 1 Type 2"
    },
    {
      "@type": "Certification",
      "name": "SOC 2 Type 2"
    },
    {
      "@type": "Certification",
      "name": "GDPR Compliance certification"
    },
    {
      "@type": "Certification",
      "name": "ISO 27001"
    }
  ]
}
```

```json
{
  "@context": "https://schema.org",
  "@type": "Article",
  "headline": "The When, Why, and How of Refactoring in Agile",
  "url": "https://linearb.io/blog/refactoring-in-agile",
  "author": {
    "@type": "Person",
    "name": "Carlos Schults"
  },
  "datePublished": "2022-01-10T14:31:14.000Z",
  "dateModified": "2022-01-10T14:31:14.000Z",
  "image": "https://assets.linearb.io/image/upload/v1720000000/valery_fedotov_Cq_X6_Ih_Vj2_TI_unsplash_scaled_f0340aabb8.jpg",
  "publisher": {
    "@type": "Organization",
    "name": "LinearB",
    "logo": "https://assets.linearb.io/image/upload/v1777485755/linearb-logo-2026.png"
  },
  "description": "Refactoring in agile isn’t only a nice activity, but a vital technique to ensure code quality. Learn more about it in this post.\n"
}
```

## More on linearb.io

### Top navigation

- [Book a Demo](https://linearb.io/book-a-demo)
- [AI Code Reviews — Catch security risks, bugs, and spec mismatches](https://linearb.io/platform/ai-code-reviews)
- [AI & Productivity Insights — See how AI tools affect cycle time and delivery speed](https://linearb.io/platform/ai-developer-productivity-insights)
- [Measure AI Impact — Track AI adoption and tie it to delivery outcomes](https://linearb.io/use-case/measure-ai-impact)
- [MCP Server — Chat with your data to spot patterns and boost output](https://linearb.io/platform/mcp-server)
- [Resource Allocation — Cost initiatives and shape your investment strategy](https://linearb.io/platform/resource-allocation)
- [Cost Capitalization — Capitalize engineering costs with audit-ready reports](https://linearb.io/platform/cost-capitalization)
- [Dev Team Management — Set targets and tie throughput to business outcomes](https://linearb.io/platform/goals-and-reporting)
- [DevOps Workflow Automation — Policy-based PR routing, approvals, and tests](https://linearb.io/platform/ai-workflow-governance)
- [AI Powered Support — Unify AI and human code delivery in one clear view](https://linearb.io/use-case/ai-powered-support)
- [Optimization — Surface friction with feedback and MCP insights](https://linearb.io/platform/developer-experience)
- [Reporting — Spot what's working and what needs attention](https://linearb.io/use-case/measuring-developer-experience)
- [Surveys — Turn developer feedback into actionable signals](https://linearb.io/platform/developer-surveys)
- [Platform overview](https://linearb.io/platform/overview)
- [Register now](https://linearb.io/event/engineering-productivity-gap)
- [Customers](https://linearb.io/customers)
- [Pricing](https://linearb.io/pricing)
- [Why choose LinearB — Explore your data. Measure performance. Act to improve it.](https://linearb.io/why-linearb)
- [APEX framework — The operating model for AI-era engineering teams](https://linearb.io/resources/apex-framework)
- [Anti-FAQ — The questions other vendors won't answer](https://linearb.io/why-linearb/anti-faq)
- [Security — Enterprise-grade compliance and zero code access](https://linearb.io/security)
- [Build vs. buy — The hidden cost of building it yourself](https://linearb.io/resources/build-vs-buy)
- [Dev Interrupted Podcast — Conversations with engineering leaders](https://linearb.io/dev-interrupted/podcasts)
- [Reports & Guides — Deep dives on productivity and delivery](https://linearb.io/resources)
- [Webinars — Expert sessions on productivity and AI](https://linearb.io/resources?category=workshops)
- [Metrics Benchmarks — See how your engineering org stacks up](https://linearb.io/resources/software-engineering-benchmarks-report)
- [Blog — Product updates and practical insights](https://linearb.io/blog)
- [Help Center — Documentation, setup, and support](https://linearb.helpdocs.io)
- [API Docs](https://docs.linearb.io/api-overview)
- [Status](https://www.linearbstatus.com/)
- [Integrations](https://linearb.io/integrations)
- [LinearB is a Leader in the 2026 Gartner® Magic Quadrant™ for Developer Productivity Insight Platforms](https://linearb.io/resources/gartner-magic-quadrant-dpi-platforms-2026)
- [Sign in](https://app.linearb.io/login)
- [Enterprise](https://linearb.io/solutions/enterprise)
- [Contact](https://linearb.io/contact-us)
- [About us](https://linearb.io/about-us)
- [Careers](https://linearb.io/careers)
- [Service agreement](https://linearb.io/services-agreement)
- [Privacy policy](https://linearb.io/privacy-policy)
- [DPA](https://linearb.io/data-processing-agreement)
- [Security FAQ](https://linearb.io/security-faq)
- [Substack](https://devinterrupted.substack.com/)

### Footer

_Additional links from the site footer, not repeated from the top navigation above._

- [GitHub](https://github.com/linear-b)
- [LinkedIn](https://www.linkedin.com/company/linearb)
- [Twitter](https://twitter.com/LinearB_Inc)