Why you should be doing Functional Reactive Programming
From an Android Developer perspective
Let me start by saying that Functional Reactive Programming is hard when you come from an imperative mindset, but once you overcome the steep learning curve, it can save time, make for a cleaner, more robust, and maintainable code base.
So let’s take a closer look at a few advantages
- Avoid the dreaded “callback hell”;
- It’s a lot simpler than regular threading;
- Has a standard mechanism for error recovery;
- Pretty straight forward and obvious way to compose asynchronous operations;
- Handling UI interactions/events is insanely easy;
- It offers the same “API” for database access, UI, computation, network access and everything you need it to be;
- It makes a lot easier to do complex threading, synchronising work in parallel and running some code when everything is done;
- Super easy way to talk to the UI thread;
- Makes concurrency almost hassle free;
- As soon as you understand the basics you can learn other operators as you need them, you don’t have to understand everything to start using it;
- Operators bring a lot of well tested code that can help you solve tasks;
- It helps make things more maintainable as you can add and remove blocks of code to a stream as needed;
- Doing things in a more functional way leads to readable declarative code that is easier to understand, test, and debug;
There are some disadvantages though
Not everything is good
- The steep learning curve is not only because of the libraries, but also thinking reactively and using FRP to solve problems.
- It’s easy to not properly handle subscriptions and accidentally leak memory;
- Although being lightweight, RxJava still adds a good amount of methods towards the dex limit (with over 3500 methods);
Wrapping it up
It’s not that hard, I’ve written an article a few months ago explaining the basics and you’ll see from the samples that it’s not that difficult to start using it. It does have a little bit of a high method count but it’s worth it. The memory leaks can be avoided with the cool RxLifecycle library from the guys at trello.
Remember: the less code you have to write, the less code you have to test, debug and maintain.