Looking and digging through my code, I noticed that we usually use the @Setter
from Lombok thoroughly.
It made me wonder about the usage of @Setter
.
Indeed, is it more interesting to use @Setter
than simply making the field accessible through its visibility?
From what I understand, setters are preferable in one of those cases (thanks to Why use getters and setters/accessors?):
- you want your object to react to a field being set
- you want to lazily compute the value of a field
- you make want to mock (although I deeply think testability should not lead your code design)
- class may be inherited and setters overridden
But there are some object which does not match any of those cases.
In code terms, is this:
@Getter
@Setter
public class Person{
private String firstName;
private String lastName;
}
more preferable (in terms of maintainability and readability) than this:
public class Person{
public String firstName;
public String lastName;
}
As a side note, I would like to add that I find
person.firstName = "Vincent"
more readable thanperson.setFirstName("Vincent")
.