Angular Best Practices: Top Suggestions How to Write Better Angular Apps

Yana Troianska

May 30, 2018

When building an application within the specific framework, you must know best practices to write clean and maintainable code. We formed Top Angular 5 Best Practices based on our experience.

So, we invite you to get acquainted with the new features and use those practices in your work.

Angular 5 Best Practices: 5 Tips For Cleaner Code

#1. Project structure

In app development, a smooth project structure is a point to pay attention to in the first place. There is no completely defined way of building the perfect structure because everything depends on the work. Since requirements differ from project to project it is hard to find a universal structure for every case. Yet the style guide hints at developing an optimal application structure.
The best move is to start with the Angular CLI.
npm install -g @angular/cli
ng new my-project

The CLI is the easiest and fastest way to create a new project on Angular. It enables project deployment within a few minutes and develops sizeable apps too. But the main benefit of the CLI is the way of automating pipeline in live development with ng serve and in production ng build -prod. Angular CLI makes it much easier to manage a project. But such simplicity can be translated into inflexibility. This works great for simple projects, but sizeable ones require a manual approach.

Style guide suggests starting from the small parts but keeping in mind the final result and adjusting the project structure respectively.  To provide consistent structure, keep it small and easy to maintain. The structure should be suitable to evolve as the app expands.

#2. Writing components

Components are not a new practice, but it is one of Angular’s core features, so we considered mentioning them. When components are organized and reusable, it is already half work done. Try to be DRY, especially when you have repetitive templates in the application. Rather than pollute components with the same code, consider creating one or several base classes.

The angular application architecture is like a tree of сomponents. We suggest classifying and reusing components to build a more maintainable and organized app. This will provide a clear application structure and build unloaded with similar components code.

You can use Container (Smart) or Presentation (Dumb) Components according to your needs. When you have the component passing properties down to other components, make it Smart.

export class ContactPageComponent

/* container component */

user$: Observable<User>;

constructor(route: ActivatedRoute) 

this.user$ =

.map(({ user }) => user);


If there is a component that only performs dispatching actions, better define it as Presentation one.

export class ContactDetailComponent


    /* presentation component */

    @Input() user: User;


#3. Performance

Angular is quite a fast framework. But due to project expansion, its performance may decrease. There are some ways you can help Angular to process faster. To start, you can ChangeDetectionStrategy to onPush. It signals to the Angular that the component only depends on its own Input. Thus, Angular will check it in two cases. When a component or its derivative caused the event or if the Input reference changes.

Data changes in the collection also can affect performance. To deal with this issue, you can use trackBy function. It allows Angular to track added/removed items in the collection by a unique identifier.

We couldn’t help but mention the strongest Angular feature – LazyLoading. It splits the app into modules and loads them on-demand, thus cutting load time.

  path: 'admin',

#4. Use RxJS

RxJS is difficult to understand. Yet it does its job and simplifies the developer’s work. In the fifth Angular edition, RxJS has undergone some minor changes. The code remained the same, but RxJS moved to lettable operators. It allowed cutting problems associated with the old mechanism of data import.

Also, developers managed to fix all splitting and tree-shaking shortcomings. RxJS library is a powerful tool that helps to optimize application logic and keeps code clean.

#5. Testing

Angular 5 has an effective testing tool. Testing enables us to ensure that certain application parts work exactly as you expect them to. This, to some extent, saves the existing code from breakdowns and helps to clarify – how it will work in various cases. In the end, it allows the detection of code weaknesses.

We would like to describe the main aspects of testing in Angular 5. Choose a test type that fits your situation to simplify the testing process. Also, make sure you know all the capabilities of your IDE in terms of assisting in testing. And finally, you can disable checking the definition for directives and child components. To do so, in the TestBed configuration, write the NO_ERRORS_SCHEMA:


declarations: [ AppComponent ],

schemas: [ NO_ERRORS_SCHEMA ]


Also, important to mention TestBed alone. It is the utility for simplifying and facilitating a testing process in Angular. We can check whether the component was created and how it interacts with templates and dependencies.

beforeEach(async(() => {
declarations: [

Angular 5 Best practices are not intended to dictate an exclusive course of procedure to be followed or act like a fixed protocol. It is rather a recommendation to keep the project simple and clean. But remember that development is where creativity pays, not the last role.

`<div *ngFor="var item in items; trackBy=customTrackBy"
class MyCoponent {
(index: number, obj: any): any {
return index;
2.5/5 - (11 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *