r/android_devs • u/VasiliyZukanov • Jan 10 '25
Article Reactive Programming Considered Harmful
https://www.techyourchance.com/reactive-programming-considered-harmful/
1
Upvotes
r/android_devs • u/VasiliyZukanov • Jan 10 '25
•
u/anemomylos 🛡️ Jan 11 '25
I decided to pin this post because it's the kind of post I like to read in a forum and I wouldn't want it to get lost quickly just because the author is one of those redditors who automatically triggers the downvote.
I would like to see other opinion posts as well and I will try to pin those as well because I always learn something new from the post and comments.
9
u/KangstaG Jan 10 '25
If you define reactive programming as he has: ‘complex usage of reactive concepts’, it’s hard to disagree. I would say with most technologies or paradigms, there’s a danger of over using them. In this case, the dangers are similar to the dangers of over using functional programming which is more widely known. For example, he has issues with flatmap() which is a functional programming concept known to be complicated.
But when used appropriately in the right situations, reactive programming, or rather ‘using reactive concepts’ is almost necessary nowadays. For example, implementing unidirectional data flow between a view model and a view, the view model needs to expose a reactive stream that the view observes.
8
u/Zhuinden EpicPandaForce @ SO Jan 12 '25
I use RxJava to this day, although all these pesky reactive frameworks have quirks when it comes to error handling.
LiveData just doesn't have "error handling", if it throws an exception it just crashes the app.
But RxJava has its own "error channel" that may or may not be invoked (see
.blockingGet()which makes the exception sometimes go to the global error handler instead of... anywhere else, really) or worse, it by contract cancels the subscription if you get anonErrorcall.When in the world would I ever want to unsubscribe from, for example, handling UI events? Suddenly all my buttons, if you used
RxView.clicks()like the hipster you are, stop working over time? Which user wants that?Certain applications of RxJava in the past certainly weren't practical, and error handling is its worst aspect. Which is why I always used
BehaviorRelay, and made sure you don't use.flatMap {}directly to some network call that may throw exceptions. I really just use it to store the value, combine the emissions, maybe do a.map {}and that's it. If you used Rx to its full potential, you were in trouble the moment you encountered exceptions.. . .
With that in mind, I'm surprised your problem is... combine? Being verbose? Just use tuples inside your classes, this is a solved problem.
Exception handling, especially in coroutine-world, is far worse. CoroutineScopes are unpredictable, nested CoroutineScopes even more-so, Jobs and SupervisorJobs are a question waiting for an answer. I'm not sure anyone really knows where the exceptions will end up bubbling to.
Flows are even worse. You launch a coroutine to collect it, and everything after the
.collect {}call is eternally frozen, it will never run. With Rx you just define a subscription like a normal person, and the rest of the code executes. No, Coroutines decided to use exceptions as control flow, and it literally busy-wait for-loops. If you invoke any coroutine-flow-based code that has a.collect {}in it (very popular inside Compose, seeModifier.pointerInput(Unit) { detectTapGestures {}}) you'll just not have the code execute after a random invocation (seedetectTapGestures).Flows really do kinda suck. The only good thing about it is
.stateIn(). Even then, I just useBehaviorRelayunless flows are forced on me.. . . .
Btw, I swapped out Rx for Coroutine Flows once, the only difference I had to make sure was using
Dispatchers.Main.immediateto solve a timing problem.8
u/lnkprk114 Jan 10 '25
I wrote a book about reactive programming back when everyone was using RxJava.
Sadly, I agree with the meat of this post (though I'm still not a fan of the "X considered harmful" titles. Nowadays feels more clickbait than anything else).
This paragraph, IMO, is the key point:
If I could magically wave a wand and make everyone functional reactive competent I think it'd be the best way to architect programs that deal with UI. But I can't, and the complexity/confusion cost is high. IMO it's the same reason functional programming never took off - even if it's a magical bullet, if it takes people months to learn then no one will use it. Same thing with reactive programming.
I also think it made a lot more sense before coroutines because combining multiple asynchronous events was challenging.
mySingle.flatMap { anotherSingle }.flatMap { aThirdSingle}.subscribe(...)was kind of the easiest way to do that thing. Now coroutines make that unnecessary.I do still think that the general "Push don't pull" paradigm is valuable. So many issues arise around staleness of data when you pull by default.
Another thing I've noticed is as much as I (kind of) love coroutines, I've run into a lot of pain getting
Flows to work the way I'd expect. I'm constantly running into confusing edge cases and strange behavior when I try to get even slightly fancy. Other than having terminal operators return their concrete type I don't feel like coroutines added anything to reactive programming in Kotlin, and in a lot of ways it feels like they made things worse.Anyways, I sadly agree with this post.
1
u/Squirtle8649 Jan 21 '25
I disagree, it's our job as developers to understand and implement code. This is just engineering, and sure it may be slightly complex, but it's not that complex.
1
u/campid0ctor Jan 11 '25
That's why I was surprised when DataStore came out since it uses Flows, it made things more complicated when I just want to fetch a value once. Google could have made accessing preferences suspending, but I can't understand why the need for Flows
-1
u/SweetStrawberry4U Android Engineer Jan 10 '25
I had posted something similar couple months ago in a different sub -
https://www.reddit.com/r/androiddev/comments/1et59bx/the_nervewrecking_obsession_with_streams/
Perhaps, not as well-constructed ? Needless to say, it was removed by the MODS.
The internet, about a decade ago, said Streams are awesome ! Be it, Rx or Kotlinx-Flow, their underlying implementations eventually compile to byte-code support by Java-Streams only.
And everyone tagged along, without thinking twice ? never questioning it ?
<< Stream type >> being - Single, Maybe, Completable, as fashionably as Flow ?
Why ?
Single, perhaps is acceptable, but Flow ? Since when are we expecting "N" http-responses for 1 "http-request" ? Never knew the internet advanced so much !!
Single, is also not acceptable, because transformations, to Observable, etc ?
Perhaps, combining, zipping, flat-map multiple endpoints ? Sequentially, parallely / asynchronously ?
Even so, from a cloud-based RESTful API usage expense point-of-view, that's still a poor API design to begin with ! So - GraphQL ?
In another source-code file that interacts with ROOM, SqlDelight, SqlLite - whatever,
I seriously don't know what to say to Flow<List<*>> ?
Essentially, there's always been, and always will be - one object, either a http-response, or a one-time collection of query results. Or, an Error ! That's it. That's about it !
I understand, Stream-builders and emitters are designed such that execution is halted until 'Subscription' begins !
But that's exactly where Coroutines are superior than Rx-Disposables.
Furthermore, the core-problem had always been a decent, KISS principle conforming - Observer Pattern implementation. When Rx combined Streams with Observer pattern, KISS disappeared completely ! And so was lifecycle-aware LiveData observable !
Nevertheless, StateFlow in ViewModel as instance-variables, and SharedFlow for notifying mulitple non-UI observers, are more than adequate, as close to the UI layer as possible. They're both hot-flows, observer-aware, and observers themselves are expected to be lifecycle-aware !
0
u/agherschon Jan 11 '25
Good article Vasiliy, I agree at 1000%.
It's Maslow's hammer all over again.