domains / / microsoft/typescript
Names reveal semantic purpose
Choose names that clearly communicate the semantic purpose and behavior of code elements. Variable and function names should precisely reflect their role, return type, and intended usage.
Choose names that clearly communicate the semantic purpose and behavior of code elements. Variable and function names should precisely reflect their role, return type, and intended usage.
Key guidelines:
- Function names should indicate their behavior and return type
- Use distinct names for related but different concepts
- Choose specific over generic names
- Fix misleading names immediately
Example:
// Poor naming
function isGlobalType(checker: TypeChecker, symbol: Symbol) {
return checker.resolveName(symbol.name, undefined, SymbolFlags.Type, false);
}
// Better naming - reflects actual behavior
function canResolveTypeGlobally(checker: TypeChecker, typeName: string) {
return checker.resolveName(typeName, undefined, SymbolFlags.Type, false);
}
// Poor naming - reusing variable
let initializer = getCandidateVariableDeclarationInitializer(declaration);
if (initializer) { ... }
initializer = getDestructuredInitializer(declaration);
// Better naming - distinct purposes
let directInit = getCandidateVariableDeclarationInitializer(declaration);
if (directInit) { ... }
let destructuredInit = getDestructuredInitializer(declaration);