Skip to main content

Command Palette

Search for a command to run...

Kotlin Code Smell 13 - Companion Object Functions

Yet another global access coupled with laziness that cannot be mocked.

Published
1 min read
Kotlin Code Smell 13 - Companion Object Functions
Y

I've started to work as a software engineer at 2014, however, I started to write code at high-school.

My first language was Assembly, but still, I fall in love with the possibilities to make the computer to do as you wish, shortly after that I started to write in C.

Later on I studied a practical engineering in electricity, and during this time discovered that I preferred much more writing code than design electrical components.

As a result of this understanding I decided to switch and study bachelor degree in computer science in Reichman university, where the focus was of the Java language.

Today I'm working at SumUp using Kotlin, SpringBoot & Micronaut, Cassandra and Kafka

TL;DR: Companion object functions are globally available and cannot be replaced for testing.

Problems

  • Coupling

  • Testability

  • Protocol Overloading

  • Cohesion

Solutions

  • Honor the Single Responsibility Principle by creating an instance for a class whenever possible.

  • Delegate the method to the instance, if feasible.

  • Create stateless objects instead of referring to them as "helpers."

Examples

  • Static class initializers

  • Static class methods

  • Static attributes

Sample Code

Wrong

class DateStringHelper {
    companion object {
        private val formatter = 
            DateTimeFormatter.ofPattern("yyyy-MM-dd")

        fun format(date: OffsetDateTime): String  =
            formatter.format(date)
    }
}

fun main() {
    print(DateStringHelper.format(OffsetDateTime.now()))
}

Right

class DateToStringFormatter(private val date: OffsetDateTime) {

    fun englishFormat(): String {
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        return formatter.format(date)
    }
}

fun main() {
    print(DateToStringFormatter(OffsetDateTime.now()).englishFormat())
}

Conclusion

Using companion object methods pollutes the class protocol with "library methods," which breaks cohesion and generates coupling. It is advisable to extract them through refactorings.

We cannot manipulate companion classes and use them polymorphically, so we can't mock or test them effectively.

As a result, we end up with a globally accessible reference that is challenging to decouple.

More info

Credits

Kotlin Code Smells

Part 24 of 36

In this series, we will see several symptoms and situations that make us doubt the quality of our development. We will present possible solutions. Most are just clues. They are no hard rules.

Up next

Kotlin Code Smell 12 - Ripple Effect

When Ripples Become Waves: The Importance of Decoupling

More from this blog

Yonatan Karp-Rudin | kotlin for backend developer skills | java for backend developer skills | SpringBoot | Tutorials

57 posts

Experienced Senior Software Engineer passionate about functional programming & Kotlin. Excels in app development, optimization, and team collaboration. Let's create something amazing!