Skip to content
Clean CodeChapter 9 / 57Intermediate5 min read

File Size & Organization

File organization large components ke saath kaise karein. 400 line ki component limit, ek file ek zimmedari, aur bari React file ko child components mein todna.

On this page

1. Yeh practice kya hai?

Function ka size Small Functions mein dekha. File ek level upar hai: kai functions aur components ka ghar. Yahan sawal hai "kya yeh sab ek hi ghar mein rehna chahiye?"

HissaMatlab
400 line limit.tsx component file ki upar ki hadd
Ek zimmedariFile ka naam batata hai andar kya hai, aur sirf wahi hai
Ek unit, ek foldercomponents/LeadTable/LeadTable.tsx aur us ke apne hisse saath
Ek naming styleEk folder mein PascalCase ya kebab-case, mix nahi

2. Kyun zaroori hai?

1200 line ki file mein koi bhi badlav dar ke saath hota hai. Scroll karte karte context bhool jata hai, aur do developers ek hi file chhed kar merge conflict mein phans jate hain.

Ek bari fileChhoti focused files
Har change par merge conflictAlag log alag files, conflict kam
Ek state badli to poori file re-renderChild components apni state sambhalte hain
Kuch dhoondna ho to Ctrl F aur scrollFile ka naam hi pata batata hai
Review mein 600 line ka diffChhota diff, focused review
Kuch reuse karna ho to poori file importSirf zaroori component import

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectJab file scroll karna takleef de, tab todoComponents todna React ki bunyadi mahaarat hai
Production code400 line limit review meinBari files team ki speed ka sab se bara dushman hain
Senior levelFolder structure aur boundaries decide karoKahan todna hai yeh design faisla hai

4. Bura vs acha code

Bura: ek Dashboard.tsx jo 900 lines ka hai.

tsx
export function Dashboard() {
  const [filters, setFilters] = useState<Filters>(DEFAULT_FILTERS);
  const [selected, setSelected] = useState<string[]>([]);
  const { leads, isLoading } = useLeads(filters);

  return (
    <main>
      <header>
        <h1>Leads</h1>
        <input value={filters.query} onChange={(e) => setFilters({ ...filters, query: e.target.value })} />
        <select value={filters.status} onChange={(e) => setFilters({ ...filters, status: e.target.value as LeadStatus })}>
          <option value="all">All</option>
        </select>
      </header>
      <table>
        <tbody>
          {leads.map((lead) => (
            <tr key={lead.id}>
              <td>{lead.name}</td>
            </tr>
          ))}
        </tbody>
      </table>
      <footer>{selected.length} selected</footer>
    </main>
  );
}

Yahan sirf dhaancha dikhaya hai. Asli file mein har hissa 200 lines ka hai: filters ka logic, table ki sorting, bulk actions ka modal.

Acha: har hissa apni file mein.

bash
components/
  Dashboard/
    Dashboard.tsx
    DashboardFilters/
      DashboardFilters.tsx
    LeadTable/
      LeadTable.tsx
      LeadRow.tsx
    BulkActions/
      BulkActions.tsx
hooks/
  use-leads/
    use-leads.ts
tsx
export function Dashboard() {
  const [filters, setFilters] = useState<Filters>(DEFAULT_FILTERS);
  const [selected, setSelected] = useState<string[]>([]);
  const { leads, isLoading } = useLeads(filters);

  return (
    <main>
      <DashboardFilters filters={filters} onChange={setFilters} />
      <LeadTable leads={leads} isLoading={isLoading} onSelect={setSelected} />
      <BulkActions selected={selected} />
    </main>
  );
}
Farq kya hai?BuraAcha
Size900 linesHar file 50 se 150 lines
ZimmedariFilters, table, actions sabDashboard sirf jor kar rakhta hai
DhoondnaScrollFolder ka naam
TestingPoora dashboard renderLeadTable akela test
TeamworkEk file par sabHar koi apni file

5. Kaise apply karein?

Bari file todne ke steps
File ko scroll kar ke uske visual ya logical hisse pehchano
Har hissa jo apni state ya apna data rakhta hai, ek candidate hai
Hisse ko apni file aur folder mein nikalo
Props sirf woh do jo us hisse ko chahiye
Logic ko hook ya pure function mein nikalo
Parent sirf children ko jore

Todne ke ache nishaan: alag state, alag data source, alag wajah se badalna, ya .map ke andar bara JSX (woh Row component hai).

6. Exceptions aur common mistakes

400 line ek upar ki hadd hai, target nahi. Aur kabhi file todna nuqsaan deta hai: agar do hisse hamesha saath badalte hain aur ek doosre ki andar ki details par chalte hain, to alag files mein bas navigation barhti hai. Ek saath badalne wali cheezein saath rakho, alag badalne wali alag.

Config files, generated types ya lambi constant lists 400 se bari ho sakti hain, kyunki un mein logic nahi hota.

Galati: Har cheez ke liye alag file chahe 5 lines ki ho

Why? 20 tiny files jo sirf ek dusre ko import karti hain parhne ko aur mushkil bana deti hain.

Behtar: Tab nikalo jab hissa apni zimmedari rakhta ho ya file waqai bari ho rahi ho.

Galati: Ek folder mein naming style mix

Why? Kahin LeadTable.tsx kahin lead-row.tsx, har import ek andaaza ban jata hai.

Behtar: Har folder ke liye ek convention chuno aur poore repo mein rakho.

Galati: utils.ts mein sab kuch daal dena

Why? Utils file har kisi ka kachra daan ban jati hai aur 1000 lines tak pohanch jati hai.

Behtar: Har helper ko us module ke paas rakho jo use karta hai, naam ke saath.

Galati: Prop drilling se bachne ke liye sab ek file mein rakhna

Why? File chhoti rakhne ka faida khatam, aur problem wahi rehti hai.

Behtar: Composition, children prop ya context use karo, file phir bhi todo.

7. Pehle aur baad mein kya seekhein?

Final quick revision

CheckSawal
SizeKya component file 400 lines se kam hai?
ZimmedariKya file ka naam poora andar ka kaam batata hai?
FolderKya har component apne folder mein hai?
NamingKya folder mein ek hi naming style hai?
CohesionKya saath badalne wali cheezein saath hain?