Refactoring

Dhiraj KhapangiDhiraj Khapangi
3 min read

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

  1. It's widely understandable among tech savvies

  2. Fewer classes

  3. Passes all tests

  4. 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

  1. Business Pressure

  2. Lack of understanding of the negative consequences of technical debt

  3. Lack of tests

  4. Lack of communication between team members

  5. Procrastination in refactoring

  6. Incompetence

When to refactor

Rule of Three

  1. First time job, just get it done.

  2. Same logic, cringe at having to repeat but do it again anyway.

  3. Same login again, start refactoring

Addition of new feature

  1. Dirty codes are difficult to grasp. So, whenever you see the dirty code, try to refactor it first.

  2. Refactored code is easier to use while adding new features. Clean code are easier to maintain and update.

Bug fixing

  1. 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

  1. Think before refactoring. Refactored code in wrong way is only waste of your valuable time

  2. Never add new functionality while refactoring

  3. Always separate refactoring and adding new features. Never mix them up

  4. 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.

0
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.