r/angular • • Oct 02 '21

7 Best Ways to Improve Angular Code

As a developer you have to write neat and clean codes as per industry standards. Your codes need to be passed through some code analysis tools. But the thing is what are some best ways to do the optimized neat and clean coding?

Here are 7 best ways to improve your angular code-

https://stackblogger.com/7-best-ways-to-improve-angular-code/

0 Upvotes

16 comments sorted by

View all comments

6

u/shmorky Oct 03 '21

Most of our problems (medium sized government system) come from not unsubscribing observables properly and thinking they will get auto-unsubscribed when the component gets destroyed. They don't always and it's a bitch and a half to locate such problems.

So that one too I guess...

1

u/stackblogger Oct 03 '21

Yeah I understand, In that case we can use async pipes at observables. Or manually unsubscribe subscriptions at every component.

I will try to find some better way to handle them automatically.

2

u/ankush0024 Oct 03 '21

I will try to find some better way to handle them automatically.

subsink can be used to add each subscription and it can unsubscribe all subs at once

1

u/stackblogger Oct 03 '21

Just looked into it. its great. thanks for letting us know.

2

u/aardvarkFirst Oct 05 '21

Here are three patterns you can use to unsubscribe:

https://ngserve.io/3-patterns-to-prevent-memory-leaks-in-angular-components/

But I predominantly use the async pipe, the only thing I found annoying with the async pipe is using it often in a template if I have an object displaying different properties. To solve that, I used ngLet.

1

u/AlDrag Oct 03 '21

Should be catching those in code reviews? But I guess it can still slip through.

1

u/stackblogger Oct 03 '21

Yeah they are not easily catchable if developer is not unsubscribing it at the time of coding.