How to Refactor
When you have to add a new requirement to an existing code, first you should check if the code is open to the new change. Open means that the code is following the SOLID principle open for extension and closed for modification.
If the code isn’t open yet, you should first refactor it to make it open, and once done you can add the new code.
To refactor to make it open, you should be guided by the code smells. Identify the code smells in the code and remove them using the refactoring recipes. Remember always fix the easier problem first.
To do the refactoring you should always rely on the test, so you can be sure you never introduce a bug in the code. Do just one change at a time, and refactor always under green.
If you are not able to identify the abstractions that will make your code open, you should use the flocking rules to iteratively identify and extract them.
Once identified the new abstraction you should name it, so the code can be easily understood. The rule here is that the name of a thing should be one level of abstraction higher than the thing itself.
When you remove all the code smells in your code, it should be open to the new requirement, so will be time to add the new code.
- 99 Bottles of OOP book by Sandi Metz & Katrina Owen.
- Refactoring web and book by Martin Fowler.