Function Properties in Data Classes are Code Smells

To me, using functions as properties in the primary constructor of a data class is a code smell. Here’s why: Data classes represent data. Data is a value. Data is never executed. Functions are not data. They produce values when executed. Note: By the book, a function returns a value, while a procedure executes commands. In both cases, neither is data. Why It Matters Kotlin generates key methods for data classes based on the properties in the primary constructor, such as:...

November 29, 2024

Robolectric in commonTest

Sharing tests across Kotlin Multiplatform (KMP) projects can be tricky when dealing with platform-specific APIs like Android’s Bundle. commonTest on Android relies on the androidTest source set, which uses an empty android.jar, leading to test failures. The Solution The ideal solution is to avoid platform-specific APIs in commonTest. If that’s not an option, you can use Robolectric in your commonTest source set to access functional Android classes. This approach: Allows testing of platform-specific code in commonTest....

November 28, 2024

Extension Shadowing for Actual Declarations in KMP

Heads-up: this article assumes familiarity with Kotlin’s extension functions and expect and actual declarations in Kotlin Multiplatform (KMP). My work has recently focused on “commonizing”1 APIs, and I came across KT-70012, which I believe merits attention. In Kotlin JVM development, the EXTENSION_SHADOWED_BY_MEMBER warning indicates that an extension function is redundant, as it will always be overshadowed by a member function with the same name when invoked. However, in KMP, this behaviour can be useful, as shadowing may occur on some platforms but not all....

November 7, 2024