This misses the main problem with get/set methods.
In OO othodoxy, you aren't supposed to make state public, because it violates encapsulation. If you want to modify your Point object to use polar coordinates instead of cartesian, then if you expose your cartesian state (x and y), then all the code that relies on x and y is now broken.
So what getX() and setX() do is allow your x and y fields to be encapsulated. Problem solved! Except that once you change to polar coordinates you have your choice of problems:
1) Find all the getX() and getY() uses, and rewrite the affected code to use getR() and getTheta(), since you are now using polar coordinates. In other words, you have violated encapsulation by having getX() and getY() methods, and your get/set methodology hasn't achieved anything.
2) Re-implement getX() and getY() in terms of polar coordinates. That's doable. But do you also now have getR() and getTheta()? Why?
Having getFoo() and setFoo() methods for each field foo is this dumb idea that began, I think, with JavaBeans. It has nothing to do with OO as I understand it. The idea behind OO encapsulation is to separate your object's behavior (the methods) from private state. You can change state at will as long as you maintain behavior. Mechanically adding behavior (get/set methods) to track your private state has always been a stupid idea that has nothing to do with OO except to violate encapsulation.
> Mechanically adding behavior (get/set methods) to track your private state has always been a stupid idea that has nothing to do with OO except to violate encapsulation.
What should be private state is a matter of debate.
If you have a point, it's not too ridiculous to ask "where is it?" Cartesian coordinates may or may not be how the Point object represents where it is, and that internal detail should be hidden from callers.
> But do you also now have getR() and getTheta()? Why?
It seems like a Point class should hold its internally state in the "best" implementation for how it is usually used, and provide its state out to callers in all of the convenient forms that would be used more than occasionally.
Of course, one should avoid purely exposing internal details. And sometimes you might encapsulate more to encourage callers to use the pointA.getDistanceAndBearing(pointB) call. (But I don't think this makes sense for points; knowing their location is too fundamental).
They enable your option 2 and allow the implementation to change behind the public API. Should theta/R have getters is entirely up to your API decisions.
The point of getters and setters is they're the bare minimum of encapsulation. The advice is to ensure even reading a field should be abstracted so implementation details do not cause large reactors of downstream code.
How this is conflated with 'every field should have getters and setters' like it is in this discussion I do not know but that is not the intention.
> Should theta/R have getters is entirely up to your API decisions
But it often isn't. The blog post starts out talking about how that field -> getter/setter pattern is very widespread, and tools (JavaBeans tools for example), make that pattern unavoidable sometimes. Yes, it should be an API decision, but for a variety of reasons -- tools, patterns, coding standards, cargo cult programming -- it isn't.
3) implement getX() and getY() alongside getR() and getTheta() and allow user code to decide which to use. Double the functionality without having to expose internal state.
4) implement a PolarPoint class with a way to convert between that and Point. This is more applicable if the concepts are not orthogonal but also not as strongly correlated as coordinate systems.
The two ways of handling you describe are not how I would actually handle any of this in real situations.
Getters and setters meanwhile allow you to do things like recalculate internal state whenever you call setX() or setR() to amortize computational costs or do any other kind of change tracking without having to rely on some more language-specific feature to do so. Plus getters and setters work across network boundaries unlike modifying state directly.
Point 2) feels very weak. You could easily have getR and getTheta, but critically you could also always have had them - if they make sense as APIs, and your state is encapsulated, you can expose them at any time.
The benefit of having getters is that API consumers don’t privilege public state above any other API component, because they don’t know what’s actually the real state and what’s just under contract.
Given your Coord example, can you come up with a better OO version than the article's advice of "can I make it immutable"?
The immutable version would let you have all four fields. A setter on any of the 4 items (should you desire it) will return a new object whose four fields remain consistent with one another.
...and how precise do the switches between coordinate systems have to be? depending on your format for floats, you will land on areas that are only expressible with perfect precision in one or the other coordinate system. I can imagine a fixed point that moves around by being repeatedly queried in one or the other system.
Point object could be either, since the coordinate systems are truly interchangeable, e.g. the class could just store everything as Cartesian internally and convert to/from polar as needed. In a class with more mutually exclusive properties I can see how this wouldn’t work so well.
The point is that mechanically maintaining get/set methods for each field is completely at odds with encapsulation. To continue the example, sure, go ahead, have any or all of get/setX, get/setY, get/setR, get/setTheta if they make sense for your application. But writing them just because you've created a field, and the coding standards say they should be there is dumb. The coding standards that say this (like JavaBeans) may have their reasons, but OO ain't it.
What terrible advice. Your public variables are so much easier to refactor if/when your data changes when you have simple accessors methods around them. Having accessors for data have saved my ass multiple times when I've had to do things such as a) migrate the data transparently b) remove data and use computation instead c) debug reads or writes of data.
The last point, sure you can have a breakpoint on a memory address but if you have a getter/setter method its trivial to put a breakpoint in there, but I bet that if you have to set a memory brekapoint you have to waste time googling how do that (at least with GDB)
Raw data POD type structs are useful but only when the scope is limited.
The article doesn't suggest making the properties public, it makes valid suggestions IMHO (though I don't agree with all). I would recommend reading the article.
I feel like this is one of the things python got right. You can start off with plain old data:
def Foo:
def __init__(self, foo):
self.foo=foo
Use it in all the ways you like. If then later you discover a need for a getter/setter type setup (because you're now e.g. storing foo in a different format internally) you can switch without any of the code depending on the Foo class needing to change:
I'll grant that the syntax is slightly wonky, but its really nice to not have to think about getter/setters when initially designing a class. You can add them when and where you need them without breaking other code.
I don't think this kind of article is that helpful.
There are aspects of a valid point in there, but it's buried and confused.
That builder pattern makes me sick to my stomach.
Also, it starts with claiming state is part of the problem with getters/setters, but does not mention it again. (It seems to be conflating mutability and state, which, while often related in practice, are not necessarily that tightly coupled -- an immutable thing can nevertheless be state, and you can have mutability without introducing state. State is really about things that live beyond the immediate context/scope. So really nothing to do with getter/setter.)
I always thought blank / barebones getters and setters were kind of dumb and genuinely pointless, some obscure ceremonial thing that. C# added syntactic sugar for them.
If you are not validating any of the input you might as well just have a public variable.
I’ve written a lot of C# and have heard the proclaimed reasons. I’ve stopped doing this over time. You almost never need the interface and when you do, I haven’t found it hard to add.
Maybe you do this by default if you are developing an external API for versioning reasons but that’s such a niche use case.
When you change from fields to properties with getters and setters you break binary compatibility with previous versions, which is very bad for libraries.
How much legacy code have you maintained/added features to vs. writing new code?
I find that getters and setters seem like pointless overhead when I have an idea that I want to test empirically as soon as possible, but if I don't, I end up with a lot of regret years later when I have to track down all the references and update them instead of just changing code in the one place.
I'm not a fan of lazy loading, but I know a lot of people are, and using the getter/setter pattern also enables fairly straightforward ways to lazy-load properties from persistent storage (or add that capability down the road if it becomes advantageous).
Also lets you maintain your invariants. It's particularly useful for cache invalidation. Like if your object exposes a hash of all its public properties, you can invalidate the cached hash when a prop setter is called with a new value.
I was unable to put a synchronized onto the field directly:
error: modifier synchronized not allowed here
public synchronized final Object field;
But when I locked it in code:
synchronized (field) {
while(true) {
..
I was still able to access the synchronized field directly without triggering a deadlock - whereas a getter() could be synchronized and trigger a deadlock.
In any case, the article already made a strong points for immutability so you don't need to mess around with deadlocking crap.
They're saying multiple things can synchronize on the same public field. Same reason you don't want to use this.
Although I suppose they could mean the concept and not the keyword. A field might not be thread safe and there's no way for an object to guarantee thread safe access of to a field.
Deadlocking was raised as the objection to using public instead of getter. But in regard to deadlocking, using public is strictly better than a getter.
You have to take a lock in order to deadlock in the public version. In the getter version, you can take a lock without realising it and then deadlock.
To me that's just a matter of having standards that have clear requirements of what's okay to put in a getter - imho, refreshing invalidated caches (as long as they're lightweight and not like network communication) is perfectly appropriate. Anything with a side-effect that can be observed from outside of the object (creating the case where you get code just hitting setters and getters to cause side effects) is right out.
I mean, Lazy<T> is a great example of use of getters.
I never understood this argument for statically typed languages like Java.
> You can add validation for all setters, and abstract it as an interface later if needed.
Sure, but why not just postpone the introduction of setters until you actually need them. 90% of time you don't, in which case we just saved a ton of boilerplate and ceremony. The 10% of time that you do, converting public variables to a setter method is about 2 IDE clicks these days.
The only argument I can think of is if you are exposing a public library API, in which case you need to worry about backwards API compatibility.
Generating getters is actually 2 IDE clicks that will protect you down the road.
Trying to refactor fields that made it into a public API is a nightmare. If you're shipping code people use, you won't get to refactor their projects from your upstream library.
Also, no one is suggesting everything needs a setter. Where is this coming from?
If I could I would make this two IDE clicks behind a big mean warning. In my experience people tends to autopilot it without thinking about what that entails.
This article sort of reads like they think everything should be immutable, which feels kind of dogmatic. Using `with` for everything by default seems like overkill. But in C#, lately I have been using `readonly and `init` wherever vars should not be changed after initialization, which is most of them tbh. For small, data-only objects that can easily be immutable and get passed around a lot, this makes sense to me as a way to avoid the kind of bugs they are talking about.
I’m on board with the With and Builder patterns where it makes sense, but the final suggestion to make the fields public is just bad advice.
The author recognizes that mutable state gives you a lot to worry about, which I agree with. But public mutable fields make it even worse. Getters and setters at least minimize the surface area and give you some control over mutability.
There are many reasons why this is not the common practice, people have learned these lessons before.
I find immutable state to be useful when appropriate. But sometimes you really do want mutability, and it is a feature. Performance of updating player.coordinates, ability to create temporary but potentially complex structures inside the function, lack of need to create a new immutable copy of a bank account’s ledger to add another transaction, all good reasons to allow mutability.
It seems that there are some people who really just think of everything as a state machine and want state transitions to happen only in very controlled and specific ways. And other people seem to focus more on how to organize frequently evolutions of mutable state in a way that makes it easy to reason about. I’m not sure if there is some sort of commonality to these approaches. Different parts of the industry maybe? Different exposure to Haskell?
> lack of need to create a new immutable copy of a bank account’s ledger to add another transaction, all good reasons to allow mutability.
The immutability kool-aid was already drunk by the banking industry 5000 years ago. That's why you're appending transactions on a ledger instead of mutating bank balances (behind getters and setters, so it's "encapsulated" /s).
Sure but your account balance does mutate and so does the length of the ledger. (I know this was sarcasm, but I do think those are important considerations).
This pattern is really only used with @Entity’s. Things that actually are mutable and rightfully need to be. With @Entity’s setters are exactly what you want because it allows you to add validation, and check error states before persisting.
Arguing immutability for the sake of immutability is considered harmful imo. Only make things immutable when they actually are immutable or when concurrency is at play (which it probably isn’t, 99% of all code is single threaded).
Often, imo, this depends on where you want to put the validation complexity. You can move validation to the front end and "prevent constructing bad states" through a combination of good UI and front end data validation, i.e. validate all the pieces of an entity to make sure the eventual Entity will be valid. But the opposite trade off would be to keep your front end simple, and have the data represent anything that is possible in the UI, and then you can validate anywhere in your stack that you want or where it makes sense for any engineering reason. You can still validate the UI models on the front end before sending them over the wire. You can validate at the endpoint and return HTTP codes. You can run logic in your database to massage/correct/fix the data any way you want. You could even throw things into a queue for post-processing later on, or send to another server for processing, whatever you want.
In theory it sounds great to say "just prevent construction of bad states," but this is a complex problem in professional software that involves coordination between programmers, designers, and product managers to get the UI features into the right place where preventing the construction of bad states doesn't lead to a frustrating UI experience for users. It's often far simpler, more pragmatic, and less complex in terms of coupling in both software and humans to just accept whatever state the UI gives you and figure things out somewhere else where you aren't bothering your users so much.
> Often, imo, this depends on where you want to put the validation complexity. You can move validation to the front end
This has nothing to do with the frontend/backend split since a server will receive arbitrary requests, not just the ones you hope you programmed the frontend to send.
> In theory it sounds great to say "just prevent construction of bad states," but this is a complex problem in professional software
Defeatism.
If you have a User in your domain, and your domain dictates that Users have a UserId, simply do not allow construction of a User without a UserId. What's a UserId? Is it a String, or is it a UUID? If you think the King James Bible translated into Chinese makes for a good UserId, then a String is a perfectly good representation for that. Otherwise pick something more suitable.
I mean I was just trying to have a conversation you don't have to try and roast me with quote-quips and absurdist comparisons... What is happening to this platform these days?
Any problem can be swept away with "you don't know what you're talking about", "you'll come around to my way of thinking when you're older", "X has nothing to do with Y", etc.
I think that makes a bad assumption that _you are constructing bad states_ when that isn’t true. You most likely don’t have all the data you need at construction time and the state of the object is still valid. Some data can be null, but later when it is set you want to validate that the data is correct.
For instance when making a person or user you most likely only have their email when you initially create them and then it’s the users job to complete the rest of the data at a later time. Some might want to fill in their age or some might want to fill in their sex. You may never get this data because it’s optional, but when you set it you’ll want to validate it.
And as always in programming _it depends_. It depends on your workflow, requirements, and domain. Being dogmatic about anything is just going to result in worse code.
I personally don’t really like builders. They work in some situations. But if I’m pulling the record from the DB and I only need to set some data on it that is overkill and more complex than need be. The object is already constructed in a valid state.
> The object is already constructed in a valid state.
But that's the entire value proposition of do-not-allow-construction-of-invalid-state!
If a User is actually a User, then you can say "the object is already constructed in a valid state".
If you decide to be 'less dogmatic' and instead construct Users-which-may-or-may-not-be-users-just-throw-an-ex-if-theyre-no-good, then you cannot say that the object is already constructed in a valid state.
That's not the situation described above where data is partial.
In your case I would copy rather than mutate, unless I'm actually getting noticably higher profits from lower memory usage during the time between getting the object and GC. This hasn't happened to me yet.
Maybe I'm not getting what you're saying but the data is partial? In the situation above we have a valid record created by someone else. This is pseudocode obviously, but I genuinely don't know what copying or building or immutability is going to buy me here other than being more complex. There is no worrying about the record being in an invalid state because it could have never gotten to the database in an invalid state.
class User {
Long? id;
String email;
String? name;
Integer? age;
Sex? sex;
Avatar? avatar;
User(String email) {
this.email = email;
}
}
record ProfileVm(String? name, Integer? age, Sex? sex, Avatar? avatar) {}
// some ui controller
var user = findById(id)
if (user == null) {
return notFound();
}
var vm = ProfileVm.fromJson(request.json());
user.setName(vm.name());
user.setAge(vm.age());
user.setSex(vm.sex());
user.setAvatar(vm.avatar());
user.save();
return ok();
I agree with the article for the most part (except the last snippet where the author reverted to bare, mutable fields), but the point was very badly argued.
I think we really need to consider that sometimes you want a place to hold together data, and some other times you are encoding some behavioral trait, and prior to records Java did not have a way of distinguishing between them.
My rule of thumb is to avoid exposing data in behavioral classes, and prefer immutable patterns for data holders. But sometimes it's not possible to not have setters (e.g. for builders and for JPA entities).
This is weird. I use getter/setter regularly for DynamoDB backed DTOs. This helps catch setters at the source of a problem before it gets persisted. Also the exceptions will help pinpoint code paths where these things can happen.
Some decades ago as a child I read a Bjarne Stroustrup interview where he said you use those things to enforce invariants and if you don’t you keep access simple. In the intervening decades, I’ve found that when I have a new invariant I often need to confirm the call sites are compliant anyway as he says. So I just use that, keeping accessors only where required or where interface compliance syntactically requires it.
> Notice the subtle difference? Dropping those get prefixes for the access methods has significant benefits. Let's think about it: What value was added by the prefix? Correct, none!
Methods and functions do something rather than are something so naming them as the action they complete seems much better form to me. 3 characters is a fair price for clarity.
> Drop the getters and setters. They don't add any value
They add the ability for the underlying object to change without breaking compatibility. This is particularly important on shared libraries where you are not going to be able to refactor every single call sight.
For instance we have an object in our model representing a building, we've gone through *three* different structures of what a building is, but we've managed to make those changes while maintaining compatibility through leaving deprecated methods that translate the calls.
Depends on the language, some have runtime optimizations to make copies fast (usually FPs with purely immutable data). Not sure if that’s the case in something like Java.
please for the love of Anders Hejlsberg just add properties to the language already we've had JavaBeans for like a century now we just need properties like any sane language would have added please bro i promise bro i swear just add properties to the langauge so the expression is a valid right hand side and left hand side look at javabeans bro i promise it'll be ok it's not bad to admit that anders was right on this one it'll be ok bro just add properties please bro no more builders just lets add properties bro
To me, much of this just gives away a lack of experience.
Have you never hit a bug because an object mutated unexpectedly? Please do keep this in mind when you do, and spend hours of your life trying to hunt it down.
When you've done this 8 or 9 times after wasting days of your life debugging, you become enlightened to the joy of immutability. It's worth the tiny memory overhead.
Beyond that, get* is everywhere in of corporate OO especially in more classical OO languages. I'd argue the right way to do things as well, naming methods as actions, but that's a separate issue.
This was born from needs in the bog where standard business software lives.
Sure, you can have naked attributes, but you'll be sorry once they aren't enough and you now have two parallel ideologies seeping through your software, increasing the risk of bugs. Concurrency and collection attributes are the common reasons you'll want getters, possibly also setters.
In OO othodoxy, you aren't supposed to make state public, because it violates encapsulation. If you want to modify your Point object to use polar coordinates instead of cartesian, then if you expose your cartesian state (x and y), then all the code that relies on x and y is now broken.
So what getX() and setX() do is allow your x and y fields to be encapsulated. Problem solved! Except that once you change to polar coordinates you have your choice of problems:
1) Find all the getX() and getY() uses, and rewrite the affected code to use getR() and getTheta(), since you are now using polar coordinates. In other words, you have violated encapsulation by having getX() and getY() methods, and your get/set methodology hasn't achieved anything.
2) Re-implement getX() and getY() in terms of polar coordinates. That's doable. But do you also now have getR() and getTheta()? Why?
Having getFoo() and setFoo() methods for each field foo is this dumb idea that began, I think, with JavaBeans. It has nothing to do with OO as I understand it. The idea behind OO encapsulation is to separate your object's behavior (the methods) from private state. You can change state at will as long as you maintain behavior. Mechanically adding behavior (get/set methods) to track your private state has always been a stupid idea that has nothing to do with OO except to violate encapsulation.
reply