πŸ‘¨β€πŸ’»
Software Engineering
Refactoring: Improving the Design of Existing Code
Refactoring: Improving the Design of Existing Code
  • Overview
  • Chapter 2
    • Defining Refactoring
    • Why Should We Refactor?
    • When Should We Refactor?
    • Problems with Refactoring
    • Refactoring, Architecture, and Yagni
    • Refactoring & the Wider Software Development Process
    • Refactoring & Performance
    • Going Further
  • Chapter 3
    • Bad Smells in Code
    • Smell: Mysterious Name
    • Smell: Duplicated Code
    • Smell: Long Function
Powered by GitBook
On this page
  • πŸ†• Preparatory Refactoring: Making it easier to add a new feature
  • 🧠 Comprehension Refactoring: Making Code Easier to Understand
  • πŸ—‘οΈ Litter-Pickup Refactoring
  • πŸ—ΊοΈ Planned vs Opportunistic Refactoring
  • Opportunistic
  • Planned
  • Should we separate refactoring into different commits/PRs?
  • πŸ“† Long-Term Refactoring
  • βœ… Refactoring in a Code Review
  • πŸ‘¨β€πŸ’Ό What Should I Tell My Manager?
  • β›” When Should I Not Refactor?
  1. Chapter 2

When Should We Refactor?

"Refactoring is something I do every hour I program"

The Rule of Three: The first time you do something, just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor.

πŸ†• Preparatory Refactoring: Making it easier to add a new feature

  • Would the work be easier if it was structured differently?

  • The best time to refactor can be before adding a new feature to the code base.

  • When fixing a bug, refactoring can improve the code and reduce the chances that it will appear again.

🧠 Comprehension Refactoring: Making Code Easier to Understand

  • Before code can be changed, it requires understanding of what it does.

  • By refactoring, we move the understanding from our head into the code itself.

  • This understanding will be preserved longer and be visible to our colleagues.

  • Refactoring leads to higher levels of understanding that would otherwise be missed.

πŸ—‘οΈ Litter-Pickup Refactoring

πŸ•οΈ Boy Scout Rule: Leave the camp site cleaner than when you found it.

  • A variation of comprehension refactoring

  • Code is understood, but the implementation is poor

    • e.g. logic is unnecessarily convoluted, duplicated, etc.

  • If we make the code a little better each time we pass through, over time it will get fixed.

  • Over the course of several months, this process can achieve large refactoring efforts without requiring planned refactoring.

πŸ—ΊοΈ Planned vs Opportunistic Refactoring

Opportunistic

Refactoring performed while adding a feature or fixing a bug.

  • It's part of the natural flow of programming.

  • Refactoring is NOT an activity that's separated from programming.

  • Preparatory, Comprehension, and Litter-Pickup Refactoring are all Opportunistic.

  • Both ugly & clean code need refactoring.

Planned

  • Planned refactoring should be rare.

  • If a team has neglected refactoring, if often needs dedicated time to get a code base into a better state.

  • Sometimes a problem area grows to a point where it requires concerted effort to fix.

Should we separate refactoring into different commits/PRs?

  • As with most things in software, "it depends"

  • Should be a Team decision.

  • It's only worthwhile if it makes life easier.

  • Advantages:

    • Reviewed & approved independently.

  • Disadvantages:

    • Refactorings are often closely interwoven with adding new features and it's not worth the time to separate them.

    • Removes the context for the refactoring, making the refactoring harder to justify.

πŸ“† Long-Term Refactoring

  • Most refactoring can be completed within a few minutes - hours at most. Large refactoring effort can take weeks or months.

  • We should be reluctant to do dedicated, planned refactoring here.

  • An Alternative to Planned Refactoring:

    • Agree to gradually work on the problem over the course of the next few weeks.

    • Anyone working in the "refactoring zone" applies little changes in the direction of improvement.

βœ… Refactoring in a Code Review

  • Code reviews help spread knowledge through a Team.

    • Experienced developers pass knowledge to those with less.

  • Refactoring helps when reviewing someone else's code.

    • Helps get more concrete results from a code review.

    • Suggestions are implemented there and then.

  • Refactoring in the context of a Review (PR) can be thought of as pair programming.

πŸ‘¨β€πŸ’Ό What Should I Tell My Manager?

  • Managers should be familiar with Design Stamina Hypothesis and encourage refactoring on a regular basis.

  • When Managers lack technical awareness, don't tell them.

    • Software Developers are professionals.

    • Our job is to build effective software as rapidly as we can.

    • We're paid for our expertise in programming new capabilities fast, and the fastest way is by refactoring.

β›” When Should I Not Refactor?

  • Sometimes refactoring isn't worthwhile.

  • If code is a mess, but we don't need to modify it, then there's little need to refactor it at that moment.

  • Refactoring benefits are realized when we need to understand how the code works.

  • Is it easier to rewrite then refactor? This decision requires good judgement & experience that can't be summarized into a simple rule or piece of advice.

PreviousWhy Should We Refactor?NextProblems with Refactoring

Last updated 11 months ago