Naming Conventions की जानकारी
In this page:
Variables और Functions
Variables और functions को camelCase में respectively nouns और verbs की तरह पढ़ना चाहिए, इसलिए userCount और calculateTotal बिना एक comment की ज़रूरत के अपना role communicate करते हैं।
उदाहरण: Variables and Functions
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
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
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
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
const activeUsers: string[] = ["Ravi", "Priya"];
// Avoid vague names like arr2 or data1
console.log(activeUsers);
- Types या interfaces को lowercase से शुरू करना, जैसे
user, इसलिए वे variables जैसे दिखते हैं। - एक project में interfaces को
Iprefix देना (जैसेIUser) जब वह convention follow न करता हो, names को inconsistent बनाते हुए। - Booleans को vague names देना जैसे
loadingयाflagबजायisLoadingके।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: