← Back to TypeScript Course | Chapter 23: Performance and Best Practices | Lesson 6 of 7

Naming Conventions की जानकारी

Consistent names TypeScript code को पढ़ना और maintain करना आसान बनाते हैं। Good naming usually extra comments की ज़रूरत के बिना variables, types, functions, classes, और generic parameters का purpose communicate करती है।

Variables और Functions

Variables और functions को camelCase में respectively nouns और verbs की तरह पढ़ना चाहिए, इसलिए userCount और calculateTotal बिना एक comment की ज़रूरत के अपना role communicate करते हैं।

उदाहरण: Variables and Functions

typescript
const userCount: number = 5;
function calculateTotal(price: number, qty: number): number {
  return price * qty;
}
console.log(userCount, calculateTotal(10, 2));

Types और Interfaces

Types और interfaces conventionally PascalCase में होते हैं, जो code में exact उसी spot पर एक type reference जैसे User को एक value reference जैसे user से visually अलग करता है।

उदाहरण: Types and Interfaces

typescript
interface User {
  name: string;
}
const user: User = { name: "Ravi" };
console.log(user);

Generic Type Parameters

Generic type parameters traditionally T से शुरू होते हैं (और additional ones के लिए U, K, V), लेकिन एक parameter को TItem या TResponse नाम देना जब इसका role obvious न हो readers को usage trace करने से बचाता है यह figure out करने के लिए कि यह represent क्या करता है।

उदाहरण: Generic Type Parameters

typescript
function wrapResponse<TResponse>(data: TResponse): { data: TResponse } {
  return { data };
}
console.log(wrapResponse({ id: 1 }));

Boolean Names

Boolean variables और functions is, has, या can prefix के साथ (जैसे isLoading या hasPermission) सबसे अच्छा पढ़ते हैं, क्योंकि यह phrasing call site पर true/false meaning को obvious बनाती है।

उदाहरण: Boolean Names

typescript
const isLoading: boolean = false;
const hasPermission: boolean = true;
function canEdit(user: { role: string }): boolean {
  return user.role === "admin";
}
console.log(isLoading, hasPermission, canEdit({ role: "admin" }));

Names जो Intent Explain करते हैं

एक name को intent describe करना चाहिए, implementation नहीं — activeUsers एक reader को बताता है कि value क्या represent करती है, जबकि arr2 जैसा एक name उन्हें यह पता लगाने के लिए surrounding code पढ़ने पर मजबूर करता है।

उदाहरण: Names That Explain Intent

typescript
const activeUsers: string[] = ["Ravi", "Priya"];
// Avoid vague names like arr2 or data1
console.log(activeUsers);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Types या interfaces को lowercase से शुरू करना, जैसे user, इसलिए वे variables जैसे दिखते हैं।
  2. एक project में interfaces को I prefix देना (जैसे IUser) जब वह convention follow न करता हो, names को inconsistent बनाते हुए।
  3. Booleans को vague names देना जैसे loading या flag बजाय isLoading के।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.