Say you want to create a class to represent a point in two-dimensional space. If you come from a certain background, your immediate instinct may be to create some private properties and getter/setter methods:
This feels productive. You're writing code, after all! If you're feeling especially diligent, you might even write JSDoc comments for each of these methods. Your editor might even include shortcuts to write all these getters and setters for you.
Here's some code that uses that class:
The downside is that this is a lot of boilerplate code that doesn't do very much. Why write the getters and setters rather than this?
At least in Java, the answer is that getters and setters encapsulate the implementation of the class and give it much greater flexibility to evolve in the future.
For example, what if you realize that for some reason it's much better to use polar coordinates internally? With the getters and setters, it's no trouble to reimplement the old API using the new internal representation:
Users of the
Point2D class will be completely oblivious to this internal change. The code above works without change. Contrast this with
DirectPoint2D. You can't make an analogous change to this version because the internals are exposed. You can't get rid of the
y properties without making a breaking change to the API. You're stuck.
Here's how you'd migrate
DirectPoint2D to a polar coordinates representation using getter and setter methods:
Usage looks exactly as it did before:
What were direct property accesses before have become method calls. But the syntax is character-for-character identical, so the caller need not be aware that anything has changed. The public properties are no longer a constraint on your class design.
One cautionary note: don't go too crazy with
set. When you read code like
pt.x = 3, you expect that this will do something like setting the
x property of
3. Of course, with a
set method, it could do anything. It could set
y instead, or it could even issue a network request. But to avoid confusion and surprise, it's best if paired
set methods get and set the same thing, at least conceptually.
Here's a complete playground link for the last example if you want to give it a try.