DEV Community

Cover image for 5 Code Refactoring Techniques to Improve Your Code
Suraj Vishwakarma for Documatic

Posted on • Edited on • Originally published at surajon.dev

5 Code Refactoring Techniques to Improve Your Code

Introduction

Writing code is fun, and we enjoy it a lot—until an error pops up from nowhere, taking valuable time to resolve. Sometimes, errors aren’t immediately visible because the code seems to run as expected, but these hidden issues can cause serious problems in production. Performance bottlenecks, mobile breaking, and accessibility issues can degrade user experience.

In this guide, we’ll explore practical techniques to help you modify existing code without breaking the logic.

What is Code Refactoring and Why Does It Matter?

Code refactoring involves making improvements to existing code without changing its external behavior. It’s an essential part of the software development process that enhances code readability, maintainability, scalability, and performance. Refactoring also increases developer productivity by reducing technical debt and simplifying future modifications.

Before diving into specific techniques, it’s important to understand how to make code refactoring a regular part of your development workflow:

  • Schedule dedicated time for refactoring: In early startups, it can be difficult to prioritize refactoring as fast delivery often takes precedence over code quality. But whenever you have time or any developer has free time, allocate that time to refactoring. Scheduling refactoring can reduce future stress, as when you have to deliver something on time. But due to technical debt, it become to hard to deliver.
  • Break down large refactoring tasks: Refactoring of one module can be broken down into component-level tasks to handle specific components one at a time, while also having the context of the whole module.
  • Involve the entire team: Implementing code standards throughout the team. This will help them to find which piece needs to be refactored and in which way it should be done.
  • Leverage automated tools: You can use tools such as ESLint or Coding agent with specific skills and context to automate identifying code smells and find refactoring opportunities. You can explore more tools in 5 Essential Tools for Code Refactoring in 2026.
  • AI Code refactor: Code written by AI can, most of the time, break coding standards and add additional complexity to a working code. This happens when you keep prompting to fix a particular issue for multiple iterations. You might see that the code works and push it to production. But later, you find it harder to identify and debug such code in the future. Refactoring such code to simplify will increase readability.

Note: Code smells are part of the code that requires refactoring.

Before You Refactor: Ensure Safety with Tests

Refactoring involves the modification of the existing code. It should be changing the structure without changing its behaviour. If you refactor and introduce new bugs, then it will increase complexity and also increases time spent in refactoring. To ensure you don't break logic and introduce bugs, follow:

  • Understand working: First, understand how the code works, what it does, what the edge cases are, and identify the test cases.
  • Testing: Write unit tests for critical logic. Do integration testing with the complete workflow.
  • Running test: Run the test after every small change.

Without tests, refactoring becomes risky, which can introduce new hidden bugs. Now, let’s explore some effective refactoring techniques you can apply to your projects.

1. Extract Method: Simplify Complex Code

The Extract Method is one of the most common and yet effective techniques that involves breaking down long, complex code blocks into smaller, more manageable, and reusable functions. This improves the structure, readability, and reusability of your code. The extract method improves the code in the following ways:

  • Readability: One of the issues that I found in my last job was that there were files with around 3000 lines of code in React with no comments. This makes it harder for me to read and understand the code that involves numerous functions and states. Dividing such files into smaller components will improves readibility for other developers. In this way, each component should perform a specific function.
  • Reusability: If you extract the logic of fetching usage stats of the app to another component. It can be used in other places without rewriting the code again.
  • Debugging: If each feature or related feature has a different component and function, it will become easy to debug the code.
  • Structure: The overall structure of a module increases as each component is easy to find, understand, and work on.

How It Works

  • Identification: Identify code blocks that perform specific tasks.
  • Extraction: Extract those blocks and place them in a separate method with a meaningful name.
  • Invocation: Replace the original code with a call to the new method.

Example

Before Refactoring

function calculateInvoiceTotal(items) {
  let total = 0;
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    if (!item.quantity || !item.price) {
      console.error('Invalid item', item);
      continue;
    }
    const itemTotal = item.quantity * item.price;
    total += itemTotal;
  }
  return total;
}
Enter fullscreen mode Exit fullscreen mode

After Refactoring

function calculateInvoiceTotal(items) {
  let total = 0;
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    const itemTotal = calculateItemTotal(item);
    total += itemTotal;
  }
  return total;
}

function calculateItemTotal(item) {
  if (!item.quantity || !item.price) {
    console.error('Invalid item', item);
    return 0;
  }
  return item.quantity * item.price;
}
Enter fullscreen mode Exit fullscreen mode

2. Replace Magic Numbers with Symbolic Constants

Magic numbers are hard-coded values with unclear meaning. Replacing them with named constants makes your code more readable and maintainable. It will make future developers or even AI understand why the specific number is being used.

How It Works

  • Identification: Identify hard-coded values that can be numerical or any other type.
  • Variable Creation: Create a variable that can store the value. Give a meaningful name for better understanding.
  • Replacing: Replace the value with the newly created variable.

Example

Before Refactoring

if (temperature > 32) {
  // Do something if temperature is above freezing
}
Enter fullscreen mode Exit fullscreen mode

After Refactoring

const FREEZING_POINT = 32;

if (temperature > FREEZING_POINT) {
  // Do something if temperature is above freezing
}
Enter fullscreen mode Exit fullscreen mode

Note: If the value is used once then adding a comment to that line can be prioritized as it reduces the line of code and complexity of maintaing another variable.

3. Eliminate Code Duplication

Duplicate code increases maintenance costs and the risk of bugs. A duplicate can pose a risk, such as:

  • Maintenance: In the future, when the features need to be updated, you have to update various places.
  • Bugs: One time, in my last job, one API got updated. It was previously sending 5 items by default, but with the update, it required a parameter to pass min=5 to send only 5; otherwise, it would send all the items. I was working on the Mobile version that has a different UI. So, I didn't change the desktop, and the code was pushed to production. The desktop was using the API to display a chart that got messed up. Thus, having a single code can help you identify where this function is being used and also updating at one place will reflect all the places.
  • Complexity: It adds extra complexity, as you can reuse existing code rather than writing all the code from scratch. This eventually also leads to an increase in bundle size due to duplicate codes.

Merging repeated code into a single, reusable function reduces complexity and improves code quality.

Example

Before Refactoring

function calculateTotal(numbers) {
  let total = 0;
  for (let i = 0; i < numbers.length; i++) {
    total += numbers[i];
  }
  return total;
}

function calculateAverage(numbers) {
  let total = 0;
  for (let i = 0; i < numbers.length; i++) {
    total += numbers[i];
  }
  const average = total / numbers.length;
  return average;
}
Enter fullscreen mode Exit fullscreen mode

After Refactoring

function calculateSum(numbers) {
  let total = 0;
  for (let i = 0; i < numbers.length; i++) {
    total += numbers[i];
  }
  return total;
}

function calculateTotal(numbers) {
  return calculateSum(numbers);
}

function calculateAverage(numbers) {
  const total = calculateSum(numbers);
  return total / numbers.length;
}
Enter fullscreen mode Exit fullscreen mode

4. Refactoring While Working on it

Most of the company does not allocate time for refactoring. Your senior developer/manager expects you to refactor a component whenever you work on it. Simplifying methods can be used as a refactoring technique in such scenarios. It makes your code easier to understand, maintain, and extend. Here are some of the ways to implement code simplification:

  • Remove unnecessary debug statements: Most of the time, debug logic is console.log pushed to production. You should remove those statements unless they affect global debug logs.
  • Remove unused variables: As the code progresses, some variables might not be needed. In such cases, remove the unused variables to reduce complexity.
  • Use built-in functions: If you could use a built-in function over custom code, then use that to reduce the number of lines of code.
  • Simplify conditional logic: Use ternary operators or combine conditions where possible.
  • Implement Current coding standards: As the coding progresses, standards also change. Try to implement the current coding standards in it.

Before Refactoring:

function getUserStatus(user) {
  if (user) {
    if (user.isActive === true) {
      if (user.role === "admin") {
        return "Active Admin";
      } else {
        return "Active User";
      }
    } else {
      return "Inactive";
    }
  } else {
    return "No User";
  }
}
Enter fullscreen mode Exit fullscreen mode

After Refactoring:

function getUserStatus(user) {
  if (!user) return "No User";
  if (!user.isActive) return "Inactive";

  return user.role === "admin" ? "Active Admin" : "Active User";
}
Enter fullscreen mode Exit fullscreen mode

5. Refactoring AI-Generated Code

AI tools like ChatGPT, Gemini, Claude, and others can significantly improve the development speed. However, AI-generated code prioritizes working solutions over clean architecture. AI-generated code may introduce:

  • Complexity: It can add unnecessary complexity by adding conditional logic to solve rather than simplifying the logic.
  • Violation: Most of the time, they may violate coding standards or design systems.
  • Duplicate Code: Generating new functions despite having such a function in another file.

How to Refactor AI-Generated Code

  • Duplication: Remove duplications by using the existing code.

  • Edge Cases: You have to manually review the edge cases. Many a time, they just write the working code without understanding the edge cases and context of the page.

Redesigning: If you provide UI as a screenshot, then you might need to work on the generated code to match the actual feel of the component.

Example (React Components)

Before Refactoring:

function process(data) {
  if (data) {
    if (data.items) {
      let result = [];
      for (let i = 0; i < data.items.length; i++) {
        if (data.items[i].price) {
          result.push(data.items[i].price * 2);
        }
      }
      return result;
    }
  }
  return [];
}
Enter fullscreen mode Exit fullscreen mode

After Refactoring:

function processItems(data) {
  if (!data?.items) return [];
  return data.items
    .filter(item => item.price)
    .map(item => item.price * 2);
}
Enter fullscreen mode Exit fullscreen mode

AI helps you write code faster, but refactoring ensures you can maintain it later.

Frequently Asked Questions

  1. What is the primary goal of code refactoring?

Code refactoring is done to make the code simpler, readable, and maintainable.

  1. How do I know if my code needs refactoring?

If your code is harder to understand by other developers or causes significant performance issue then refactoring is the solution to solve these issues.

  1. What are the risks of refactoring without tests?

Major risk involves the introduction of hidden bugs, more debugging, and not covering edge cases.

  1. Which tools are best for automated refactoring?

You can use ESLint and Coding Agent with specific skills to find code smells. You can also automate the refactoring by providing the necessary context. It still requires human verification for verification before pushing to production.

  1. How can I practice refactoring skills effectively?

Practice refactoring by regularly revisiting your own or open-source code, identifying code smells, and improving structure in small steps while ensuring behavior stays unchanged with tests.

Conclusion

Code refactoring is an essential practice for improving the quality, performance, and maintainability of your code. Regularly analyzing and optimizing your codebase allows you to eliminate redundancies, simplify complex logic, and build more scalable and efficient applications.

By applying techniques like Extract Method, replacing magic numbers, eliminating duplication, simplifying logic during development, and refining AI-generated code, you can write cleaner and more maintainable software.

Thanks for reading! If you found this article helpful, feel free to share it with your network or bookmark it for future reference.

Top comments (22)

Collapse
 
ant_f_dev profile image
Anthony Fung

Great article.

I might not always replace magic numbers with symbolic constants. If the number is used in more than one place, then it definitely deserves to be replaced.

But if it's used only in one place, I might leave a comment to describe what it is instead. Taking the example above:

if (temperature > 32) { // freezing point
  // Do something if temperature is above freezing
}
Enter fullscreen mode Exit fullscreen mode

results in less code and doesn't seem harder to read than:

const int FREEZING_POINT = 32;

if (temperature > FREEZING_POINT) {
  // Do something if temperature is above freezing
}
Enter fullscreen mode Exit fullscreen mode

In addition to extracting methods, I sometimes also move these methods into different classes during refactoring. This groups common functions and helps promote single-responsibility within classes.

This in turn helps when wanting to share code between many modules. A further positive is that it makes code more testable because we think more about what functions can/should be public vs private.

Collapse
 
richardforshaw profile image
Richard Forshaw

I have had many years development experience. As a writer and reader of code, and with a basic understanding of compilers, if faced with this example I will always chose readability over lines of code for these reasons:

  • It is only 'more code' in terms of written code. After compiling (if a compiled language), there is no difference in code. So this is really an invalid argument.
  • Comments do not break code and fail tests, so they are subject to a different set of rules; thus they will often be ignored, abused and left out-of-sync with code changes (esp with copy-pasta code). So having the literal meaning form part of the compiled code is always better.
  • As another commenter posted, comments are often written for the writer to understand, and may not be understood by all readers. code that is able to be compiled, on the other hand, must be understood by all parties out of necessity.
Collapse
 
basman profile image
Adam.S

You might want to consider the case that 32°F equates to 0°C. Your comment works well. But might confuse some.

Collapse
 
ant_f_dev profile image
Anthony Fung

Hi Adam

You make a good point. Comments are only useful if read and understood. It's important to use units/conventions that the team will understand.

For example, °C is predominantly used in UK, and °F in US. If the team is UK based, it's probably best to use °C. Likewise use °F if the team is US based. It the codebase is shared between multiple teams, the best choice might be to either use both, or discuss and choose the one that everyone can agree on.

Collapse
 
ekeijl profile image
Edwin • Edited

I would prefer to use constants in most cases, because it communicates the meaning (to other developers) of the number 32. You also get the opportunity to include a unit in there: FREEZING_POINT_C to indicate the temperature is in degrees Celsius (or K or F). When the next developer comes in and looks at this piece of code, you have saved them time because they don't need to waste brain cycles on what the 32 could mean.

IDEs can help you easily look up the value of the constant (hover/ctrl-click), if you're worried about the amount of code.

Collapse
 
ant_f_dev profile image
Anthony Fung

Hi Edwin

I'm not quite sure how else // freezing point could be interpreted in this one-use context. It wouldn't refer to temperature as that would have been named freezingPoint if that was the case. Those are the only two non-keywords on that line.

if (temperature > 32) { // freezing point
Enter fullscreen mode Exit fullscreen mode

As units are mentioned in your reply, it's also possible to add units to the comment:

if (temperature > 32) { // freezing point Fahrenheit (F)
Enter fullscreen mode Exit fullscreen mode

It's also important to remember that IDEs aren't always used to look at code. Some people might prefer using simple text editors; some people might be reviewing a Pull Request online.

Thread Thread
 
ekeijl profile image
Edwin • Edited

It's not about misinterpreting the comment. If code can be self-documenting (in this case using constants), it should be written that way. It makes comments superfluous. The reality is that people primarily read code and sometimes skip the comments or forget to update comments when refactoring code. Redundant comments that repeat the code 1:1 only waste peoples time.

Furthermore, I'm assuming the context of a professional team that works with this code on a regular basis, where IDEs are the norm. Even VCS services like Github have features to annotate variables/constants and instantly show their definitions, check out their new code search functionality.

Thread Thread
 
ant_f_dev profile image
Anthony Fung • Edited

I think we’ll agree to disagree on this one.

I can see the Clean Code background that this is coming from, and I respect that. I’m not convinced by every principle of Clean Code, but if someone’s coding style fits that of the repository that it’s contributing to (whether it's their own, their company's, or the OS project), that’s all that matters really.

I completely agree on redundant comments, e.g.

// Do work
DoWork();
Enter fullscreen mode Exit fullscreen mode

Is a redundant comment. In the example given, there is only one occurrence of ‘freezing point’. This makes the comment not redundant.

As for professional teams, I’ve been in teams that favour VI over offerings from Microsoft, JetBrains, and others. I could be wrong on this one, but I don’t think VI or the PR diff viewer on GitHub (and PRs will be done often in professional contexts) offers tooltips that show a variable’s declaration/type on mouse-hover. I’m sure in some contexts it’s possible to search/jump to the declaration, but I personally find doing that takes more effort than looking upward/rightward. That said, I appreciate and respect that different people have different optimal workflows.

Thread Thread
 
delacombo profile image
Jasond_Official

"The reality is that people primarily read code and sometimes skip the comments or forget to update comments when refactoring code."

...is not an excuse. If there are comments, you update (or remove) the comments when refactoring. Now, I know things get inherited, but it's all part of being a developer.

Collapse
 
mergerg profile image
Raphael

I'm confused as to how "it communicates the meaning to other developers" doesn't apply to comments. That's literally the point of comments. You can also include the units in the comment, pretty easily.

Collapse
 
developerdoran profile image
Jake Doran

I'd like to advocate that refactoring is an essential part of development and should be a consistent process. However, it is worth noting the risk posed by refactoring code that isn't covered by adequate tests.

That said, one of my favourite patterns (especially on legacy codebases) is return early. It really does help tidy up the code and make it easier to parse and understand 😋

Collapse
 
brense profile image
Rense Bakker

This ☝️

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Really great read.
Refactoring codes can be a big relief for developers. I feel like most developers don't venture into it because they feel it's complex and time consuming but with articles like this it just proves how simple it is to refactor.
Thanks for sharing

Collapse
 
satriopamungkas profile image
Muhammad Raihan Satrio Putra Pamungkas

Nice article.

Actually, there is a practical approach to determine which code should be refactored. Many people agree that the part of the code that needs to be refactored is called "code smells". The common way is to scan and look for potential code smells, and then proceed to the refactoring step.

In his book, Fowler has categorized several code smells, along with the corresponding refactoring techniques. To find out more, you can check his book "Refactoring".

Additionally, refactoring is also a part of the Test-Driven Development process. There is a step called "red-green-refactor", which I actually like.

Collapse
 
iamspathan profile image
Sohail Pathan

It's inspiring to see how refactoring can help elevate code to a higher level of quality and maintainability. Refactoring is an invaluable tool for every software developer

Collapse
 
maame-codes profile image
Maame Afua A. P. Fordjour

Saving this for later!

Collapse
 
surajondev profile image
Suraj Vishwakarma Documatic

How you refactor code?

Collapse
 
ekeijl profile image
Edwin
  1. Make a plan. You need to know what parts of the code are affected by your refactoring. For example, if you replace a library by another, you need to identify all the places in your code base where it is used and check if it can be replaced. Which parts have edge cases? Which parts should be done first? Can we skip refactoring parts and remove that whole piece of code alltogether? Having a plan allows you to estimate the time it takes, which you can communicate to your team. If it takes too much time to do a big-bang refactor, split it into smaller tasks and do module-by-module if that makes it easier to manage.
  2. Write tests for features you are refactoring. This part is missing from your article. You need to be sure that you do not change the functionality behind the refactored code. Writing unit/integration/e2e tests before you refactor will give you confidence that nothing will break.
  3. Refactoring should be a part of implementing new functionality, not as dedicated user stories. Please read this article. In short, when doing a planning session for new functionalities, identify parts of existing code that need to be refactored and allot time to clean that up.
Collapse
 
howtomakeaturn profile image
尤川豪 • Edited

only 4 techniques in the article .... ?

where is the 5th technique ... 🥹🥹🥹

Collapse
 
tspuujee profile image
Пүрэвжаргал

5th technique is be do nothing. (:

Collapse
 
bantangzm profile image
Tang Ziming

testing is important if you refactor

Collapse
 
mayronsouza profile image
Mayron_Souza

I'll thank you