Go With the Flow: A Reactive State of Mind
Programmers-- are you sick of passing around state?
How much time do you spend maintaining state variables, tracking state variable values across multiple threads and asynchronous callbacks, and painstakingly caring for state variables until you accidentally feed one after midnight and it turns into a gremlin?
In many languages, it seems like our job as programmers is becoming more about passing state around than it is about writing business logic. Much of this is due to the added complexity caused by asynchronous callbacks.
- Reactive to the Rescue
There is another option. It is new paradigm called "reactive programming."
Reactive programming treats data variables as flows or streams of events over time. These event streams can be combined, filtered, mapped, and manipulated in many ways.
Reactive programming borrows ideas from functional languages like Haskell and Scala. However, the difficulty of writing programs with reactive extensions is greatly reduced because programs are still allowed to use easy concepts of "imperative" languages like variables, looping, and side effects.
Programming with streams requires far fewer state variables, offers thread safety benefits, and does not suffer from "callback hell."
- Who Uses This?
There are plenty of lesser-known companies who use reactive extensions. But there are more than a few household names as well.
<img height="60%" width="60%" src="http://reactivex.io/assets/netflix-logo.png" alt="Netflix">
"Netflix is a big believer in the Rx model, because Rx has made it much easier for us to build complex asynchronous programs. RxJava is effective on the server for us and it spreads deeper into our code the more we use it."
<img height="60%" width="60%" src="http://reactivex.io/assets/microsoft-logo.png" alt="Microsoft">
"Reactive Extensions is a set of libraries that makes asynchronous programming a lot easier. If asynchronous spaghetti code were a disease, Rx is the cure.”
<img height="60%" width="60%" src="http://reactivex.io/assets/github-logo.png" alt="GitHub">
"Using Rx and ReactiveUI, we've written a fast, nearly 100% asynchronous, responsive application, while still having 100% deterministic, reliable unit tests. The desktop developers at GitHub loved Rx so much that the Mac team created their own version and are now using it on the Mac to obtain similar benefits."
- What We'll Cover
We will start by discussing Java 8 streams. This will provide a quick introduction to the concepts.
Then we'll dive into Netflix's library providing reactive extensions to Java, RxJava. RxJava can do everything Java 8 streams can do, but do it asynchronously.
Finally, we'll explore Elm, a functional reactive programming language for the web that offers both two-way data binding similar to AngularJS and some truly unique features-- hot-swapping of running code and a time-traveling debugger. Elm's unusual features allow a programmer to make and observe program changes while the live program is running.
- Will I Benefit?
You should understand the concepts of programming in at least one language. Reactive extensions exist for more than a dozen languages, including the following list. These extensions offer a real paradigm shift for authoring asynchronous code, handling errors, and managing state.
- Java: RxJava
- C#: Rx.NET
- C#(Unity): UniRx
- Scala: RxScala
- Clojure: RxClojure
- C++: RxCpp
- Ruby: Rx.rb
- Python: RxPY
- Groovy: RxGroovy
- JRuby: RxJRuby
- Kotlin: RxKotlin
- Cocoa: ReactiveCocoa
True functional reactive programming (FRP) languages like Elm provide a real WOW factor for those who have never witnessed a time-traveling debugger in use. As a programmer, it would be hard to walk away from this session without learning something interesting.
I gave an Android-specific version of this talk in front of over 100 developers at AnDevCon 2014 in San Francisco and 96% of attendees gave positive reviews. Several tweeted that this talk was one of their favorites at the convention.
Learn more at [ReactiveX](http://reactivex.io).blog comments powered by Disqus