Skip to content
Clean CodeChapter 12 / 57Beginner5 min read

Breathing Room & Transient Locals

Code readability formatting ke do rules. Logic blocks ke beech blank line, return se pehle khaali line, aur ek dafa use hone wale variables ko inline karna.

On this page

1. Yeh practice kya hai?

Yeh do chhote rules hain jo saath kaam karte hain:

RuleMatlab
Breathing roomFunction chhote alag steps ki tarah parhe, ek ghana paragraph nahi
Return se pehle blank lineFunction ka nateeja hamesha alag nazar aaye
No transient localsEk dafa use hone wali value ka naam nahi, seedha istemal
Naam kab doJo value ek se zyada dafa use ho, accumulator ho, ya aisi condition jise samajhne mein waqt lage

2. Kyun zaroori hai?

Blank lines code ke paragraphs hain. Bina paragraphs ki kitaab parhna thaka deta hai. Aur bekaar variables parhne wale ko har naam yaad rakhne par majboor karte hain, chahe woh dobara kabhi na aaye.

Ghana code aur extra variablesSaans leta code
20 lines ek block, steps ki hadd nazar nahi aatiHar step alag, aankh khud tukre pehchanti hai
return beech mein chhup jata haireturn hamesha alag, nateeja foran dikhe
5 naam jo sirf ek dafa aayeSirf woh naam jo zaroori hain
Parhne wala har variable track karta haiKam naam, kam yaaddasht ka bojh

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectBlank lines ki aadat abhi daaloSab se aasaan readability jeet hai
Production codeLint aur formatter se enforceInsaan ko formatting yaad rakhne ki zaroorat na ho
Senior levelTransient locals par judgementKab naam madad karta hai aur kab shor hai, yeh tajurba se aata hai

4. Bura vs acha code

ts
async function publish(postId: string, editor: User) {
  const post = await posts.require(postId);
  const isOwner = post.authorId === editor.id;
  const role = editor.role;
  const isEditor = role === "editor";
  if (!isOwner && !isEditor) {
    throw new ForbiddenError("Cannot publish");
  }
  const now = new Date();
  const data = { status: "published", publishedAt: now };
  const updated = await db.post.update({ where: { id: postId }, data });
  const url = `/blog/${updated.slug}`;
  await cache.invalidate(url);
  return updated;
}
ts
async function publish(postId: string, editor: User) {
  const post = await posts.require(postId);
  const canPublish = post.authorId === editor.id || editor.role === "editor";

  if (!canPublish) {
    throw new ForbiddenError(`User ${editor.id} cannot publish post ${postId}`);
  }

  const updated = await db.post.update({
    where: { id: postId },
    data: { status: "published", publishedAt: new Date() },
  });

  await cache.invalidate(`/blog/${updated.slug}`);

  return updated;
}
Farq kya hai?BuraAcha
Blank linesKoi nahi, ek ghana blockLoad, check, update, invalidate, return, sab alag
role, now, data, urlEk dafa use, phir bhi naamInline, parhne wale ko kam naam yaad rakhne
isOwner, isEditorDo aadhe predicatesEk poora canPublish, condition ka matlab naam mein
post, updatedNaam theekNaam theek, kyunki ek se zyada dafa use

canPublish ek predicate hai: is ke bina parhne wale ko || wali condition decode karni parti. Is liye yeh naam ke laayak hai. now ka naam kuch nahi batata jo new Date() nahi batata.

5. Kaise apply karein?

Function ko saans dena
Function ko steps mein socho: load, check, kaam, side effect, return
Har step ke baad blank line
return se pehle hamesha blank line
Har variable dekho: kitni dafa use hua?
Ek dafa aur naam kuch naya nahi batata? Inline karo
Mushkil condition? Predicate ka naam rakho

Lint config se yeh khud ho jata hai:

json
{
  "rules": {
    "padding-line-between-statements": [
      "error",
      { "blankLine": "always", "prev": "*", "next": "return" }
    ]
  }
}

Baaqi formatting (indentation, quotes, semicolons) Prettier jaisa formatter sambhale. Insaan sirf meaning wale faisle kare: kahan step khatam hua.

6. Exceptions aur common mistakes

Ek line ka function jis mein sirf return ho, us ke upar blank line ki zaroorat nahi.

Honest baat: "no transient locals" ek strict opinion hai. Bohat si teams aur mashhoor refactoring kitaabein "explaining variable" ko achha maanti hain: ek lamba expression ek naam mein tod do taake parhna aasaan ho. Yeh rule usi ko allow karta hai jab naam waqai kuch samjhata ho (predicate). Jo nahi karta, woh hai const data = ...; return data; jaisa naam jo kuch nahi batata. Ek aur trade-off: debugger mein local variable ki value dekhna aasaan hota hai. Jab inline expression bohat lamba ho jaye, to naam rakhna behtar hai.

Galati: Har cheez ko pehle variable mein daalna

Why? const result = compute(x); return result; mein result kuch naya nahi batata, bas ek line aur ek naam barha.

Behtar: Seedha return compute(x) likho.

Galati: Mushkil condition ko inline chhor dena

Why? Lamba && aur || wala if parhne wale ko decode karna parta hai.

Behtar: Us condition ko isX ya canX naam do, yeh predicate naam ke laayak hai.

Galati: Har line ke baad blank line

Why? Har jagah khaali line ho to koi grouping nazar nahi aati, ghane code jitna hi bura.

Behtar: Blank line sirf ek logical step khatam hone par.

7. Pehle aur baad mein kya seekhein?

Final quick revision

CheckSawal
StepsKya har logic step ke baad blank line hai?
ReturnKya return se pehle blank line hai?
TransientKya koi variable ek dafa use hua aur naam kuch nahi batata?
PredicateKya mushkil condition ka naam hai?
ToolingKya formatter aur padding lint rule on hain?