Comparison of how readable, easy and maintainable the code is using reactive OR kotlin coroutine. Given that both are equally scalable
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

How coroutines improve code readability

Programs must be written for people to read, and only incidentally for machines to execute. — Abelson and Sussman

Kotlin coroutines provide an easy way to write highly scalable code, using traditional style of programming, while avoiding a thread allocated to each task. Much has been written about light weight threads and many resources are available on the internet, if you want to read more about this.

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

You can read more about Kotlin coroutines here. A very exciting Project Loom is going to bring the light weight thread model to Java. It is a simialr concept as GOLang routines.


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

  • The process starts with calling getOrderInfo and getShipmentInfo, in parallel. This part showcaes how calls can be made in a non-blocking way.
  • When the methods calls menrtiond above are completed, the process calls the sendEmail method.

Let's call the processOrder function as the process function and the individual getOrderInfo, getShipmentInfo and sendEmail functions as business functions.

This code is available in this repo. It is showcasing how Kotlin couroutines can be used to write more readable code as compared to reactive approach without loosing the scalibility benefits.

Simple is beautiful

In this section, a comparison of code is provided if it is written using Kotlin's aproach vs the reactive approach.

Business Functions

Note that, in kotlin, the fuctions are just representing the business logic with very less additional overhead.

fun getOrderInfo(orderId: String): String {
    return "Order Info $orderId"

The same function needs to understand the reactor concepts e.g. Mono as shown below.

Mono<String> getOrderInfo(String orderId) {
        return Mono.just("Order Info " + orderId);

Process Function

The main fucntion which calls the methods is seen below. Note that using Kotlin's approach, the code is highly readable. The code documents itself for what business fucntion it is implementing.

fun processOrder()  {
    val orderId = "SN19876"

    val orderInfo = async { getOrderInfo(orderId) }
    val shipmentInfo = async { getShipmentInfo(orderId) }

    sendEmail(shipmentInfo.await(), orderInfo.await())

The following code implements the same business process as implemented above. However, the readability of the code mentioned below is reduced and the non-fucntional behaviour of the code is being mixed with the fucntional aspect of the process.

    void processOrder()  {
        String orderIdNumber = "SN19876";, getShipmentInfo(orderIdNumber))
                .flatMap(data -> sendEmail(data.getT1(), data.getT2()))
                .doOnSuccess(o -> System.out.println("Email sent " + o))


From the example above, it is evident that coroutines provides a better alternative to write more readable code. The reactive code I have mentioned can be optimised to reduce more clutter, but there will always be reactive specific classes which I can avoid now.

Kotlin is an exciting new programming language specailly if you are coming from java background. You can start your kotlin journey by attending this coursera course.

This project uses the following technologies.