DEV Community

Discussion on: How you can reduce usage of getter methods in your code

arj profile image
arj • Edited on

A getter is only really necessary to guarantee an invariant on the internal field.
If there is no invariant for the internal field, then it is not necessary to write a getter.

If one assumes that there might be an invariant that is to be added later, then it might be better to start with a getter. Otherwise, it doesn't matter.

Furthermore, getters are for accessing the state of an instance. If you need to sort or get a representation, create a method for that. There often exists a convention in the languages (e.g. compareTo or operator==).