Refactoring
Clean Code
Clean code is achieved through refactoring to tackle technical debt. Refactoring converts messy code into clean and more readable code.
Properties of Clean Code
It's widely understandable among tech savvies
Fewer classes
Passes all tests
Easily understandable and cheaper to maintain
Technical Debt
It was coined by Ward Cunningham. It is the consequence of negligence in writing code for the sake of short-term benefits. It's like taking a loan for a quick fix, but eventually ending up paying extra money as interest.
Technical Debt vs. Bank Loans: Figurative Comparison
Bank Loan
Purpose: To borrow money from a bank to fund the construction of your house.
Interest: You pay interest on the loan over time.
Repayment: You must repay the loan, plus interest, within the predetermined time constraint.
Technical Debt
Purpose: In software development, "technical debt" is like taking a shortcut or using a temporary solution to speed up development.
Interest: The "interest" in technical debt is the additional effort required to fix or refactor the code later, as the project grows or changes.
Repayment: "Repaying" technical debt involves going back and improving the code's quality, which can be time-consuming and costly.
Essence
Both bank loans and technical debt involve trade-off. With a bank loan, you trade immediate funds for future repayment. With technical debt, you trade faster development for potential future costs.
Causes of Technical Debt
Business Pressure
Lack of understanding of the negative consequences of technical debt
Lack of tests
Lack of communication between team members
Procrastination in refactoring
Incompetence
When to refactor
Rule of Three
First time job, just get it done.
Same logic, cringe at having to repeat but do it again anyway.
Same login again, start refactoring
Addition of new feature
Dirty codes are difficult to grasp. So, whenever you see the dirty code, try to refactor it first.
Refactored code is easier to use while adding new features. Clean code are easier to maintain and update.
Bug fixing
- Bug are hidden into the darkness of dirty codes. Write a clean code and bug will themselves get visible.
How to Refactor
Dos and Don'ts
Think before refactoring. Refactored code in wrong way is only waste of your valuable time
Never add new functionality while refactoring
Always separate refactoring and adding new features. Never mix them up
Follow the standard for refactoring widely accepted by the tech community.
Conclusion
It may seem extra unnecessary code, and extra duration to get the job done which could have finished earlier without having refactoring in the mind while writing the code. But, eventually refactored code will take less time in the long run as it is easily maintainable and is less prone to bugs. Ultimately, it will enhance your experience in software development and can be collaborated with number of team members to create large and complex software. To learn more, you may go to refactoring-guru and learn in depth about refactoring and design patterns.
Subscribe to my newsletter
Read articles from Dhiraj Khapangi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Dhiraj Khapangi
Dhiraj Khapangi
Software engineer.