Code refactoring is the process of improving the the design of existing code without changing its external behaviour. You can see it as cleaning your code from bad smells (over complex solutions, unreadable code and so on).
What is Refactoring
You can see Refactoring as a controlled technique used for improving the design of existing code. By refactoring the code, you can improve the code readability and reduce its complexity.
A good program is as good as its code.
If the code of a program is easily readable and understandable then the program made by it is better as compared to a code which is messy.
The essence of refactoring is applying small coding changes that preserves the functionality. The small changes when combined together bring about a significant change.
Why You Might Want to Refactor?
- Refactoring your code makes it easier to understand for other people.
- It avoids duplications and hence you follow the Don't Repeat yourself (DRY) principle
- It reduces the code which is less maintenance.
- It passes all the tests.
- Debugging and maintenance is easier.
Problems the Refactoring Helps to Solve
- Improves the objective attributes of code(length, duplication, coupling ,etc.)
- Poor Code Quality
- Demotivated Team from unreadable code
- Low Bus Factor by simplyfing your code
How to Implement Refactoring
- Find the code which you use in many places.
- Create its own function.
- Call that function in the place instead.
- Repeat till you can not find any frequently used code
- Commit with the Refactoring label Keep in mind during refactoring
- If code is not cleaner refactoring is not done.
- Don't include new features.
- Try making small changes step by step.
- All tests should pass
Common pitfalls of Refactoring
- Lack of developer motivation to do these small changes
- Expensive and risky in the view of management.
- Bug introduction is quite common.
- Developer start adding new features, rewriting the code, or fixing bugs during refactoring.
- Does not mean fixing the bugs