Effective Kotlin: Item 16 — In public classes, use accessor methods, not public fields
As recommended by Joshua Bloch in Effective Java, you should access fields in public classes through getters and setters. Kotlin helps us achieve this by automatically generating these for any properties you define. By encapsulating the field, we have an opportunity to change the internals without breaking the external API.
For example perhaps initially your API exposes a
String, but internally you now want to represent this as an
Int, you could do the following: