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.

Devashree

Full-stack Developer

Devashree is a Full-stack developer with around 2 years of experience. Her expertise is in the angular framework and has worked on angular 4+ versions. She has also worked on nodeJS and Ionic framework version 3.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks