In this blog, we will see how to reduce the code, follow the conventions, and stick to typescript code.
Let’s get started 🙂Â
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:
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
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 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.
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.
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.
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.
Get answers to all the frequently asked questions from founders that have done it in the past.
Download NowMindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and MindBowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project. Â
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Our CISO was extremely impressed by Mindbowser’s work. It is pretty rare to see this kind of clean security report so early in the company’s journey. Huge Thank you for the disciplined approach here.
Founder, TrestleIQ
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
The flexibility and capacity of the Mindbower staff has been impressive.
CEO, ProofPilot
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
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.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.Â
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
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.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
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.
Founder, Courtyardly
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork
We cannot express enough how pleased we are with the entire Mindbowser team. From our first call, they focused on our vision and ensured things were handled as per requirement. Communication was so easy and flexible to our schedule. I’m...
Founder, SafeZone Safety Systems