Angular Coding Styles and Standards - PART 1

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: 1name: 'Ford Mustang (1964-Present)' },
   { id: 2name: 'Rolls-Royce Silver Cloud (1955-1966)' },
];


// variable names should be lower camel case.
public selectedCarstring;


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: 1name: 'Ford Mustang (1964-Present)' },
    { id: 2name: 'Rolls-Royce Silver Cloud (1955-1966)' },
  ];
}


7. Avoid ‘var’  and use the ‘let’ .


public display(): number {
  // Avoid ‘var’.
  var valuenumber;
  if (value === 0) {
    value = 25;
  }
  return value;
}


public display(): number {
  // Use ‘let’.
  let valuenumber;
  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

Popular posts from this blog

Send API POST Request in MS SQL Server

Create Custom Form Control for ng-select in Angular

Domain Driven Design (DDD) in .NET Core

Tightly Coupled and Loosely Coupled in .NET Core with example

How to Convert JSON Object to XML in C#

Delete Empty Rows or Null Rows in Datatable in C#

Restrict Special Characters in Windows Application using c#