- Use the Angular CLI for file generation:
This creates the component files in a folder with the component-name as <component-name>.ts|html|css|spec.ts, making the file structure easy to read. - Maintain a flat folder structure without excessive nesting of subfolders.
- Maintain small function definitions (Angular suggests <75 lines) whenever possible for ease of testing, reuse, and maintainability.
- Consistent naming conventions:
a) Upper camel case for class names and feature.type.ts (type: component, service, module, etc.)
b) Folder naming as <feature-name> - Keep third party modules in a separate module file: this helps avoid clutter in app.module.ts and encourages maintainability (e g angular-material module for all Angular Material UI imports)
- Single responsibility per component/service: Simplifying the folder structure and enabling developers to easily read, maintain and work collaboratively.
- Use service names that make sense of service provided e.g.: ItemsListService to get the list of all items.
- Each component should hold code only for the view. Delegate complex logic to a service which can be reused. (e.g. ConfigService to read information from a config JSON file which can then be used via dependency injection in other components/services)
- Talk to the server through a service (for data operations) e.g. DataService, which can be extended by other services, and in turn, can be used by multiple components/services.
- Use lifecycle hooks for tapping into events. e.g. ngOnInit, a life cycle hook called by Angular to indicate that Angular is done creating the component.
- Use constructors for dependency injection.
Instead of using using @Inject parameter decorator with each dependency, use @Injectable() class decorator for dependencies of a service mentioned in the service class’s constructor making use of Angular’s dependency injection and keeping code concise. - Isolate component-specific styling to the component’s style file, while also following a consistent naming convention. Provide style class names that make sense and are easy to understand. e.g. email-label-text.
- Avoid inline styling. Avoid the usage of !important for style values.
Reference: https://angular.io/guide/styleguide