Naming Conventions
Esta página aún no está disponible en tu idioma.
Source: Angular 2025: New Style Guide Standards and How to Apply Them
Overview
Section titled “Overview”Consistent naming conventions are fundamental to code readability and team collaboration. Angular 2025 emphasizes two key principles:
- Avoid Generic Names — Don’t use
helpers.ts,utils.ts,common.ts - Consistent Naming Across Related Files — Component files should share the same base name
Key Update: Avoid Generic Names
Section titled “Key Update: Avoid Generic Names”From Angular 2025 style guide:
“Steer clear of generic file names like
helpers.tsorutils.ts. Instead, use descriptive names that reflect the file’s purpose.”
❌ Generic Names (Don’t Use)
Section titled “❌ Generic Names (Don’t Use)”helpers.ts — What helpers?utils.ts — What utilities?common.ts — Common what?data.ts — What data?misc.ts — Miscellaneous what?✅ Descriptive Names (Use These)
Section titled “✅ Descriptive Names (Use These)”date-formatter.ts — Clear purposestring-validators.ts — Specific functionalityuser.model.ts — Domain-specificapi-endpoints.ts — Clear categoryConsistent Naming Across Related Files
Section titled “Consistent Naming Across Related Files”Angular 2025 emphasizes that related files should share a common base name.
Example
Section titled “Example”✅ Good - All related files share "user-profile" base nameuser-profile.component.tsuser-profile.component.htmluser-profile.component.scss
❌ Bad - Inconsistent naminguser.tsuser-view.htmluser-styles.scssFile Naming Patterns
Section titled “File Naming Patterns”Components: {name}.component.{ts|html|scss}
user-profile.component.tsuser-profile.component.htmluser-profile.component.scssServices: {name}.service.ts
user.service.tsauth.service.tsapi.service.tsDirectives: {name}.directive.ts
highlight.directive.tsauto-focus.directive.tsPipes: {name}.pipe.ts
phone-format.pipe.tstruncate.pipe.tsModules (NgModule): {name}.module.ts
user.module.tsauth.module.tsClass and Interface Naming
Section titled “Class and Interface Naming”Classes
Section titled “Classes”Pattern: PascalCase, descriptive, with optional suffix for type
export class UserService {}export class UserComponent {}export class UserGuard {}export class ApiInterceptor {}export class user {} // lowercaseexport class userServ {} // abbreviatedexport class userSERVICE {} // inconsistent casingexport class Service {} // too genericInterfaces
Section titled “Interfaces”Pattern: PascalCase, optional I prefix (but avoid if possible)
export interface User { id: string; name: string; email: string;}
export interface UserService { getUser(id: string): Observable<User>; updateUser(user: User): Observable<User>;}export interface IUser { id: string; name: string; email: string;}Variable and Function Naming
Section titled “Variable and Function Naming”Variables and Properties
Section titled “Variables and Properties”Pattern: camelCase, descriptive
// ✅ Goodprivate userList: User[] = [];private isLoading = false;private selectedUserId: string | null = null;private maxRetries = 3;
// ❌ Badprivate users: User[] = []; // too shortprivate u: User[] = []; // single letterprivate USER_LIST: User[] = []; // SCREAMING_SNAKE_CASEprivate the_users: User[] = []; // snake_caseprivate x = 0; // unclear meaningFunctions and Methods
Section titled “Functions and Methods”Pattern: camelCase, verb-based for actions, clear purpose
// ✅ Good - Action verbsprivate loadUsers(): void {}private fetchUserData(id: string): Observable<User> {}private validateEmail(email: string): boolean {}private formatPhoneNumber(phone: string): string {}private saveChanges(): void {}
// ✅ Good - Gettersprivate getSelectedUser(): User | null {}private getUserById(id: string): User {}
// ✅ Good - Checkers (is/has prefix)private isUserAuthenticated(): boolean {}private hasPermission(permission: string): boolean {}
// ❌ Badprivate user_data(): void {} // snake_caseprivate UserData(): void {} // PascalCaseprivate processUserDataAndSaveIt(): void {} // too longprivate p(): void {} // too shortprivate data(): void {} // unclear what it doesConstants
Section titled “Constants”Pattern: UPPER_SNAKE_CASE for true constants, camelCase for config objects
// ✅ Good - True constantsexport const MAX_RETRIES = 3;export const DEFAULT_TIMEOUT_MS = 5000;export const API_BASE_URL = 'https://api.example.com';
// ✅ Good - Configuration objects (if immutable)export const defaultConfig = { timeout: 5000, retries: 3, debug: false};
// ❌ Badexport const maxRetries = 3; // should be UPPER_SNAKE_CASEexport const MAX_RETRIES_ALLOWED = 3; // redundant suffixexport const API = 'https://api.example.com'; // too short/genericSignals and Observables
Section titled “Signals and Observables”// ✅ Good - no suffix, called as functionsreadonly count = signal(0);readonly username = signal('');readonly isLoading = signal(false);readonly selectedUser = signal<User | null>(null);// ✅ Good - optional $ suffix for observablesreadonly users$ = this.userService.getUsers();readonly user$ = this.userService.getUserById(id);
// or without $ (also acceptable in modern Angular)readonly users = this.userService.getUsers();readonly user = this.userService.getUserById(id);Selector Names
Section titled “Selector Names”Pattern: kebab-case with prefix
// ✅ Good@Component({ selector: 'app-user-card', // ✅ with app- prefix selector: 'vs-button', // ✅ with lib prefix selector: 'lib-modal' // ✅ with lib prefix})
// ❌ Bad@Component({ selector: 'UserCard', // ❌ PascalCase selector: 'user_card', // ❌ snake_case selector: 'usercard' // ❌ no hyphen (looks like one word) selector: 'user-card-component' // ❌ redundant suffix})Feature Module Naming
Section titled “Feature Module Naming”Pattern: Feature name + .module.ts (NgModules) or feature folder for standalone
src/app/features/├── user/ ✅ Feature folder│ ├── user.component.ts│ ├── user.service.ts│ └── models/├── admin/ ✅ Feature folder│ ├── dashboard/│ ├── settings/│ └── admin.module.ts (if NgModule-based)└── dashboard/ ✅ Feature folder ├── dashboard.component.ts ├── analytics.component.ts └── dashboard.service.tsConsistency Checklist
Section titled “Consistency Checklist”Use this checklist to verify your naming is consistent:
- All component files start with component name (e.g.,
user-profile.component.ts,user-profile.component.html) - Services end with
.service.ts - Directives end with
.directive.ts - Pipes end with
.pipe.ts - No generic names (
helpers.ts,utils.ts,common.ts) - Files use kebab-case
- Classes and interfaces use PascalCase
- Variables, functions, and properties use camelCase
- Constants use UPPER_SNAKE_CASE
- Selectors use kebab-case with a prefix
- Feature folders are named descriptively
Why This Matters
Section titled “Why This Matters”✅ Readability — Developer can quickly understand what each file does ✅ Searchability — Easy to find files with consistent naming ✅ Team Collaboration — Everyone follows the same conventions ✅ Maintainability — Reduces cognitive load when reading code ✅ IDE Support — Better autocomplete and refactoring tools