Please read before referring me to post like those, I'm asking about a very specific case :
Why use getters and setters/accessors?
What is the use of encapsulation when I'm able to change the property values with setter methods?
Let's take for example a JAVA JEE application, those are basically websites. Your bank website is probably a JAVA JEE application.
On those we are a generally a team of 10-15 devellopers, nobody except us uses our code, nobody imports our package and uses our objects.
The backend usually have DAOs to access the database and Value objects to transfert data to the presentation layer.
The VOs are perfect copies of the entities, example :
public class UserVo {
private String name;
private String email;
private String password;
public UserVo(String name, String email, String password) {
this.name = name;
this.email = email;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
So you get an entity, you convert it to a VO for "layer separation" (which I don't see the point either because if you change an entity you change the VO then you change the code in the presentation layer that use that VO but whatever)
First of all a VO is suposed to be immutable, but on all the projects that I've worked for they are mutable.
My question is : is there an advantage here to have getters/setters ?
There is no logic inside, some will tell you that a setter can do some validation but this is not useful because all our code is unit-tested so there is no need to validate anything in the setters.
Since all properties have a get/set is is exactly the same as if they were public.
When the implementation changes the getter/setter are changed too and all the code that uses the old getter/setter is refactored/modified.
I often get told about data hiding, that the user won't be able to access a private property : what? the user is browsing a website.
So, why is everybody so attached to those getter/setters? if I uses public properties instead of those getter / setters I will be ridiculed, everyone saying things like "don't you know about encapsulation", "you should NEVER have public fields"
My take on it is that it's useless, but everybody does it because it is how it has always been done and nobody ever ask questions about it.
Am I wrong?
Thanks.