Refactoring is the disciplined practice of changing the internal structure of code, such as renaming, splitting a long function or removing duplication, without altering its externally visible behavior.
Why it matters
Code is read far more often than it is written. Regular refactoring keeps a codebase understandable and pays down technical debt, which makes later features cheaper to add. It is only safe when tests confirm that behavior stayed the same.
Example
Before:
def total(items):
t = 0
for i in items:
t = t + i["price"] * i["qty"]
return t
After:
def line_total(item):
return item["price"] * item["qty"]
def total(items):
return sum(line_total(i) for i in items)
The result is the same, but each piece now has a clear name and can be tested separately.
Common mistakes
- Refactoring without tests, then discovering behavior changed.
- Mixing a refactor with a new feature in one commit, which makes review and rollback hard.
- Rewriting everything from scratch when small, incremental steps would do.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.