DEV Community

Cover image for Don't follow RxJS Best Practices

Don't follow RxJS Best Practices

Nik Poltoratsky on July 15, 2019

Nowadays more and more developers learn RxJS and use it properly with best practices in mind. But we shouldn't. All those so-called best practices ...
Collapse
 
gab profile image
Gabriel Magalhães dos Santos

I could smell the junior's developer happiness reading this article

Collapse
 
kristijanfistrek profile image
KristijanFištrek

🤣

Collapse
 
benjamindelpech profile image
benjamin DELPECH

i could smell the javascript framework expert feeling superior because he knows some tricks.

Collapse
 
halcaponey profile image
halcaponey
getUser().pipe(
  switchMap(user => getDetails(user)),
  switchMap(details => getPosts(details)),
  switchMap(posts => getComments(details)),
)

should be

getUser().pipe(
  switchMap(user => getDetails(user)),
  switchMap(details => getPosts(details)),
  switchMap(posts => getComments(posts)),
)

but really should be

getUser().pipe(
  switchMap(getDetails),
  switchMap(getPosts),
  switchMap(getComments),
)

Good article, made me smile

Collapse
 
nikpoltoratsky profile image
Nik Poltoratsky

Thank you! Fixed

Collapse
 
emlautarom1 profile image
Martín Emanuel

For a moment I tought you were serious. But when you said no to pure functions I had to stop reading. Not gonna lie, you got me at the beginning.

Collapse
 
tptshoe profile image
Mark Flegg

Haha, same here... at first I was thinking, "this is interesting," then "these ideas are a bit strange," then when I got to the no pure functions part I had to scroll to the bottom to see if it was a joke.

Collapse
 
devpato profile image
Pato

sameee hahah

Collapse
 
dexterstpierre profile image
Dexter St-Pierre

We have code in our codebase that is like this.. And the people that wrote it were senior developers...

Collapse
 
nikpoltoratsky profile image
Nik Poltoratsky

I’m not telling you mustn't do that. In some particular cases, it is acceptable, so hopefully, everything is okay with your codebase 😁

Collapse
 
dexterstpierre profile image
Dexter St-Pierre

Eh, in the case of our code base it is not acceptable cases. But we are working on fixing the issues :D

Thread Thread
 
nikpoltoratsky profile image
Nik Poltoratsky

Hmm, then, good luck 😉

Collapse
 
qm3ster profile image
Mihail Malo

Is this actually a good one?

takeWhile(() => this.alive)

Wouldn't this only trigger once the source tries to push a value, so if a value is never pushed the subscription is never garbage collected?
Seems vastly inferior to

destroyed$ = new Subject()
Collapse
 
innercitypressure profile image
InnerC#ity

Fantastic!! Well done Nikita! :)

Collapse
 
lcoenenjci profile image
Loic Coenen

There's something I really don't get though. For me the difference between dumb and smart component is that the later doesn't communicate directly with the store or any other services, not that we can't pass down observable.

Collapse
 
kristijanfistrek profile image
KristijanFištrek

Ohh you sneaky, you! The pure function one killed me 😅

Collapse
 
medeirosrafael profile image
medeirosrafael

Nice. But in pipe(takeWhile(this.alive)) should be: pipe(takeWhile(() => this.alive)).

Collapse
 
anduser96 profile image
Andrei Gatej

Also, setting that value to false won’t be enough. You must emit another value so the observer can unsubscribe when the cb function returns false.

Collapse
 
nikpoltoratsky profile image
Nik Poltoratsky

Thank you! Fixed

Collapse
 
qm3ster profile image
Mihail Malo

pls fix

Also, setting that value to false won’t be enough. You must emit another value so the observer can unsubscribe when the cb function returns false.



too
Collapse
 
elasticrash profile image
Stefanos Kouroupis

Smack me as much as you want but I really hate the async pipe

Collapse
 
nikpoltoratsky profile image
Nik Poltoratsky

And what is the reason? 😃

Collapse
 
elasticrash profile image
Stefanos Kouroupis

psychological, I had weird/torturing issues in older versions of Angular. Maybe I need to try it again :P

Collapse
 
ich5003 profile image
Lucas

I'm still scared someone doesn't get the sarcasm and starts following this 😄

Collapse
 
stephangalea profile image
Stephan Galea

This article reminds me of this :) youtube.com/watch?v=X0tjziAQfNQ

Collapse
 
wlun001 profile image
Wei Lun

Oh gosh

Collapse
 
chris_bertrand profile image
Chris Bertrand

Haha, nice article. A nice checklist to follow!

Collapse
 
zerquix18 profile image
I'm Luis! \^-^/

I'm just getting started and I spotted the sarcasm. This is a great article for starters, thanks!

Collapse
 
carlillo profile image
Carlos Caballero

Oh!! Thanks! I've a post to recommend to my students!

Thanks!

Collapse
 
vitalyt profile image
Vitaly Tomilov • Edited

Best practice that I discovered with RxJS - do not use it at all. It is a flawed attempt at trying to turn everything into a cryptic stream.

Collapse
 
drflaviopsilva profile image
flavio

kkkkkk man, what a great post. congrats!! in a cool manner you make me see some things about this kind of good practice.

Collapse
 
abdallaanasser profile image
Abdalla Abdelnasser

Nice joke 😅
Pure functions and the nested subscribes are hilarious

Collapse
 
ngclient profile image
ngclient

I like It

Collapse
 
devpato profile image
Pato

OMG!!! I hate you! hahaha i was like who is this kid? he doesn't know what he is talking about, then I read the entire article lol that was funny

Collapse
 
jogelin profile image
jogelin

I was hating you until the end :D

Collapse
 
oleksandr profile image
Oleksandr

Marble example is super! Can I use same example in my talk?

Collapse
 
nikpoltoratsky profile image
Nik Poltoratsky

Definitely! It'll be awesome 😅

Collapse
 
osnersanchez profile image
osnersanchez

I need an article on good practices with the safe navigation operator for next week <3 Thank you

Collapse
 
mutembeijoe profile image
Mutembeijoe

You had me at never unsubscribe, but was back to my mind at nested subscriptions and polluting the global space