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
Resources for Refactoring
Want to write for DXKB?
Feel free to contribute. People from DXKB community will be more than happy.
Code Coverage measures the percentage of source code lines that are covered by automated tests. If you have 90% CC, it means that 10% of the source code is not being tested at the moment.Read more
A Bus Factor measures the minimum number of team members who have to be hit by a bus to put the project in jeopardy. The goal is to increase your Bus Factor as much as possible.Read more
Git Flow is a specific branching system for Git. It helps the team to better control and add different project versions.Read more
Respectful Code Reviews
Respectful code reviews involve giving a constructive feeback.The main goal should be to help deliver a good quality final product.Read more
Proper Bug Reporting
Proper bug reporting is a crucial practice for development. It helps to understand where the product lacks its functionality or performance. Bug reports are descriptions of bugs found by testers.Read more