Skip to main content

Refactoring

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/refactoring

In short

Refactoring is the process of restructuring existing code to make it cleaner and easier to maintain without changing what the code does from the outside.

What is refactoring?

Refactoring means improving the internal design of code while keeping its external behavior exactly the same. Typical refactorings include renaming unclear variables, splitting a long function into smaller ones, removing duplicated code, and simplifying complicated conditions. The goal is code that is easier to read, test, and change in the future.

Refactoring is done in small, safe steps, and automated tests are what make it safe: you run them before and after each change to confirm nothing broke. Many code editors have built-in refactoring tools, such as rename symbol or extract function, that update every reference automatically.

A good analogy is reorganizing a kitchen: you move things to better places and label the drawers, but you still cook the same meals. Developers often refactor just before adding a feature, to make the change easier, or right after, to clean up. Refactoring is also the main way teams pay down technical debt.

Refactoring is different from rewriting. A rewrite throws away existing code and builds it again, which is slow and risky, while refactoring changes code gradually and keeps it working the whole time. It is also different from fixing bugs or adding features, since those intentionally change behavior.

Key takeaways

  • Refactoring changes the structure of code, not its behavior.
  • Work in small steps and run the tests after each one.
  • Common refactorings include renaming, extracting functions, and removing duplication.
  • It is the main tool for reducing technical debt.

Example

The same function before and after refactoringjavascript
// Before: unclear names, magic numbers, and duplicated logic
function calc(o) {
  if (o.type === "vip") return o.total - o.total * 0.2;
  return o.total - o.total * 0.05;
}

// After: same behavior, clearer names, no duplication
const VIP_DISCOUNT = 0.2;
const REGULAR_DISCOUNT = 0.05;

function calculateFinalPrice(order) {
  const rate = order.type === "vip" ? VIP_DISCOUNT : REGULAR_DISCOUNT;
  return order.total - order.total * rate;
}

Readers ask

What is the difference between refactoring and rewriting?

Refactoring improves existing code in small steps while keeping it working, whereas rewriting replaces the code with a new implementation. Refactoring is usually lower risk because the software keeps working and can be tested after every change.

When should you refactor code?

Good moments are just before adding a feature to code that is hard to change, right after getting a feature working, and during code review. Avoid large refactorings without tests, because you cannot easily confirm that the behavior stayed the same.

Does refactoring change functionality?

No. By definition, refactoring keeps the external behavior the same; if the behavior changes, it is a bug fix or a new feature, not a refactoring.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings