What is Reactive Programming

I've been doing a bunch of interviews lately. In particular I'm interviewing for Android positions (finally). A question that always gets asked is my thoughts on reactive programming. Why? I really don't know and tend to forget what it means. So in this post I will attempt to breakdown what reactive programming is and maybe give you some pointers from the Android perspective.

Android Perspective

Ok so this is going to sound bad but at this point as you can see I still don't think I know what reactive programming is. I know using it can shorten code and looks cleaner. The pitfall is that you actually have to know what is going on with every little call. Why? Well everything is asynchronous.

Kotlin uses reactive programming but that's not why I'm using it. I use Kotlin because Google started writing more of their documentation with it and I know it's going to become the standard. While learning Kotlin I've gotten to see first hand how "simple" reactive programming is.

One no longer needs to have these big complex methods to trigger updates. Instead your code is always looking for info to change and adapts automatically. As a mobile developer this makes things really easy since MVC (model-view-controller) is standard for Android development as well. When building out classes I can see a difference where you no longer need to explicitly use getters and setters. We all know at this point that getters/setters are imparitive for any class but now you can just designate variables and the language does the rest.

I guess my easiest way to comprehend Reactive Programming is to just say it's code that...well..reacts. Just as we as humans are able to take in information and then make changes accordingly then the same can be said here for this coding practice. So to leave you with a concrete example I'll show how a boilerplate class is built in Java vs Kotlin.

Class Example

//Java firebase Login
mAuth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                // Sign in success, update UI with the signed-in user's information
                FirebaseUser user = mAuth.getCurrentUser();
                updateUI(user);
            } else {
                System.out.println("I did it!!")
            }

            // ...
        }
    });
//Kotlin firebase login
mAuth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener(this) {
        fun onComplete(@NonNull task: Task<AuthResult>) {
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                FirebaseUser user = mAuth.getCurrentUser!!;
                updateUI(user);
            } else {
                println ("You don't exist")
            }

            // ...
        }
    }

Resources

Intro to Reactive Programming

09/17/2018: Edits thanks to Mike Ornellas
10/6/2018: Code change suggested by Adam Conrad