Setters are Evil?

  • In Java, it is common to have a setter method
  • public void setSomething(String something) {
      this.something = something;
    }
  • public setters let any code change your object (or class's data)
  • It becomes hard to know where data is changing and when
  • Who is responsible for calling the setter? Who is allowed?
  • Setters can almost always be avoided (JPA is a good exception)

7 / 22