What is refactoring and why do we need it?
By refactoring we mean changes in the application that aim to maintain high quality source code. Why do we need refactoring? Answer for this question is not simply. Most projects that want to stay on the market and be popular needs a lot of changes to meet customers requirements. Some of these changes are developed in a quick way and as long as we do this our code becomes incomprehensible. Sometimes changes in source code are “hacks” to provide them as quick as possible.
To keep source code easy to maintain and understand by other developers (and ourselves) we need to do refactoring. In short, we can describe it as a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.
What are the pros of refactoring?
- Makes code readable for developers
- Allows for better maintenance
- Increases efficiency
- Reduces complexity
- Allows for cost reducing in future
What kind of refactoring do we have?
We can list three basic types of refactoring:
- Small – these one contains small changes that improves code readability without huge changes in structure of application
- Advanced – bigger changes where we change source code in a drastic way like application structure changes etc.
- Full refactoring – in this case we mostly mean application rewrite
You have to remember that on every higher level there is a bigger risk of application destroy.
How can we achieve successful refactoring?
- Treat refactoring as a part of normal work
- Be sure that you have appropriate code coverage
- Start refactoring from methods not whole classes or modules
- Run tests after every refactoring iteration
- Stick to the rules like SOLID, KISS, DRY, YAGNI, Separation of Concerns