How Kotlin’s coroutines improve code readability

How Kotlin’s coroutines improve code readability
Programs must be written for people to read, and only incidentally for machines to execute. — Abelson and Sussman

Kotlin is a new practical language designed to solve real-world problems. It is based on JVM but there are many differences between Kotlin and Java. Kotlin is a null-safe and concise language with support for functional programming. You can try programming in Kotlin here.

Kotlin coroutines provide an easy way to write highly scalable code, using the traditional style of programming, while avoiding having a thread allocated to each task.

In this article, I focus on code readability and how, in my opinion, coroutines provide a cleaner approach to writing code compared to a reactive approach. I have used Project Reactor to showcase the reactive code; however, the example can be extended to any reactive library, for example, RxJava and CompleteableFuture. Note that coroutine-based code scales as well as code written using a reactive approach. To me, coroutines are a win-win situation for developers.

You can read more about Kotlin coroutines here. The very exciting Project Loom is going to bring the lightweight thread model to Java. It is a similar concept as GOLang routines.

Approach

I have implemented the following workflow using both Reactor and the coroutines approach. The main function is processOrder, which performs the following:

  • The process starts with concurrent calls to getOrderInfo and getShipmentInfo, in parallel.
  • Upon completion of both the methods mentioned above, the process calls the sendEmail method.

Let’s call the processOrder function the process function and let’s call the individual getOrderInfo, getShipmentInfo, and sendEmail functions business functions. This code is available in this repo. It showcases how Kotlin couroutines can be used to write more-readable code compared to a reactive approach without losing the scalability benefits.

Everything you need to grow your career.

With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.

SIGN UP

Simple is beautiful

In this section, you can find code that implements the getOrderInfo and getShipmentInfo business functions and the processOrder process function using Kotlin’s coroutines and also using a reactive approach.

Business Functions

Following are examples of the business functions using Kotlin’s approach. Note that these functions are just representing the business logic with much less non-functional overhead:

fun getOrderInfo(orderId: String): String {
     return "Order Info $orderId"
}
fun getShipmentInfo(orderId: String): String {
     return "Shipped for order $orderId"
}

Note that the same functions when written in Kotlin, mix the non-functional aspects of the code with the business logic.

Mono<String> getOrderInfo(String orderId) {
     return Mono.just("Order Info " + orderId);
}
Mono<String> getShipmentInfo(StringorderId) {
     return Mono.just("Shipped for order "+ orderId);
}

Process Function

In this section, you can find code that implements the processOrder process function using Kotlin’s coroutines and a reactive approach. Note that by using Kotlin’s approach, the code is highly readable. The code documents itself for what the business function is trying to do.

fun processOrder() = runBlocking {
  val orderId = "SN19876"
  val orderInfo = async { getOrderInfo(orderId) }
  val shipmentInfo = async { getShipmentInfo(orderId) }
  sendEmail(shipmentInfo.await(), orderInfo.await())
}

The snippet below shows the same business process implemented using the reactive approach. The code below is less readable because the non-functional aspects of the code are present with the functional aspects of the business process.

void processOrder() {
  String orderIdNumber = "SN19876";

  Mono.zip(getOrderInfo(orderIdNumber), getShipmentInfo(orderIdNumber))
      .flatMap(data -> sendEmail(data.getT1(), data.getT2()))
      .doOnSuccess(o -> System.out.println("Email sent " + o))
      .subscribe();
}

Conclusion

From the examples above, it is evident that coroutines provide a better alternative for writing more-readable code. The reactive code referenced in this blog can be optimized to reduce more noise, but there will always be non-functional aspects of the code being mixed with the business aspect, which I can avoid now using Kotlin’s coroutines.

Kotlin is an exciting new programming language, especially if you are coming from a Java background. You can start your Kotlin journey by attending this coursera course.

In addition, you might find this article interesting: Inter-Reactive Kotlin Applications.

Share