Published on

Best practices for refactoring what to do and avoid

3 min read - 418 words
Authors
  • avatar
    Name
    Cédric RIBALTA
    Twitter
post image

Introduction 🌟

Refactoring is an essential practice to improve the quality of your code without changing its behavior. It helps simplify and optimize your code, but be careful not to fall into some common pitfalls! Here are the best practices to follow and the mistakes to avoid, with TypeScript examples.


What to Do:

1. Understand Before Changing

Before refactoring, take the time to fully understand the existing code. This helps avoid breaking important parts of the project. ⚠️

// Before refactoring: dense function
function calculateInvoiceAmount(items: Item[], discountRate: number): number {
  let total = 0
  for (let i = 0; i < items.length; i++) {
    total += items[i].price * items[i].quantity
  }
  let discount = total * discountRate
  return total - discount
}

// After refactoring: simplified and clearer
function calculateTotalAmount(items: Item[]): number {
  return items.reduce((total, item) => total + item.price * item.quantity, 0)
}

function applyDiscount(total: number, discountRate: number): number {
  return total - total * discountRate
}

function calculateInvoiceAmount(items: Item[], discountRate: number): number {
  const total = calculateTotalAmount(items)
  return applyDiscount(total, discountRate)
}

Explanation: This refactoring breaks down the logic into small, reusable functions. 🎯

2. Refactor in Small Steps

Never make massive changes all at once. 🐢 Take it slow to avoid breaking any functionalities.

3. Keep It Simple

The goal of refactoring is to simplify the code. If your refactor adds complexity, it’s better to revert the changes!

4. Follow Conventions

Refactoring doesn’t mean changing everything! Stick to the project’s conventions. Changing the coding style without reason can make the code harder to maintain. ⚙️

5. Automated Testing

Make sure your tests pass before and after each refactor. This protects you from hidden bugs. ✅

What to Avoid:

1. Over-Refactoring

Avoid trying to do too much. Unnecessary abstractions and massive changes are often counterproductive. 🚫

2. Too Many Changes in Coding Style

Switching between coding styles without reason creates confusion. Stick to a consistent style across the project. 🙅‍♂️

3. Unnecessary Abstractions

Don’t add layers of abstraction where they aren’t needed. It complicates the code for no good reason. 🌀

4. Inconsistencies

Inconsistent code is a frequent trap during refactoring. Keep a uniform structure so your code remains easy to understand and maintain. 📏

5. Not Understanding the Business Context

Refactoring without understanding the business context behind the code is risky. Know why the code is there and what problem it solves before changing anything. 💼

Conclusion

A good refactoring should simplify, clarify, and enhance code maintainability. However, poorly managed refactoring can introduce unnecessary complexity or errors. Always keep the ultimate goal in mind: make the code cleaner and more readable, while respecting conventions and the business context. 💡