Albert P
Albert P

Reputation: 375

Should I use reactive programming (RxJava) to solve complex problems?

I have an application where I started to represent a stream of events as an RxJava Observable. The requirements and structure of the events were simple at the beginning, and the operators that came with RxJava were a very good fit for the simple transformations that were needed.

Since then, however, the requirements and business logic became much more complex. The need came to correlate events in very special ways, and the operations on the Observable that were so nice and friendly before became heavy chunks of code. I had to write custom transformers which I use from other custom transformers and so on.

Altough I am sure that the code would have become at least as much complicated without using RxJava as it is now, I am concerned about the following things:

  1. Everything is written in a reactive style, which I think is difficult to get for most newcomers, and I am worried about the limitations that this poses on further contributions to the project.
  2. A friend of mine (who is also working on the project) has mentioned several times that RxJava should be used much more simply. He said that any RxJava-based project he has seen used simple operators without nesting custom transformers in each other.
  3. Testing the code became difficult. Unless I mock my smaller transformers, I have to test everything together, but otherwise I have to use them as constructor arguments of transformers that use them, which does not feel right, even with injection.

I believe that there has to be a definite answer to my question: Is RxJava supposed to tackle complex problems, and if so, in what way? How should these solutions be tested? Right now I could only came up with solutions that require organizing the code into separate units so that I do not have hundreds of lines of continuous code fiddling with groupyBy,flatMap,scan and combineLatest operators nested in each other several levels deep.

Thank you for your answer!

(btw, I grew quite fond of reactive programming, as it proved to be more straightforward at certain problems, especially where it helped me avoid to directly handle mutable state. Maybe the testability is what I am most worried about. I usually use dependency injection in kotlin and write separate unit tests for everything, but I doubt that this is the best solution in RxJava)

Upvotes: 3

Views: 1160

Answers (1)

Bob Dalgleish
Bob Dalgleish

Reputation: 8227

RxJava is like any good tool: when used right it does a good job. You have let the code grow as the requirements grew, and your "good tool" is starting to weigh you down.

RxJava is extremely useful for solving problems that require complex coordination across thread boundaries. The operators allow you to "herd" your data on to the desired threads and ensures thread-safe operations without requiring, in most cases, excessive uses of synchronized and volatile constructs.

Refactor Your Code If something is difficult to unit test, refactor it. If something is difficult to understand, refactor it and/or redesign it.

Reactive style is something that is learned. Don't back away from it because it is difficult, or because other people may not understand it. The most important capability that the reactive style gives you is the ability to reason about your code, even crossing thread boundaries, and time and space itself :).

Upvotes: 2

Related Questions