Mark Richardson wrote:So, as a student, I've always learned that we make our instance variables private because some "rogue class" might inadvertent (or nefarious) changes to your class.
That is, every class should have responsibility over it's own variables. I get that.
But the getter and setters methods are public. They can presumably just be called by another class, no?
I suggest you read articles related to "getters and setter break encapsulation
" to get a better grasp of some of the motivations for keeping the scope of things (methods, fields, classes) as small as possible. Essentially, the wider the scope of something, the less control you have over how that something will be used. Therefore, it is in your best interest to put up "guardrails" around things that are widely accessible to mitigate the risk of them being used in ways that you didn't anticipate that could violate some the assumptions made when you designed those program elements.
This is another reason I like to adhere to Kent Beck's 4 rules of simple design
- I usually think of rule #4 as "keep things small," including scope.