Skip to content
Clean CodeChapter 14 / 57Intermediate6 min read

Refactoring Safely

Safe refactoring small steps mein kaise karein. Behaviour badle bina code behtar karna, tests ka safety net, chhote commits aur scope mein rehna.

On this page

1. Yeh practice kya hai?

Refactoring aur naya feature do alag kaam hain. Refactor mein app bahar se bilkul waisi hi rehti hai: same input, same output. Sirf andar ki tarteeb badalti hai.

HissaMatlab
Chhote qadamEk rename, ek extract, ek move, phir check
Hamesha chalta huaHar qadam ke baad tests green
Behaviour alag commitRefactor ka commit aur feature ka commit alag
Scope mein rahoSirf woh saaf karo jo aapke kaam se juda hai, baaqi note karo
Callers checkPurana code delete karne se pehle dekho kaun use karta hai

2. Kyun zaroori hai?

Bara "sab kuch theek kar deta hoon" refactor sab se khatarnak kaam hai. 40 files badli, kuch toota, aur ab pata nahi kaunsi badli ne toda.

Big bang refactorChhote safe qadam
3 din ka kaam, beech mein kuch nahi chaltaHar ghante code chalta hua
Toota to 40 files mein dhoondoToota to sirf aakhri qadam check karo
Refactor aur nayi feature ek PR mein, review namumkinRefactor PR mein sirf shakal badli, review aasaan
Main branch aage nikal gayi, bara merge conflictChhote PR jaldi merge
Revert karo to sab kuch jata haiSirf ek chhota commit revert

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectGit commit har chhote qadam parToot jaye to wapas jana aasaan, yeh aadat jaldi daalo
Production codeTests aur chhote PR lazmiUsers par asar wala bug refactor se nahi aana chahiye
Senior levelBare refactors ka plan aur tukreSenior bari tabdeeli ko safe chhote hisson mein baanta hai

4. Bura vs acha code

Kaam: discount function saaf karna hai.

ts
export function discount(c: any, t: number) {
  if (c.vip) { if (t > 1000) return t * 0.8; else return t * 0.9; }
  if (c.orders > 10) return t * 0.95;
  return t;
}

Bura tareeqa: ek hi baar mein naam, signature, logic aur types sab badal diye, aur saath mein "chhoti" behaviour change bhi daal di.

ts
export function price(customer: Customer, total: number) {
  if (customer.tier === "vip") {
    return total * VIP_RATE;
  }

  return total;
}

Loyal customers (10 se zyada orders) ka 5 percent discount chupke se gayab ho gaya. Koi test nahi tha, koi nahi jaanta.

Acha tareeqa: pehle safety net, phir chhote qadam.

Qadam 1: maujooda behaviour ko test mein pakar lo.

ts
test.each([
  [{ vip: true, orders: 0 }, 2000, 1600],
  [{ vip: true, orders: 0 }, 500, 450],
  [{ vip: false, orders: 11 }, 100, 95],
  [{ vip: false, orders: 2 }, 100, 100],
])("discount(%o, %i) is %i", (customer, total, expected) => {
  expect(discount(customer, total)).toBe(expected);
});

Qadam 2, 3, 4: ek ek kar ke badlo, har dafa tests chalao.

ts
const VIP_BIG_ORDER_THRESHOLD = 1000;
const VIP_BIG_ORDER_RATE = 0.8;
const VIP_RATE = 0.9;
const LOYAL_MIN_ORDERS = 10;
const LOYAL_RATE = 0.95;

export function discount(customer: Customer, total: number) {
  if (customer.vip) {
    return total * vipRate(total);
  }

  if (customer.orders > LOYAL_MIN_ORDERS) {
    return total * LOYAL_RATE;
  }

  return total;
}

function vipRate(total: number) {
  if (total > VIP_BIG_ORDER_THRESHOLD) {
    return VIP_BIG_ORDER_RATE;
  }

  return VIP_RATE;
}
Farq kya hai?BuraAcha
Safety netKoi test nahiPehle maujooda behaviour ka test
QadamSab ek saathTypes, phir constants, phir braces, phir extract
BehaviourLoyal discount gayabBilkul same, tests saboot hain
Naam ka badlavFunction ka naam bhi badla, saare callers tootePublic naam wahi, alag PR mein agar zaroori

5. Kaise apply karein?

Safe refactor ka loop
Tests hain? Nahi to pehle maujooda behaviour ka test likho
Ek chhota badlav: rename, extract ya move
Tests aur typecheck chalao
Green? Commit karo
Red? Sirf aakhri qadam wapas lo
Agla chhota badlav

Refactor kab karein? Jab code mein yeh nishaan hon: duplication, lamba function jo kai kaam kare, uljhi hui conditions, ya naam jo kuch nahi batate. Sab se achha waqt: jab aap usi code mein feature add karne wale hon. Pehle refactor karo taake feature aasaan ho, phir feature alag commit mein.

Editor ke built-in refactor tools (Rename Symbol, Extract Function, Move to File) haath se karne se zyada safe hain, kyunki woh har reference update karte hain.

6. Exceptions aur common mistakes

Kabhi poora rewrite waqai sasta hota hai, jaise chhoti file jis ka koi caller nahi ya prototype code. Lekin production mein "rewrite from scratch" aksar purane chhupe hue rules kho deta hai jo koi document nahi kar paya. Pehle tests se un rules ko pakro.

Public API ya doosri team ke istemal wale function ka signature tabhi badlo jab us ki migration aapke kaam ka hissa ho.

Galati: Refactor aur behaviour change ek hi commit mein

Why? Bug aaye to pata nahi chalta shakal badalne se aaya ya naye logic se, aur reviewer dono ek saath nahi jaanch sakta.

Behtar: Pehle refactor commit jis mein behaviour same, phir alag commit mein nayi feature.

Galati: Bina tests ke bara refactor

Why? Koi saboot nahi ke behaviour same raha, chhupe hue edge cases chupke se toot jate hain.

Behtar: Pehle maujooda behaviour ke tests likho, chahe woh behaviour ajeeb lage.

Galati: Kaam ke beech poori codebase saaf karne lag jana

Why? PR bohat bara ho jata hai, scope bhatak jata hai aur asli kaam der se pohanchta hai.

Behtar: Jo aapke kaam se juda hai woh saaf karo, baaqi ek alag task ke taur par note karo.

Galati: Purana function bina callers dekhe delete

Why? Kisi aur module ya script mein use ho raha tha, build ya production toot gaya.

Behtar: Pehle saare references search karo, phir delete karo.

7. Pehle aur baad mein kya seekhein?

Final quick revision

CheckSawal
Safety netKya badalne se pehle tests hain?
QadamKya har badlav itna chhota hai ke toote to foran pata chale?
GreenKya har qadam ke baad tests aur typecheck chale?
Alag commitKya refactor aur feature alag commits mein hain?
ScopeKya sirf kaam se juda code chheda?
CallersKya delete se pehle references check kiye?