Description
Refactoring should be done in a series of small changes, each making the code better than earlier without breaking the functionality.
Here is a list of things that make refactoring done the right way.
- Refactoring should make code cleaner than earlier.
- Refactoring should not add new functionality to the existing code.
- Refactoring should not break the existing functionality.
- Refactoring should not break the existing test cases.
Refactoring should make code cleaner than earlier
- Refactoring with small changes and testing after each change, otherwise, the refactoring makes the code messier than it was before.
- Never include multiple changes into a single commit.
- However, there may be instances where improving the code becomes a disaster, which can happen with extremely sloppy code.
Refactoring should not add new functionality to the existing code
- Refactor a piece of code in small chunks and test after each change to make sure nothing is broken.
- Never mix any new features while refactoring the code.
Refactoring should not break the existing functionality
- Refactoring must improve the code quality and readability but should not change the functionality.
- Not just functionality, but performance should also be taken care of.
Refactoring should not break the existing test cases
- Always run the tests after each step of refactoring.
- All the tests must be passed, in case of any failures, fix them and proceed.
- Write new tests, update existing tests, or even delete the tests as needed, but make sure the tests are covering enough code.
Overall
We now know how to refactor a piece of code in software development.