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

  1. Avoid the dreaded “callback hell”;
  2. It’s a lot simpler than regular threading;
  3. Has a standard mechanism for error recovery;
  4. Pretty straight forward and obvious way to compose asynchronous operations;
  5. Handling UI interactions/events is insanely easy;
  6. It offers the same “API” for database access, UI, computation, network access and everything you need it to be;
  7. It makes a lot easier to do complex threading, synchronising work in parallel and running some code when everything is done;
  8. Super easy way to talk to the UI thread;
  9. Makes concurrency almost hassle free;
  10. 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;
  11. Operators bring a lot of well tested code that can help you solve tasks;
  12. It helps make things more maintainable as you can add and remove blocks of code to a stream as needed;
  13. 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

  1. The steep learning curve is not only because of the libraries, but also thinking reactively and using FRP to solve problems.
  2. It’s easy to not properly handle subscriptions and accidentally leak memory;
  3. 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.