To add to other answers, these are from the book Java to Kotlin, chapter 11 Methods to Properties:
Example 1
Suppose we want to add age to this class:
data class Person(val dateOfBirth: LocalDate)
We can compute age easily (ignoring time zones) from the dateOfBirth
property. But this doesn’t depend only on that property; it also depends on when we call it.
Unlikely though it is, fred.age == fred.age
can return false
.
Age is an action; its result depends on when it is called. Properties
should be calculations, timeless and dependent only on their inputs, in this case the dateOfBirth
property.
Hence, age()
should be a function, not a property:
data class Person(val dateOfBirth: LocalDate) {
fun age() = Period.between(dateOfBirth, LocalDate.now()).years
}
Example 2
What if we want a cryptographic hash of all the other properties of the object?
This is a calculation (for immutable objects), but if it is expensive to compute, it should be a method hash()
not a property hash
. We might even want to hint at the cost of the method in its name:
data class PersonWithProperties(
val givenName: String,
val familyName: String,
val dateOfBirth: LocalDate
) {
fun computeHash(): ByteArray =
someSlowHashOf(givenName, familyName, dateOfBirth.toString())
}