Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Refactoring

Рефакторинг

It means reorganizing existing code without changing what users or other parts of the program can observe. The goal is to make the code easier to understand and change.

What refactoring means

Refactoring changes how code is organized, not what it does from the caller's point of view. You might rename a local variable, move a calculation into a helper, or replace nested branches with early returns. The key question is whether callers can still rely on the same behavior. That includes more than return values: if a function writes a record, raises a particular error, or publishes an event, those effects matter too.

What it looks like in practice

Start by identifying the behavior you need to preserve. Run the existing tests before editing so you know your starting point. If coverage is thin, add tests that capture what the code currently does at a stable boundary, such as a public method or API endpoint. Then make one focused change at a time and rerun the relevant checks. Prefer tests that assert outcomes over tests that depend on which private helper gets called; otherwise a harmless move can break the tests. Passing tests give you evidence about the cases they exercise, not proof that every possible case is unchanged.

Example: make a decision easier to read

Suppose a checkout function offers free shipping to members and to anyone spending at least 5,000 units. Naming the condition makes the policy easier to spot:

# Before
def shipping_cost(total, is_member):
    if total >= 5000 or is_member:
        return 0
    return 300

# After
def shipping_cost(total, is_member):
    qualifies_for_free_shipping = total >= 5000 or is_member
    return 0 if qualifies_for_free_shipping else 300

The rule is unchanged. For a non-member, test just below the threshold and at the threshold; for a member, test an order below the threshold. Changing >= to > would change what a non-member pays at exactly 5,000, so that edit wouldn't be a refactoring.

When to do it and what to avoid

A small refactoring often pays off just before a feature change: isolate the rule you need to alter, verify that it still behaves as before, and then change the rule in a separate step. Separate commits let reviewers distinguish moving code from changing product behavior. Avoid extracting a shared helper just because two blocks look alike. If they implement independent policies, coupling them can make the next change harder. A bug fix and a refactoring can belong in the same pull request, but the fix deliberately changes behavior and should be easy to identify.

How it comes up in interviews

An interviewer may hand you a working but tangled function and ask you to clean it up. Before editing, state what must stay the same and name a few checks, including an edge case. Explain why each change helps: for example, naming a business rule makes the function easier to read without adding an abstraction nobody needs.