Reactive Programming as Simple

Hari Sudhan
5 min readJul 6, 2020

--

Everyone might know about reactive programming and even some might be using in your projects. But unless you understand why reactive programming model and the motivation behind it? you cannot use the paradigm to the full extent.

Reactive programming is a programming model that deals with asynchronous data streams and the specific propagation of change.

Asynchronous | Data Streams | Propagation of change. If you understand these keywords from the definition then using reactive programming in your projects would be a cake walk for you.

Asynchronous

Obviously everyone knows what is asynchronous is. When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.

Data Streams

Streams are the backbone of reactive programming, so it is necessary for you to understand what is data stream actually meant.

Figure 1: Water streams over time

Everyone might have two pipelines in your home, one is for pure water and another for salt ground water, for different usage respectively. Suppose at morning 9:00 A.M you have switched on the motor to lift the pure water from ground to tank, thus the pure water flows through a pipe and falls into pure water tank. And within 15 mins, that is by morning 9:15 A.M you have switched on the another motor to lift the salt water from ground to salt water tank through another pipe.

Usually we call the water that flows from one place to another as stream. If we write a definition for data stream as of this example then it would be

Data (water) stream is sequence of data (water) over time

In Reactive Programming, every sequence of values(data stream) are termed as observable. These sequence of values can be anything like data response from network service, number that incremented by 1 for every second like [0,1,2,3,4], location change as stream [(9.925201,78.119774), (13.082680,80.270721), (10.800820,78.689919)] etc. Don’t confuse yourself by thinking how these sequence can be formed because ReactiveX API have wide set of operators to do that.

Propagation of change

Spreading the change happening in the flow of data to the receivers. One more illustration is, if you want to purify your salt water, the best way is to install a purifier before the taps receives the water. This purifier purifies the water and spread the change to the taps.

Figure 2: Propagation of pure water to taps

The transformation in the data flow is done by the operators in ReactiveX and these transformed data are passed to the observers that are subscribed for these change.

Simple Reactive Equations

Reactive Water Flow = Water Stream + Purifier+ Propagation to taps

Reactive Programming = Observable + Operators + Propagation to observers

Starting from the creation of data stream to the end where observers receives their response, there are wide set of operators can be used in reactive programming. These operators works in the following kind of pattern.

Figure 3: Operations on streams

What makes reactive programming paradigm popular?

We cannot forget imperative programming while talking about reactive programming because the lack of automatic reactiveness in imperative model made reactive programming model more popular.

Imperative programming executes statements that change a program’s state i.e. call a function/expression that change the values of variables. Let’s have a simple operation and assignment, a = b + c this mean that a will be assigned with the result of b + c when the expression is evaluated or called. But if the values of b and c is changed then it cause no effect on the value of a unless the expression is called or evaluated again.

We are in the advanced technology era where hardwares are build to handle constantly changing inputs. It’s also easy to see that imperative paradigm may not scale to the present, and certainly not to the future. This drawback in imperative programming paved way for the more usage of reactive programming model.

Let’s have a look at the same example using reactive programming by changing the variables a, b and c to streams.

Figure 4: Reactive addition

Now if we push 1 onto stream b and c, the addition operation is automatically called, calculating the sum of 2 and pushing it onto stream a. So if later on the value of b changes, we simply push the new value to the stream b and then this new value of b is automatically added with the previously pushed value of c and the result is pushed to stream a.

With reactive programming we don’t call expression instead we just define how the streams are plumbed together and start pushing values onto streams. Let the plumbing and operations handle the rest.

In layman term, reactive programming is like plumbing. We decide which pipes we need in our application, how those pipes are connected together and processed, then we turn on the water and sit back.

Reactive Extensions (ReactiveX)

Reactive extensions is an API for implementing reactive programming using observables, operators, schedulers and observers with multiple language implementations including RxJs, RxJava, RxKotlin, RxPy and RxSwift.

  1. Do more with less: API is built in such a way that you can do more with less, which means very easy to write more functionalities using simple declarations within few lines of code.
  2. Easy Multithreading: Muti-threading let the app to make better use of the cores inside the device CPU. Coding for concurrency before ReactiveX was really painful. But now you just need to declare the thread on which you want the task to be executed (declaratively) instead of creating and managing threads(imperatively).
  3. Relaxed Error Handling: If you turn the pages of programming history, for any asynchronous calls we do have ways to handle success response. But to handle unexpected exceptions we have to enclose the code with try..catch. Your code really looks messy, patchy and difficult to deal with if you use try..catch for all the error scenarios. Rx provides you a sophisticated error handling mechanism using which you can handle the exceptions sitting relaxedly.
  4. Bunch of powerful operators: Rx api provides you operators that make your programming life easy.

Rx gives you a toolbox that you can use to implement solutions. But it is in our hand to use the toolbox wisely.

To know more about RxJava 2 and to learn about the concept with real time examples have a look at my below article link.

If you found this post useful, it would help others to find it if you could click on the 👏 icon below and also consider sharing it with your friends via social media. Do follow for more articles like this — thanks!

--

--

Hari Sudhan

Learn, extract, and teach others something old but rarely realised in every step of your life.