Let's continue our series of short posts about code refactoring! In it, we discuss technics and tools that can help you improve your code and projects.
Today we will discuss things to avoid during a refactoring session and why to avoid them.
Unlimited Scope
The first thing to avoid is to start refactoring without determining the scope of the change beforehand.
Without scope boundaries, it is harder to find a suitable strategy to asses the changes and distinguish the code that should not change.
Limited scope helps with making refactoring sessions smaller but more frequent and cheaper.
Lack of Tests
Refactoring without tests is dangerous. A good test suite is a safety net that shows what we broke during refactoring right after it's been broken.
The type of tests depends on the situation and is not as important as their existence and good coverage.
Large Change Sets
Large change sets are more challenging to review and find issues in them. If we want to improve the code, we need the pull request to be reviewed. So our job is to make the reviewer's job easier.
Splitting big chunks into smaller ones helps with evolving code gradually and not spending too much time on a refactoring session.
Adding Features
Refactoring must not change the functionality of the code. So it is better to avoid adding new features or fixing bugs along with refactoring.
If we found a bug, we should postpone the refactoring, fix the bug, and then get back to improving the code.
More Details in My Book
In this post, I did not mention other things to avoid, like “mixing various technics in a single commit” or “refactoring test code together with the production code.” I also omitted the details about why we should follow these rules and how to follow them more easily.
If you want to know more about the reasons behind this advice, how to make refactoring easier, and improving code in general, I encourage you to check out my online book:
The book is free and available on GitHub. In it, I explain the topic in more detail and with more examples.
Hope you find it helpful! Enjoy the book 🙌
Top comments (0)