Coding Styles is very important to every application. It tells how the application is clean and neat.
Every programming language has common coding styles and standards. We must follow the few of the coding styles it will very helpful to application development. Then the code will easier to understand.
In this post, listed few of the angular coding styles and standards with small examples.
1. Per file, the code should limit to 400 lines.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
// Per file, the code should limit to 400 lines.
}
2. Per function, the code should limit to 75 lines.
public displayUserInformationToTable(): void {
// Per function, the code should limit to 75 lines.
}
export function getUserName(): void {
// Per function, the code should limit to 75 lines.
}
3. Properties, Variables and Methods should be in lower camel case.
// Method Name should be lower camel case.
public displayUserInformationToTable(): void { }
const CARS = [
// Properties names should be lower camel case.
{ id: 1, name: 'Ford Mustang (1964-Present)' },
{ id: 2, name: 'Rolls-Royce Silver Cloud (1955-1966)' },
];
// variable names should be lower camel case.
public selectedCar: string;
4. Component file name should be Kebab-Case.
5. Leave one empty line between imports. Such as angular, third party imports and application imports.
// Angular and Third Party imports.
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { NgSelectModule } from '@ng-select/ng-select';
import { BrowserModule } from '@angular/platform-browser';
// Application created imports.import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { CustomNgSelectComponent } from './custom-ng-select/custom-ng-select.component';
6. If the values of the variables are intact, declare it with ‘const’.
public display(): void {
// If the values of the variables are intact, declare it with ‘const’.
const CARS = [
{ id: 1, name: 'Ford Mustang (1964-Present)' },
{ id: 2, name: 'Rolls-Royce Silver Cloud (1955-1966)' },
];
}
7. Avoid ‘var’ and use the ‘let’ .
public display(): number {
// Avoid ‘var’.
var value: number;
if (value === 0) {
value = 25;
}
return value;
}
public display(): number {
// Use ‘let’.
let value: number;
if (value === 0) {
value = 25;
}
return value;
}
8. Do give the filename the conventional suffix (such as .component.ts, .directive.ts, .module.ts, .pipe.ts, or .service.ts) for a file of that type.
9. Method and function should have the return type.
// Method or functions should have the return type.
public thisMethodShouldReturnString(): string {
return 'returnsString';
}
public thisMethodShouldReturnNumber(): number {
return 0;
}
public thisMethodShouldReturnBoolean(): boolean {
return false;
}
10. Directive, Component, Service, Module, Interface and Pipe class names should be in pascal case.
// Class name should be pascal case.
export class AppComponent implements OnInit {}
Comments
Post a Comment