Angular 6 Style guide quick reference - Rugved/angular6-StyleGuide-referencelist GitHub Wiki
Please refer to official guide for full explanation https://angular.io/guide/styleguide#style-guide
General
- All files service/components should have single responsibility principle (SRP)
- Limit 400 lines code per file
- Define small function limit lines to 75
Naming (Files & Symbol)
- Name files based on “description”.”type”.”extension” i.e. sample.type.ts
- Use conventional types when creating files i.e. service.ts, component.ts
- Upper camel case for class name i.e. AppModule
Export class AppModule { … } App.component.ts
- Put bootstrapping and platform logic in main.ts
- Put “–“ for component selector i.e. selector: ‘main-button’
- Use lower case and prefix component selector i.e. for button inside sample component selector:sample-button
- Prefix and have camel case for directive name i.e. selector:[sampleValues]
- Provide lowercase names i.e. ‘sorting’
- Name the files with component.spec.ts i.e. sample.component.spec.ts
- Provide feature name.module.ts naming convention i.e. sample.module.ts
- Class name should be in upper camel case i.e. SampleModule {}
- Provide routing module with –routing name. i.e. sample-routing.module.ts
Coding Convention
- Use Upper camel case for class name i.e. SampleModule
- Use Lower camel case for const i.e. export const appUrl = “”
- Use Upper camel case for interface i.e. ISample
- Use Lower camel case for properties and method names i.e. message:string, btnclick()
- Leave one-line space between thirdparty and app imports
App structure
- Organize code in LIFT style = Locate, Identify, flat, tdry (try to be dry (don’t repeat yourself)
- Locating code should be simple & fast
- Identifying folder, files etc should be easy
- Keep folder structure as flat as possible. Consider sub older after 7 files (psychologist believe human mind start to struggle once number of adjacent interesting things exceed nine)
- T-dry is try to don’t repeat yourself. Avoid duplicate code
- Create folder as per feature area, this will cover LIFT methodology
- Create ngModule for each feature for lazy loading
- Keep app module as root app module
- Shared Module
- Create sharedmodule in shared folder
- Keep pipe, declarative in shared module to reference in the app
- Consider not keeping services in shared module as they are singleton and should be in core
- Lazy loaded shared module will have its own copy of component, keeping service inside there will have undesired result
- Core Module
- Keep single use classes and services inside core module
- Import coremodule only inside app module as application wide module.
- Put guard so that other modules can’t import core module
- Never import lazy loaded module directly in another module
Component
- Use @input @output as property decorates instead of input and output
- Give component selector name not attribute
- Move style and html into their own file when code exceed more than 3 lines
- Keep properties on top of the methods
- Keep complicated logic inside services not inside component
- Keep logic of presentation inside the component, not inside html i.e. avoid this a * b
- Use directive to enhance element logic
Services
- Use services as singleton within same injector.
- Use them to share data and functionality
- Provide service with @Injectable attribute in app root.
- Always interact with server through service
- Use life cycle hooks and interface to tap into import events provided by angular
Coding helper
- Use codelyzer for guideline and quick code generation as per this style
- Use angular 6 code snippet in vscode