Search code examples
javagetter-settergetteraccess-control

Is there a rule that states that complex access methods are evil?


Adding more functionality to access methods (getters/setters) than simply this.x = x; or return x; is usually considered bad style.

I am looking for a written source which I can reference in a technical paper. The JavaBeans specification does not contain a statement about the content of access methods. Neither does the Java Language Specification.

Is there any official Oracle document or something similar with high significance that explicitly states that? Or is just an unwritten law?

EDIT: Seems like I was wrong about "usually considered bad style." I did not want to start a discussion about an opinion based topic. For me, my answer is that I cannot assume it is considered bad style. Thanks for your input!


Solution

  • This is, by nature, an opinionated view, but I was under the opposite impression. If indeed getter/setter were limited to simple return/assignment, then they would serve no added value then the direct approach.

    It is quite common and expected of setter to have some validation.

    also, getter method can be created for calculated fields (like getSum(), getAvg(), etc) and in this case, they may include a simple or complex calculations.