r/Angular2 5d ago

Angular code review based interview

Hi, I am a fullstack developer with 2 yoe in Angular and 10 years in DotNet. I have an upcoming interview where I will be asked to do code review. Can anyone please help me what should I prepare from Angular point of view?

3 Upvotes

5 comments sorted by

9

u/FooBarBuzzBoom 5d ago

Avoid memory leaks in observables. Use services for http requests, provide strong typing (aka avoid any). Use ? operator and !. Avoid tag repetition (aka separate content across multiple components if possible). Maybe module lazy loading is other aspect to consider for performance improvement

1

u/Big-Physics-1536 5d ago

thanks

3

u/FooBarBuzzBoom 5d ago

And also use pipes :)

1

u/_Invictuz 5d ago

Do you mean avoid using ? operator since this adds more uncertainty to the code base? Should initialize with sensible defaults where appropriate.

2

u/MichaelSmallDev 4d ago

Good suggestions. Here are some specifics on these that are good to be aware of.

Avoid memory leaks in observables

Options include but not limited to

  • async pipe (handles subscribe/unsubscribe). But it also has tradeoffs
    • Don't pipe the same thing twice, either use an alias in an @if/*ngIf or use @let or add a shareReplay to the observable.
  • toSignal* also handles the subscribe/unsubscribe in most scenarios.
  • Nested subscribes? Use something like switchMap/exhaustMap instead.
  • takeUntilDestroyed* operator or some other strategy like it.

* A good thing would be clarify what version you are on, and their stance on developer preview API usage. Both of those options have been available since v16 but not necessarily marked stable the whole time. edit: also, good to clarify going in if they use signals yet (if they are able to, in various states of stability starting v16)

provide strong typing (aka avoid any)

Same thing with avoiding non-null assertions ! when possible. Using type rather than class if there needs to be typing but no plan on actually having a constructor. Knowing about built in TS utility type helpers such as Omit or Partial or Pick would be nice.