I have a simple Java model Class called Person which have a constructor that receives a JSONObject. In that constructor I get the values from the JSONObject and put in my instance variables. This is just a simple question but which way is best?
public Person(JSONObject personJsonObject) {
// Without setter
this.name = personJsonObject.getString("name");
// With setter passing String
setName(personJsonObject.getString("name"));
// With setter passing JSONObject
setName(personJsonObject);
}
And when my setter isn't just a simple attribution but a evaluation, which one should I use?
public Person(JSONObject personJsonObject) {
// Without setter
this.fullName = personJsonObject.getString("name") + personJsonObject.getString("lastName");
// With setter passing String
setFullName(personJsonObject.getString("name"), personJsonObject.getString("lastName"));
// With setter passing JSONObject
setFullName(personJsonObject);
// Or should I put this logic in the getFullName method? The caveat is if there is a huge calculation in the getter method. It wouldn't be efficient.
}
Thanks for your answer.
setFullName(personJsonObject);
If you are going to evaluate, accept only what you need, unless it ruins the 'legibility' of the code. \$\endgroup\$