Naming¶
Rule¶
Good naming in TypeScript is crucial for several key reasons:
- Code Readability and Self-Documentation - Well-named variables, functions, and types make code immediately understandable. When you see calculateUserTaxRate() instead of calc(), the purpose is instantly clear without needing to read the implementation or comments.
- Type System Clarity - TypeScript's strength lies in its type system, and descriptive names make type definitions much more valuable. Compare User vs. U, or isEmailValid: boolean vs. flag: boolean - the first versions immediately communicate what the type represents.
- Enhanced IntelliSense and Developer Experience - IDEs can provide better autocomplete suggestions and hover information when names are descriptive. This speeds up development and reduces errors when working with complex codebases.
- Reduced Mental Overhead - Good names eliminate the cognitive load of constantly translating cryptic abbreviations or remembering what x, fn, or obj represent in different contexts throughout your application.
- Team Collaboration - Multiple developers can work on the same codebase more effectively when naming conventions are consistent and meaningful. New team members can onboard faster when the code explains itself.
Examples¶
🚨 DON’T¶
function isBetween(a1: number, a2: number, a3: number): boolean {
return a2 <= a1 && a1 <= a3;
}
class Subs {
ccId: number;
billingAddrId: number;
shippingAddrId: number;
}
var FooVar;
function BarFunc() {
}
class Foo {
Bar: number;
Baz() {
}
}
type Car = {
carMake: string;
carModel: string;
carColor: string;
};
✅ DO¶
function isBetween(value: number, left: number, right: number): boolean {
return left <= value && value <= right;
}
class Subscription {
creditCardId: number;
billingAddressId: number;
shippingAddressId: number;
}
var fooVar;
function barFunc() {
}
class Foo {
bar: number;
baz() {
}
}
type Car = {
make: string;
model: string;
color: string;
};