Kotlin Extensions - Giving Voice to Your Domain Model

by Spas Poptchev

In the world of software development, the domain model is at the heart of everything we do. It's like the storybook of our application, trying to capture the complex world in a simple and organized way. But, there's a challenge we often face: the programming language's type system. This system, which is supposed to help us by checking our code for errors, can sometimes feel more like a barrier. It can make our domain model - our story - harder to tell because it doesn't always match the way we think.

Throughout my experience in coding across different languages, I've consistently run into this problem. The tools we use to write our code can sometimes make our ideas feel boxed in, making it harder than it should be to bring our domain model to life.

In Kotlin there is a special feature called extensions which help to bridge the gap. These extensions allow us to add new features (functions and properties) to existing classes without changing their code, making it easier to express our domain model in a way that feels natural. It's like giving us the magic to shape our code to better tell our story, without the constraints of the type system getting in the way.

In this article, we are not only utilizing Kotlin to navigate the limitations of the type system effectively; we are collaborating with it to refine our code. Extensions allow us to transform the static and occasionally cumbersome language of our code into a dynamic narrative that more accurately reflects our thought process. Let's explore how Kotlin can transform the way we write and think about our code, making our domain models not only more readable but also more maintainable.

Introduction to Kotlin Extensions

Imagine you have a toolbox. In it, you've got all the basics for your projects — hammers, screwdrivers, pliers. But then, you find a special tool, one that can adapt to tasks you hadn't even thought possible, without the need to buy new tools or alter the ones you already have. This is what Kotlin's extensions are like in the world of programming.

Simply put, extensions allow us to add new functionalities to existing classes without the need to modify them or use inheritance. Think of it as sticking a new, custom tool onto your existing toolkit. The original tool isn't changed at all, but now you can do more with it.

Here's how they work: Extensions give you the power to 'extend' a class with new functionalities. Let's say you're working with a string of text, and you find yourself frequently needing to check if that string is a palindrome (a word that reads the same backward as forward). Normally, you might write a utility function elsewhere and pass your string to it:

fun isPalindrome(value: String) = value == value.reversed() // very basic implementation

// usage
fun main() {
    val result = isPalindrome("racecar")
    println("Is palindrome: $result") // -> Is palindrome: true

With Kotlin, however, you can add a new function directly to the String class called isPalindrome(). This new (extension) function feels as if it's part of the original String class, making your code cleaner and more intuitive to read and write:

fun String.isPalindrome() = this == this.reversed() // or better: this == reversed()

// usage
fun main() {
    val result = "racecar".isPalindrome()
    println("Is palindrome: $result") // -> Is palindrome: true

Not only can you add new functions to existing classes, but you can also introduce new properties in a similar fashion. This means you can also attach new fields to classes without altering their original structure:

val String.isPalindrome
    get() = this == reversed()

// usage
fun main() {
    val result = "racecar".isPalindrome
    println("Is palindrome: $result") // -> Is palindrome: true

The beauty of extensions is their simplicity and elegance. They blend seamlessly into existing classes, making it seem as though the new functionalities were always supposed to be there. This feature doesn't just add to the readability and maintainability of your code; it aligns well with the way we think and work, making our code more expressive.

Through extensions, we're not hacking the system or piling on cumbersome workarounds. Instead, we're elegantly enhancing our toolkit, making our code not just more efficient but also more enjoyable to work with. It's like being able to write in your language, exactly the way you think, directly into the fabric of your code.

In the next part, we'll dive deeper into how these extensions can be applied, transforming the way we interact with our domain models and bringing our ideas to life with greater clarity and less friction.

Enhancing Readability and Expressiveness

In our coding journey, we often encounter the need to translate the simple building blocks of programming—like strings of text, dates, or numbers—into more complex, domain-specific concepts that carry real-world significance. This transition from basic data types to rich domain types is important in crafting software that not only functions efficiently but also resonates clearly with human understanding.

We can use extensions to bridge this gap. They allow us to elegantly wrap these basic types, whether they're strings, numbers, or any other primitive data type, into custom types tailored to our specific domain. This wrapping process enriches our code, making it not just a set of instructions for a computer to execute, but a story that's meaningful to those who interact with it.

For better understanding, we will use a measurement domain in this example. Here, numbers aren't just numbers; they represent lengths, weights, or volumes. For example, when dealing with lengths — measured in meters or centimeters — the ability to seamlessly switch between and operate on these units can significantly reduce complexity.

Let's start by looking at a code example that does not use extensions. We define our basic Length class, focusing on meters as the unit of measurement:

data class Length(val meters: Double) {
    fun add(other: Length) = Length(meters + other.meters)

val lengthInMeters = Length(1.0) // 1 meter
val lengthInCentimeters = Length(0.01) // 1 centimeter, assuming Length is defined in meters
val totalLength = lengthInMeters.add(lengthInCentimeters)

Here, even simple operations require us to wrap our head around conversions and method calls. Now, let's improve this with extensions and operator overloading:

data class Length(val meters: Double) {
    operator fun plus(other: Length) = Length(meters + other.meters)

This setup is already quite handy. It allows us to replace the add function with a + operator:

val lengthInMeters = Length(1.0)
val lengthInCentimeters = Length(0.01)
val totalLength = lengthInMeters + lengthInCentimeters

But we can make it even more intuitive. Let's introduce an extension field for Int to seamlessly work with meters and centimeters:

val Int.meters
    get() = Length(this.toDouble())

val Int.centimeters
    get() = Length(this / 100.0)

With these in place, expressing lengths and performing operations on them becomes straightforward:

val totalLength = 2.meters + 100.centimeters

Notice how the operation 2.meters + 100.centimeters not only looks cleaner but is instantly recognizable for what it does — adding two meters to one hundred centimeters. Using extensions to transform simple types into our domain-specific types does more than just tidy up our code; it brings it in line with the way we think about measurements in everyday life. This makes our code easier to read, letting us talk about domain ideas directly and in a straightforward manner.

This improvement isn't limited to our main code; it's incredibly useful in our tests as well. The clarity it brings is especially important there. Let's look at how this plays out with lengths in our test scenarios:

fun `verify length calculation`() {
    val roomLength = 5.meters
    val tableLength = 2.meters

    val totalLength = roomLength + tableLength

    totalLength shouldBe 7.meters

In this test, the way extensions let us write code helps us see right away what we're setting up and what we're testing, skipping over the need to decode or navigate through unnecessary code.


It's clear to see the powerful impact Kotlin extensions have on bridging the gap between the simple data types we start with and the rich, meaningful models we aim to build. By allowing us to extend basic types into something far more expressive and aligned with our domain, extensions not only clean up our code but make it resonate with real-world concepts, making both writing and understanding our code a smoother experience.

Especially noteworthy is how these extensions simplify testing. They transform what could be a cumbersome setup into clear, straightforward test cases. This not only makes our tests easier to write but also turns them into a more readable and reliable reflection of our intentions.

I encourage you, whether you're tackling a complex domain model or looking for ways to make your tests more intuitive, to give Kotlin extensions a try in your projects. The simplicity and clarity they bring to your code can significantly enhance its quality, readability, and maintainability. Start small, experiment with extending different types, and see firsthand how your code transforms. You might just find that extensions become an indispensable part of your coding toolkit, illuminating your domain models and tests with precision and ease.

How can we help?

We're passionate about solving challenges and turning exciting ideas into reality, together with you. If you have any questions or need assistance with your projects, we're here to help. Don't hesitate to get in touch!

Book a Call
or send a message