Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

12
  • 20
    "[G]etters and setters are not necessarily simple access" -- I think that's a key point. If your code is accessing a field by name, you can't change the behaviour later. If you're using obj.get(), you can. Commented Oct 5, 2017 at 14:07
  • 4
    @jrh: I've never heard it called a bad practice in Java, and it's pretty common. Commented Oct 5, 2017 at 14:52
  • 2
    @MichaelBorgwardt Interesting; a bit off topic but I've always wondered why Microsoft recommended against doing that for C#. I guess those guidelines imply that in C# the only thing setters can be used for is converting a value given to some other value used internally, in a way that cannot fail or have an invalid value (e.g., having a PositionInches property and a PositionCentimeters property where it automatically converts internally to mm)? Not a great example but it's the best I can come up with at the moment. Commented Oct 5, 2017 at 15:05
  • 2
    @jrh that source says to not do it for getters, as opposed to setters. Commented Oct 5, 2017 at 19:44
  • 3
    @Gangnus and you really see that someone is hiding some logic inside getter/setter? We are so used to it that getFieldName() become an automatic contract for us. We will not expect some complex behavior behind that. In most cases it's just straight breaking of encapsulation. Commented Oct 6, 2017 at 16:10