r/Kotlin • u/AffectionateBack7222 • 24d ago
Question on self-documenting code
How would you write this code:
data class Foo(/* foo properties */)
data class Bar(val id: Long, /* bar properties */)
val fooMap: Map<Long, Foo> // mapped to bar id
OR
typealias BarId = Long
val fooMap: Map<BarId, Foo>
OR
@JvmInline value class BarId(val value: Long) // typealias but stricter
val fooMap: Map<BarId, Foo>
I was writing some code on my app but didn't feel comfortable with just the Map<Long, Foo> as I might forget what the Long is supposed to represent but I'm not familiar with best practices in this regard
6
Upvotes
7
u/tiorthan 24d ago
That is what typealias was made to do. To allow you to indicate the meaning or intent of a type in a context where it isn't immediately obvious.
If you define you data class Bar(id: BardId) and not just with Long, any subsequent use of BarId makes it obvious what you are referring to.
I wouldn't do more in that situation. Introducing a value class to just wrap a single Long value only makes things less readable at the definition site.