Skip to content
Clean CodeChapter 5 / 57Beginner6 min read

Naming Things

Naming conventions clean code mein kaise kaam karti hain. Functions, booleans, hooks, files aur exports ke naam jo bina comment ke apna kaam batayein.

On this page

1. Yeh practice kya hai?

Jab code mein comments nahi hote, to saara samjhane ka bojh naamon par hota hai. Achha naam aadhi documentation hai.

CheezConventionMisaal
FunctionEk chhota verb, module context deta haiinvoices.create, dispatch, record
Booleanis, has, can, should prefixisExpired, hasQuotaLeft
Production checkensure ya require, assert sirf tests meinensureOwner(caller, doc)
Custom hookNamed state object, tuple ya JSX nahi{ leads, isLoading, refresh }
Component filePascalCaseLeadTable.tsx
Hook ya module filekebab-caseuse-lead-table.ts
Env variableSCREAMING_SNAKEDATABASE_URL
ExportNamed export, default sirf jahan framework maangeexport function price()

Underscore se private dikhana (_cache) nahi. Visibility language se aati hai: module se export na karo, ya class mein private / #field.

2. Kyun zaroori hai?

Bura naam har parhne wale se thora waqt churata hai. 100 files mein yeh ghanton ban jata hai, aur galat naam seedha bug paida karta hai.

Bure naamAche naam
data, temp, obj, x har jagahinvoice, overdueInvoices, customer
flag ka matlab andaaza lagana parta haiisArchived khud sawal ka jawab hai
Hook ka tuple, order yaad rakhna parta haiNamed fields, order ka koi masla nahi
Default export, har file mein alag naam se importNamed export, har jagah ek hi naam, search aasaan
Ek folder mein PascalCase aur kebab-case mixImport ka path andaaze ki bajaye pakka

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectBohat zarooriNaming sab se sasti aur sab se zyada faida dene wali aadat hai
Production codeRepo ki convention poori follow karoEk repo mein do styles search aur imports ko mushkil banate hain
Senior levelConventions likh kar lint se enforce karoJo rule likha na ho woh review mein behas banta hai

4. Bura vs acha code

tsx
export default function useData(id: string) {
  const [d, setD] = useState<any>(null);
  const [l, setL] = useState(true);
  const _load = () => fetchLeadsForCampaignById(id).then(setD).finally(() => setL(false));

  useEffect(_load, [id]);

  return [d, l, _load] as const;
}

const [leads, loading] = useData(campaignId);
tsx
export function useLeads(campaignId: string) {
  const [leads, setLeads] = useState<Lead[]>([]);
  const [isLoading, setIsLoading] = useState(true);

  const refresh = useCallback(async () => {
    setLeads(await campaigns.leads(campaignId));
    setIsLoading(false);
  }, [campaignId]);

  useEffect(() => {
    refresh();
  }, [refresh]);

  return { leads, isLoading, refresh };
}

const { leads, isLoading } = useLeads(campaignId);
Farq kya hai?BuraAcha
Hook naamuseData, kaunsa data?useLeads saaf
State naamd, lleads, isLoading
Function naamfetchLeadsForCampaignById, poora jumlacampaigns.leads, module context deta hai
Private_loadBahar export hi nahi, underscore ki zaroorat nahi
ReturnTuple, order galat to bugObject, naam se lo
ExportDefaultNamed

Ek lafz wala verb

Strict house style kehta hai function ka naam ek chhota verb ho: create, cancel, record. Context module ya class ke naam se aata hai, invoices.cancel(id) parhne mein jumla ban jata hai.

ts
export const invoices = {
  create,
  cancel,
  send,
};

5. Kaise apply karein?

Naam rakhne se pehle
Yeh cheez domain mein kya kehlati hai?
Function hai to kya karta ya kya deta hai?
Boolean hai to is, has, can ya should lagao
Kya module ka naam context de raha hai? To naam chhota rakho
Kya repo mein is qisam ka pattern pehle se hai? Wahi follow karo

6. Exceptions aur common mistakes

Loop counter i, j aur coordinates x, y theek hain, sab jaante hain. Domain ke mashhoor abbreviations (id, url, api, html) bhi theek hain.

React ka apna useState tuple deta hai, woh framework ka faisla hai. Named object wala rule aapke custom hooks ke liye hai.

Honest baat: "ek lafz ka verb" ek sakht opinion hai. Bohat si teams fetchUserProfile ya calculateInvoiceTotal jaise lambe naam pasand karti hain, khaaskar jab functions alag alag files se import hote hain aur module ka context nazar nahi aata. Trade-off: chhota naam tab achha hai jab call site par invoices.create jaisa context ho. Bina context ke create akela mubham hai. Jo bhi chunein, poore repo mein ek hi rakhein.

Galati: Generic naam jaise data, info, handle, manager

Why? Yeh lafz har cheez par fit hote hain is liye kuch nahi batate.

Behtar: Domain ka asli lafz use karo, jaise invoice, overdueInvoices, ya sendReminder.

Galati: Boolean ka naam jo sawal na lage

Why? status ya flag dekh kar pata nahi chalta ke true ka matlab kya hai.

Behtar: isActive, hasPaid, canEdit jaisa naam jo haan ya na ka sawal ho.

Galati: Production code mein assertX naam

Why? assert test ka lafz hai, parhne wala samajhta hai yeh sirf tests mein chalta hai.

Behtar: Production checks ka naam ensureX ya requireX rakho.

Galati: Hook se JSX return karna

Why? Hook ka kaam state aur logic dena hai, JSX dene se UI aur logic ek jagah phans jate hain.

Behtar: Hook named state de, component us se JSX banaye.

7. Pehle aur baad mein kya seekhein?

Final quick revision

CheckSawal
DomainKya naam business ka asli lafz hai?
FunctionKya naam batata hai yeh kya karta ya deta hai?
BooleanKya is, has, can ya should se shuru hai?
HookKya named object deta hai, tuple ya JSX nahi?
PrivateKya underscore ki bajaye export na kar ke chhupaya?
FilesKya folder mein ek hi naming style hai?
ExportKya named export hai?