Reactive Programming: Resistance is Futile

“You will be assimilated. Resistance is futile.” — Locutus of Borg

If you grew up watching Star Trek: The Next Generation, this phrase conjures up images of cube ships and pale beings. It was the mantra of one of the greatest threats in the Star Trek universe: The Borg. The Borg consisted of various alien races that were brainwashed and converted into cybernetic beings whose sole purpose was to bring more species into the collective (with or without their permission).

What made The Borg such a formidable enemy for the crew of the Enterprise was their ability to adapt to any threat and share that ability with the rest of The Borg in real time. This feat was attributed to the connection each Borg member shared with all of the other Borg members. This hive-mind is similar to the reactive programming paradigm which performs variable persistence throughout the entirety of the codebase. In other words, if a variable’s value is updated or changed in one place that new state will propagate to all other references of that variable.

Consider this block of code:

1.    var a = 5;
2. var b = a + 5;
3. console.log('a: ' + a, 'b: ' + b);
4. a = 10;
5. console.log('a: ' + a , 'b: ' + b);

Stepping through the code we see that on line one we created a variable named “a” and set that variable to equal “5”. On line two we created a variable called “b” and set the value of “b” to be equal to the value of “a” at that exact moment in time and added 5 to “a”. Which means when we get to line three we will print out “a: 5 b: 10”. Moving forward we see that on line four we change the value of “a” to equal “10” and perform another print out and, assuming that this is vanilla Javascript, line five will print out “a: 10 b: 10”.

In the example above we created a relationship between “b” and “a” that never persisted beyond line three. In reactive programming variable dependencies, like the one above, are permanent; if “a” changes, “b” will change as well. They are forever entwined as we defined them to be.

Consider this next block of code.

1.    var a = 5;
2. var b <= a + 5;
3. console.log('a: ' + a, 'b: ' + b);
4. a = 10;
5. console.log('a: ' + a, 'b: ' + b);

It is very similar to the first example but uses the principals of reactive programming. Walking through the code, we first make our assignment to “a” on line one. Then we establish our relationship between “a” and “b” on line two. On line three we print out “a: 5 b: 10” just as we did in the first example. On line four things get a little more interesting; we update the value of “a” to now equal 10 and due to its predefined relationship to “b”, the value of “b” is also updated with the new value of “a”. Which leads to line five printing out “a: 10 b: 15".

Various programming languages perform reactive programming in their own way (some better than others). C and C++ are able to establish reference pointers to variables and evaluate those references on demand while Javascript and other scripting languages tend to require a library to perform heap lookups.

A practical application of reactive programming is displaying an investors stock portfolio. Stock prices vary from moment to moment and your average, on-the-go, busy investor just cares about one thing: How much money did I make? By defining the total to be a relationship between the various stocks prices and number of shares, when the price of a particular stock changes, so will the total. And just like that the investor has a new powerful tool to manage their portfolio.

So whether you’re the next Wolf of Wall Street or a collection of assimilated alien races from a popular Sci-fi franchise, reactive programming can ensure that your data is always synchronized and up-to-date.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.