Skip to main content
AI
  • Get started with AI

    • Red Hat AI

      Accelerate the development and deployment of enterprise AI solutions.

    • AI learning hub

      Explore learning materials and tools, organized by task.

    • AI interactive demos

      Click through scenarios with Red Hat AI, including training LLMs and more.

    • AI/ML learning paths

      Expand your Red Hat OpenShift AI knowledge using these learning resources.

    • AI quickstarts

      Focused AI use cases designed for fast deployment on Red Hat AI platforms.

    • No-cost AI training

      Foundational Red Hat AI training.

  • Featured resources

    • Red Hat OpenShift AI
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

Red Hat AI Factory with NVIDIA

Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.

Learn
  • Self-guided

    • Documentation

      Find answers, get step-by-step guidance, and learn how to use Red Hat products.

    • Learning paths

      Explore curated walkthroughs for common development tasks.

    • Guided learning

      Receive custom learning paths powered by our AI assistant.

    • See all learning
  • Hands-on

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

    • Interactive labs

      Learn by doing in these hands-on, browser-based experiences.

    • Interactive demos

      Click through product features in these guided tours.

  • Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics
  • Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
Build
  • Get started

    • Red Hat build of Podman Desktop

      A downloadable, local development hub to experiment with our products and builds.

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

  • Download products

    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products
  • Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset
  • References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
Community
  • Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions
  • Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github
  • Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

Join the Red Hat Developer program

Download Red Hat products and project builds, access support documentation, learning content, and more.

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

How Kotlin's coroutines improve code readability

December 3, 2018
Faisal Masood
Related topics:
Java
    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.

    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.

    Last updated: December 9, 2018

    Recent Posts

    • A day in the life of an NVIDIA H100 on Red Hat OpenShift 5

    • Migrate BuildConfigs to builds for Red Hat OpenShift with one plug-in install

    • Run Red Hat Enterprise Linux CoreOS on NVIDIA BlueField DPUs

    • Agent harness engineering: Separating model capabilities from enforcement

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    Red Hat Developers logo

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    Red Hat Developer

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility
    Ask AI