Solutions — Enter MVI & functional Reactive Programming

These are 2 very popular buzzwords so lets break them down to what they mean and how they can solve some of our issues. For more info check out this awesome article.

Reactive

Something reacting to an event rather than requesting data.

Functional

No side effects, a function that take an input and gives an output, it will produce the same result given the same input.

An important concept from both of these is immutable data.

If you think of both of these concepts in the context of an architecture, combined with the lessons we have learned from MVP example above, we can start to create a structure that forces us to write better code.

Reactive programming, immutable data and pure functions help us quickly create single responsible code, that is narrowly scoped only affecting local state, has no side effects and is very easy to reason about. — Dan Lew

Below is a very simple example of what a Functional Reactive stream might look like, you can see how each step is reacting to the input from the previous step and how no variables are mutated or exist outside of the stream.

It is extremely easy to imagine what state the objects will be in each step and based on the next function what the outcome will be. If this was an architecture you would quickly understand what each section is doing and the state of an input at any given moment.

State

One of the main issues with the MVP example above is that there is no explicit state. All of the impure functions, mutable data, and multidirectional data flow leave us with no clue about what state the view is in. This can be partly solved by having an explicit state that cannot be mutated.

Say you have a page which loads 2 scores for a team from the network and then shows them on the view:

Simply by explicitly saying what states the view can have we immediately make it easier to understand what the application is doing.

Model View Intent — Functional Reactive Programming as an architecture

Frontend-web developers used to have this problem, it was common to use Model-View-Controller (MVC) they solved this issue by using a framework called redux, an architecture based entirely around a functional approach. Below is a quote that summarizes the issues discussed in this article very well:

“The Redux architecture encourages a functional approach where you compose pure functions. Pure functions are basic functions that are deterministic in nature. Meaning that the output of a given function would always yield the same result if called with the same inputs. This is because pure functions have no internal state and leave no side effects.” —Nish Tahir

So redux is functional, but it also uses Reactive programming.Take a look at the redux diagram below, you can see how each step leads to the next and then “triggers” the next step.

This can be a great example for us Android developers, most of our current solutions encourage a functional reactive approach like this.

Lots of people have written and talked about possible architectures for android that use a functional reactive approach and solve lots of problems highlighted in the MVP example. Simply put MVI follows the to following steps (notice how similar it is to redux):

The view generates Intens (not android intents) Intents are turned into actions (to provide a layer of abstraction from each view) These actions are processed by a function that does something, maybe performs some validation, business logic or network request and outputs a result (eg. the validation was ok or failed) A reducer then takes this result and produces a new state This view is then rendered to the user

All these steps are reacting to an intent at the start of a stream, all purple squares a immutable data type, all the yellow briefcases are pure functions.

Just like in the tiny example of a Functional Reactive stream you can see how it is extremely easy to reason about the state of any object at any given point.

For more information about implementation I can’t recommend highly enough The Contract of the Model-View-Intent Architecture by Benoît Quenaudon and his talk at Droidcon NYC 2017