Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page
  • How to make refactoring possible
  • When we should refactor

Was this helpful?

  1. Clean Code Patterns

Keep Refactoring

There are the following false assumptions about code:

  • it can be written well during when it is first implemented

  • should be changed only if required by business

  • not every class, file or function needs a test

We all know the code needs to be continuously integrated, we need to merge our code often to avoid long living branches in our source code repository. We know the code needs to be continuously deployed, to get feedback if the code is deployable and working properly. Also, we need to remember that the code needs to be contiously evolved and thus refactored.

How to make refactoring possible

In order to be able to refactor the code, we need to rely on tests. Tests are mandatory building blocks of software. If the application is not fully covered by tests, developers cannot consider a project as a success. Even when it was delivered on time.

The code needs to be maintainable. In other words, other people who did not develop the code, must be willing to change it. They need to do it in a confident way. If they change the code, they run the tests. If tests fail, they know they did something wrong and that they need to go back and rethink the change.

Sign of poor quality: "Applications or service is redeployed multiple times a day in order to fix a single bug."

When we should refactor

This needs to be discussed within the team. But the general idea is to make the code better with each pull request. There could be too many changes in a pull request. Therefore we should refactor only what is related to our changes.

If we got spaghetti code, we might end up with huge refactoring pull requests, because everything is connected to everything. These refactorings should replace spaghetti code with better structured code. So other pull requests will be not that big.

Those who are suggesting to put the refactoring changes into a special "refactoring" pull requests, to keep changes separate and easier code review process, might be wrong. They might ask you to put the refactoring changes into other pull requests, but it might be difficult to split the changes into multiple pull requests. The refactoring itself can be difficult. If we add one more difficulty, which is, how to split the changes to multiple pull request, we might end up not doing the refactoring at all. Because the refactoring become nearly impossible. Instead, suggest to and put the refactoring changes into separate commits.

PreviousRemove DuplicationsNextAlways Unit Test

Last updated 5 years ago

Was this helpful?