Things that helps to improve your Angular Code

In this blog, we will see how to reduce the code, follow the conventions, and stick to typescript code.

Let’s get started 🙂 

Follow the Angular Conventions

Working on any programming language, we first study and use syntax, but we should also follow the conventions that angular uses despite syntax.

For example:

File Name Conventions 

Structure of angular file must be

[fileName].[structureName].[extension]

If we create a component then : user.component.ts

If we create a service then: user.service.ts
If its a module then: user.module.ts

Modularization

The best way to simplify your code is to divide the code that is modularized.

Modules are nothing but the features that we want to build in our application.

All these modules are combined in the app module. So in technical terms, it is also called Lazy Loading. In Lazy loading, the module is loaded whenever its route is called.

Another advantage of modularization is we can reuse the module in different applications if we keep it completely separate.

A shared module is different from feature implementation, but functionality commonly used in all modules will fit in the shared module.

The shared module is called in the modules file where we want to use it. Pipes is a good example of a shared module.

Services

Services are something where you can add your code which will be shared throughout the project. 

Most of the time, Business logic, Common functions like HTTP API calls are written in services.

Most services are created according to modules, so each service has its own set of functions according to features.

Avoid Promises and Use Observables

I really like the await operator for promises, but now we need to stick with the framework. HTTP module uses observables to handle AJAX requests and responses. The Router and Forms modules use observables to listen for and respond to user-input events. This will also make our calls asynchronous and we will be able to avoid the promises.

Lets see an example:

Users : USER = [];
ngOnInit() {
this.userService.getList().subscribe(user => {
this.users.push(user);
});
}
Most of us forget to unsubscribe from the observable, this can lead to an unexpected changes in the application.
You can unsubscribe from observables in ngOnDestroy() function.

Users : USER = [];
ngOnInit() {
    this.userSubscription = this.userService.getList().subscribe(user => {
       this.users.push(user);
    });
}
ngOnDestroy(){

   if(this.userSubscription){

      this.userSubscription.unsubscribe();

   }

}

We can avoid such mistakes easily by using async pipes. This pipe automatically unsubscribes from observables whenever the component gets destroyed.

user$ : User = [];
ngOnInit() {
    this.user$ = this.http.get('');
}

Use the variable for template binding using the async pipe 

<p>{{user$ | async}}</p>

Now, the code looks simpler and cleaner.

StyleSheet Management

One of the big tasks is to manage CSS. We always try to reduce the CSS but somehow, it gets repeated. For such reasons, we can use the advantages provided by the SCSS. It gives the ability to declare the style variables to manage this by using common.SCSS and variable.SCSS structures.

This is one of the best practices to keep our CSS compact and simple.

Create one file called a variable.SCSS, which includes all common colors, sizes and fonts you will use throughout the project.

Let see an example:

$error: #FF0000;
$button_color : #6deace;
$text_color :  #96d7e4;

$text-font-size : 14px;

We just import this file in a component CSS file and then we can use variables.

@import 'styles/variables.scss';

user.component.scss

.error-class{
     color: $error;
}

.submit-btn{
 background-color : $button_color;
}

This is an example for colors, but you can create variables for any of the styles.

Mostly variable.scss is used for changing the theme of the application.

Lets move to common.scss.This file includes the common scss classes which can be used through the project.

For example
.user-list-view{
color: $text_color;
font-size :$text-font-size;
padding: 10px;
font-family : sans-serif;
}
<div *ngFor=”let user of users”>
<p class=”user-list-view”>{{user}}</p>
</div>
These are some of the recommendations to keep the code simple and clean. I hope you liked it.

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?