RxJava for 100% beginners(part 2-write your code in a more elegant way)

Qing Zhong
4 min readDec 6, 2016

--

In the last section I introduced some basics from functional programming, in this section we are gonna go through the real deals! RxJava!

In case you need to review the last post:

There is a very nice example which I wanna borrow from, if anyone of you can read chinese, you are encouraged to read the original here:

if not, i think i can just copy paste the example:

in the above example, we are trying to :

  1. traverse a list of folder
  2. traverse every file from the folder list
  3. load the files which are “png” format and construct them as Bitmap
  4. load the bimap to a “View”(do it in the UI thread to avoid exception)

noted that except for step 3, every step else will be executed in a new Thread other than UI thread.

Is this example complicated? Well that depends, but one thing Im almost sure is that : These few lines of code is hard for expanding its functionalities. Lets say if we want to just take the first 10 “png” file(I guess we have to use a global variable as a counter right, which breaks the rule of functional programming XD) ? or if we want to traverse the files in Thread A, and then process the files to images in Thread B(most naive way will be using a new Thread().start(), but the code will be nasty)?

Just Imagine how complicated your code will be after the expansion in a normal way. And even if I believe you are a great software engineer, you manage to solve it, you teammates who are working on the same project will be like:”What the hell is these” when they pull your code change…..

what have done to the code?

so lets check out how RxJava can handle it better!

if we want to achieve the those extra functionalities above ->

1.take only the first 10 element. 2.switch threads between traversing files and loading bitmap

we just have to add few more “operators” (which is a term from RxJava, it refers to the way we wanna change the current stream).

I believe that there are a tons of “operators” which you are not familiar with . But thats totally fine, I will go through them one by one in the following sections, right now all you need to do is to admit that the code is much much cleaner and more readable.

oh yeah!

So By using RxJava,

  1. we flatten our code in a very elegant way, code pyramid of doom is no longer there!
code pyramid of doom

2. switching threads for different operations are easier than before!

So before we start to dive into those “operators”, there’s some conceptsI want you to keep in mind.

  1. RxJava, or general functional programming may not be a ideal tool to boost up your performance(if you have any performance issue in your app).Its just a new programming paradigm which helps you to have a cleaner and more readable code, as well as facilitates your abstract thinking and bring them to code without caring too much on the implementation details(like how to traverse a collection, how to change thread).
  2. In RxJava, every time we use a “Operator” to operate on an “Observable” (which is the “event stream” we keep talking about), we manage to get a new “Observable”. For beginners , I don’t encourage you to go too deep into how RxJava implement this “feature”, you just have to know that the “event stream” will be changed to a new one, everything else is built on top of this, thats all you have to care about.

--

--