- Section: Summary
- Section: Goal
- Section: Benefits
- Section: What are my responsibilities
- Section: How do I go about it
- Section: Hot Tips and Tricks
- Section: F.A.Q.
- Section: Other Resources
- Section: Back To Other Documents
Refactoring is the practice of changing the internal structure of code without changing its external behavior. Because of possible subtle errors that can occur, refactoring is only refactoring when guided with automated tests.
The goal of refactoring is to improve the maintainability of the code. The intent is that the difficulty to change the code remains both a constant and a minimum over the lifetime of the code. You do this by removing micro-architecture errors called "smells". You will never be able to remove all the micro-architecture errors, so you want to target those that are most prominent.
- Reduction in maintenance costs by improving understanding
- This makes code faster to modify
- This reduces the introduction of bugs
- Faster implementation of new features
- Easier on-boarding of new hires
The customer has the responsibility to not pressure the team to skip refactoring with-out a very good short term reason and a plan to pay back the effort later.
The coach has a responsibility to ensure that the team is thinking about constant refactoring opportunities. They are also responsible for protecting the team from external pressures that may lead to refactoring being skipped.
The development team is responsible for ensuring that refactoring is happening. Much as a chef cleans as they work, so must the development team. They also must take time to learn and experiment with skills that will enable them to be better at this activity.
An Integrated Development Environment (IDE) is a tool that makes development easier. Many of these tools have built in refactoring options and/or have plug-ins that provide those options. These will automate the steps taken in a refactoring.
It is worth noting, that these tools are just software and can have bugs just like any other software. As, such, performing an automated refactorings without viability of proof or automated tests is not refactoring even if the tool calls it such.
I will provide a link to an article.
“Pave the Way” is one of the best techniques to know where to start. It allows you to focus on accomplishing a goal and narrows the view of what to work on.
The second-best place to start, is cleaning up a change you just made. This is the inverse of “Pave the Way” as the focus is now that things work let’s make it a little better.
Knowing when to stop initially seems as daunting as knowing when to start. Once you get familiar with code smells you will always see them. It is nearly impossible to make code 100% clean. The trick here is staying focused, here are a few guidelines to help:
- Keep to small refactorings that can be timeboxed.
- Refactor with intent to accomplish something else, aka Pave the Way.
- Keep to the rule of Three.
If you are worried about how you find the time, you are thinking too big. Remember to be considered a refactoring you must be as sure as possible that you do not change external behavior. The best way to ensure you don’t change external behavior is to focus on small changes. Small changes over time can have large effects to the quality of your code base. I recommend that you work refactoring in as part of any change you do.
Do something before the change that makes your change easier. Maybe then tackle one or two items in the same area. Commit these changes separately from the change you were there to make in the first place. This makes life easier before starting.
Then, as you make changes look for where you can improve what you just did. Clean up a little. This will go a long way to ensure that the code base’s quality at least remains the same. If done in conjunction with the initial clean up, then you guarantee that the code base’s quality is improving every time you touch it.
Provable Commits(Article)
Refactoring: Improving the Design of Existing Code (2nd Edition) by Martin Fowler(Amazon Book Link)
Refactoring to Patterns by Joshua Kerievsky(Amazon Book Link)
Working Effectively with Legacy Code by Michael Feathers(Amazon Book Link)
Code Smells(Wikipedia Article)
Code Smells Catalog(Interactive Website)