Reflection based libraries can be updated. And once they are, having a reliable list of members and accessors reflected directly as part of the Class API will be way better than relying on convention. I expect libraries like Jackson will welcome this improvement rather than complain.
Plus, the prevalance of getters and setters in general is a blight on the Java ecosystem. Beans are encapsulation-destroying garbage in OOP. Records thankfully say that if you want a simple data carrier then you have to be principled about it and follow some basic rules.
I've heard the argument before that beans break encapsulation. I don't personally buy it. But I'm also not even talking about full JavaBeans style classes with getters and setters — my preference for years now has been to use immutable classes for data, with only getters and a factory method or builder. When I say I use getFoo convention, that's what I'm envisioning.
I do agree that many libraries will update to accommodate. Some of my concerns are:
Not all libraries will, especially not right away, since it's a preview feature. Some libraries will have to wait for a new version of the spec they implement. Some just won't because they're old and in maintenance mode, or need to keep building against JDK8 (or JDK6).
Whether my class is a record class or not must be part of my library's public API. It would be a breaking change to switch it after the fact. This is good in many ways, because it makes a strong semantic statement about my class to my API consumers, but it is something I have to commit to.
If I do try to change from a record to a full class, I will be forced to rename all my accessors to getFoo-style if I still need to play nice with reflection-based libraries.
I can't easily put the class behind an interface. Suppose I have an interface with a getter method. I can't use that interface for both records and non-records because they are forced to use different naming conventions. (I could get around this by creating a getFoo() method on my record that delegates to foo() but that's terrible.)
I'm just worried it going to be like how we have both arrays and collections. They both do similar things but their APIs are completely incompatible. So we just wind up having to convert one to the other at code boundaries, and having two separate but similar sets of tools.
I've heard the argument before that beans break encapsulation. I don't personally buy it.
Allowing anyone to come along and unilaterally, arbitratily modify and read your internal state is the exact opposite of encapsulation.
Not all libraries will, especially not right away, since it's a preview feature. Some libraries will have to wait for a new version of the spec they implement. Some just won't because they're old and in maintenance mode, or need to keep building against JDK8 (or JDK6).
I mean you're probably going to have to find replacements for libraries like that eventually anyway. I'm sure people made the same argument when generics were introduced. The only way to avoid that class of complaints is to just not evolve the language at all.
Whether my class is a record class or not must be part of my library's public API. It would be a breaking change to switch it after the fact. This is good in many ways, because it makes a strong semantic statement about my class to my API consumers, but it is something I have to commit to.
Sure that's a trade-off you'll have to consider. Just like choosing between a class and an interface, for example. That's just one of the things about designing an API, you have to pick the right abstractions to permit API evolution as best you can predict it, I don't see this as a huge extra burden on the design personally but I do see your point.
If I do try to change from a record to a full class, I will be forced to rename all my accessors to getFoo-style if I still need to play nice with reflection-based libraries.
Most libraries allow for using annotations rather than just method naming conventions right? I would never trust such a mechanism in the first place, sorry if you have to deal with such crappy reflection libraries.
I can't easily put the class behind an interface. Suppose I have an interface with a getter method. I can't use that interface for both records and non-records because they are forced to use different naming conventions. (I could get around this by creating a getFoo() method on my record that delegates to foo() but that's terrible.)
Someone working on a project which has adapted foo instead of getFoo as their naming conventions would have the exact opposite opinion. That seems like just a matter of taste to me.
I'm just worried it going to be like how we have both arrays and collections. They both do similar things but their APIs are completely incompatible. So we just wind up having to convert one to the other at code boundaries, and having two separate but similar sets of tools.
APIs should almost always use collections, and that will be even more true when we have generic specialization and e.g. List<double>. But records are classes and the vast majority of APIs won't care about the difference, I don't see why there would be any conversions needing to take place at API boundaries except in extreme cases when interacting with reflection-based libraries, as we've already talked about.
Constraints aren't a solution, you're still allowing your state to be managed by someone else. You give up control of the behaviour which governs that state. Methods which modify state should contain descriptive verbs describing their behaviour, not just "set".
What if an operation needs to update many fields at once? Having to call many "sets" one at a time means for a period your object will be in an inconsistent state, and so per-field constraints can't help you.
And yes you can just not have getters, and that's my point, in most cases you shouldn't. State should not be exposed unless necessary. But if you are managing that state externally with setters ofc you will probably need getters of everything too...
14
u/eliasv Jan 17 '20 edited Jan 17 '20
Reflection based libraries can be updated. And once they are, having a reliable list of members and accessors reflected directly as part of the Class API will be way better than relying on convention. I expect libraries like Jackson will welcome this improvement rather than complain.
Plus, the prevalance of getters and setters in general is a blight on the Java ecosystem. Beans are encapsulation-destroying garbage in OOP. Records thankfully say that if you want a simple data carrier then you have to be principled about it and follow some basic rules.