Reason for using reactive programming in simple cases
Asked Answered
N

4

19

Please, can somebody explain me what are the advantages of using reactive style:

Observable<String> greeting = Observable.just("Hello");
Observable<String> yelling = greeting.map(s -> s.toUppercase());

instead of simple imperative style:

String greeting = "Hello";
String yelling = greeting.toUppercase();

I understand reactive programming like the same “API” for database access, UI, computation, network access and etc. But why we need to use reactive programming for simple toUppercase

Nilson answered 11/11, 2017 at 17:33 Comment(0)
S
21

Apart of all no blocking features, another great feature to use Reactive programing, is the important use of backpressure. Normally is used in situations where your publisher emit more information than your consumer can process.

So having this mechanism you can control the flow of traffic between both and avoid the nasty out of memory problems.

You can see some practicle examples of Reactive programing here https://github.com/politrons/reactive

And about back pressure here https://github.com/politrons/Akka/blob/master/src/main/scala/stream/BackPressure.scala

By the way, the only disadvantage about reactive programing, is the curve of learning because you´re changing paradigm of programing. But nowadays all important companies respect and follow the reactive manifesto http://www.reactivemanifesto.org/

If you want to see some practical examples you can reference here https://github.com/politrons/reactive

Subaudition answered 13/11, 2017 at 8:17 Comment(4)
Hi @Paul, does Reactive programming use NIO behind the scenes ? ThanksFeudist
Is it being used by any well known companies? In Fortune 100 ones?Disembogue
For imperative programming, where does the concept of over producing exist, to make "backpressure" relevant ?Cirrate
reactive-manifesto is something different, usual BS as any other manifestoCirrate
C
7

Everything you can do with Reactive Programming you also can do using simple functions or simple event listener.

In asynchronous programming: Reactive Programming most of the case cannot even make your codebase smaller or easier to read. But it will make your code more robust, easy to extend for later use.

Exceptionally good when data is sent as a stream especially when there are multiple elements pushed on the stream and different times, and you need to do timing-related stuff, Reactive programming makes the code a lot more maintainable.

Constructionist answered 12/11, 2017 at 4:44 Comment(0)
P
6

You are right, you don't need to use RxJava "for simple toUppercase". Reactive programming have advantages when you work with asynchronous data streams.

Pericycle answered 11/11, 2017 at 17:35 Comment(0)
G
3

There is no real use of reactive programming in the above example you've given. It gets the job done in a different way.

The real advantage of reactive programming kicks in, when you are working with streams of data and you want to do operations on them in a simple and effective manner which you can run on your preferred thread. Reactive programming has lots of operators which scares a novice programmer, but once you learn the basics it will become easier to understand which operator suits your needs.

Have a look at this article Simple background polling with RxJava and think of how to obtain the same in plain java with few lines of code.

I always preferred more lines of code. My defence was that more lines of code means more easier to comprehend. But once you start working on big projects with a huge team, it becomes difficult to understand the codebase.

Below which among the two is more understandable?

take(10);

OR

int limit = list.size() < 10 ? list.size() : 10;
for (int i = 0 ; i < limit ; i ++) {
    Log.d("Value", list.get(i));
}

Both gets first 10 items in the list. If the list is smaller then it gets the total size.

Gadmann answered 12/11, 2017 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.