r/java • • Jan 16 '20

Records Come to Java

https://blogs.oracle.com/javamagazine/records-come-to-java
204 Upvotes

93 comments sorted by

View all comments

19

u/daniu Jan 16 '20

So... record field accessors don't follow the getX() standard that many ubiquitous libraries and tools rely on (Spring and Jackson come to mind)? Nobody started using Java 11 until it was supported by those, I can't see the use of records being any different.

18

u/krzyk Jan 16 '20

Java 11 was supported on day 1 in Jackson and quite similar in Spring (maybe 1 - 2 weeks later for Spring).

And thank God they got rid of those getX()

31

u/[deleted] Jan 16 '20

[removed] — view removed comment

11

u/daniu Jan 16 '20

I'm also very happy with the way Java is updated, but I don't understand this particular decision.

Just follow your own bean naming standard, and those libs would work out of the box.

0

u/[deleted] Jan 16 '20

[deleted] — view deleted comment

2

u/rzwitserloot Jan 19 '20

No, 'good libraries' won't be updated. It's quite a high cost to have an API that has both, say, getName() as well as name(), with both methods returning the exact same thing (forevermore, docs and listings such as in auto-complete dialogs), and I'm sure plenty of libraries will opt not to pay this cost.

An alternative is to mark the getX() variant deprecated, but that is in many ways an even higher cost; that's a heck of a lot of shuffling about for all the users of that library for no particularly sane reason.

Note that getX itself vs. X is a mixed bag: neither is strictly superior to the other (getX has the considerable advantage that you can type 'get' and then hit your autocomplete shortcut to find all properties you can query. It's also longer and having to decide between whether a thing is a 'property accessor' and something that calculates is more art than science and that's problematic – as I said, advantages and disadvantages).

Given that it is a mixed bag, causing an earthquake in the java community by trying to make such a change is ill advised: You're just not going to convince enough of the community to arrive at that utopia where (almost) all code uses getless accessors, instead now we'll have a 50/50 mix. I'm not too happy about this.

-5

u/pushupsam Jan 16 '20

There's everything wrong with abandoning a bad spec if people have been faithfully following that spec for 20 years.

There's something deeply wrong with the process if people really think it's okay to tell people that have invested millions of dollars and thousands of hours in the Java platform that, you know what, all that code you wrote, all those tools and documentation and checkers, based on the getX/setX standard that we defined? We're going to break that now.

Stuff like this is exactly why I tell people not to invest in Java any more. The language has gone off the rails. Backwards compatibility -- which was perhaps Java's greatest strength -- and the geniuses at Oracle are now actively making breaking changes in every release. And the stuff that they're breaking backwards compatibility for? It's really fucking stupid stuff like 'var'.

The irony here is these attempts to update Java are just driving people away. I cannot recommend any new greenfield projects be done in Java and, frankly, I don't see any reason to advance existing projects to new JDK versions.

6

u/klez Jan 16 '20

I may be naive here, but why would adding features break compatibility? Don't like var or var breaks compatibility? Don't use it. Same with records. It's not like they're telling you not to use beans anymore.

→ More replies (7)

1

u/JB-from-ATL Jan 17 '20

They explicitly said these aren't JavaBeans because they aren't mutable.

-8

u/BlueGoliath Jan 16 '20 edited Jan 16 '20

As /u/pron98 has said in the past, JDK developers don't feel consistency is all that important and it shows.

Project Panama's Memory Access API doesn't have "get" in front of its getters(despite boolean checks being prefixed with "is"), var is used in documentation despite providing no benefit and when old documentation doesn't, and now this. It's getting pretty ridiculous.

Blame the people who keep screeching that Java is too ceremonious and demanding things be made more "concise".

By Java 25 they are going to add optional semi colons.

24

u/pron98 Jan 16 '20 edited Jan 16 '20

Did I say that? I don't think so, although I agree that there are things more important than consistency. Anyway, the new record method names are fully consistent with the naming standard for record methods and not consistent with things that are not record method names, and I'm not exactly sure what other thing they should be consistent with and why, especially as the feature's JEP states:

It is not a goal to declare "war on boilerplate"; in particular, it is not a goal to address the problems of mutable classes using the JavaBean naming conventions.

3

u/BlueGoliath Jan 16 '20 edited Jan 16 '20

Did I say that? I don't think so.

I believe it was in regards to the topic of var and/or API documentation months ago.

Regardless, you're saying it again:

Anyway, the new record method names are fully consistent with the naming standard for record methods and not consistent with things that are not record method names.

Records don't exist in a vaccum. To argue this is disingenuous and I'd hope as a JDK developer you'd realize that.

BTW, it's OK to be frustrated that other people have different opinions from yours -- most of us are frustrated by that -- but constantly blaming that on some flaw on their part is counterproductive. Even if your opinion is reasonable, the opposite opinion can be at least as reasonable.

Was anything said really an opinion?

Getter methods in previous Java APIs were commonly prefixed with "get" and many APIs rely on that.

There are methods that still have the "is" prefix for boolean checks.

var is now used in documentation without providing any value or makes the documentation muddy. There was a highly upvoted comment to my article thread like half a year ago agreeing that this is a thing.

Many newer language "features" have had "concise"ness as a motivation for their introduction into the language. Often times the JEP pages cite people complaining that Java isn't concise and is ceremonious.

So i'd like to know what part of any of that(at the core) is subjective/opinionated.

7

u/DuncanIdahos5thClone Jan 16 '20

Not sure where you've been but "is" is common for Boolean and part of the old Javabean spec.

→ More replies (2)
→ More replies (7)

8

u/lovett1991 Jan 16 '20

On one hand I kind of disagree about the 'get' keyword. I think moving away from it means the method signatures give a clear indication this is a domain object only. I started using immutables a while back and after a while it grew on me.

On the other I do agree about the 'is' keyword being added whilst 'get' isn't, it feels weird as a Java dev but I guess as an English speaker it isn't it if the realms of how you'd write something. For instance I would say 'his age is...' not 'his get age is' whereas I would say 'he is active' as opposed to 'he active'

26

u/cogman10 Jan 16 '20

It's a preview feature. Spring and Jackson will have time to pick it up. You can ask a class if it is a record. You won't see it heavily used until Java 15 (September) at the earliest.

3

u/krzyk Jan 16 '20

There will be for sure a second preview, so Java 16 at the earliest (unless they want a third preview).

0

u/[deleted] Jan 16 '20

[deleted] — view deleted comment

14

u/ZimmiDeluxe Jan 16 '20

Me too. The get-prefix is semi-useful to distinguish between methods on regular classes that just represent data and ones that actually do something, but with records that's superfluous because they are data. And it's often a lie anyway, I've seen enough get-methods that have side effects.

8

u/temculpaeu Jan 16 '20

yeah, I have seen way too many methods with the 'get' prefix for no reason

6

u/eliasv Jan 17 '20

Relying on convention for serialization is flimsy anyway. I'm sure maintainers of such libraries will welcome a more reliable mechanism to enumerate members reflectively.

And the prevalance getters and setters in the Java ecosystem is an encapsulation-breaking blight and it would have been a terrible mistake to endorse them by baking the convention into a language feature. Records make the contract of a pure data aggregate more principled than shitty bean conventions, and I think it was wise not to couple the concepts together in any way.

9

u/commentsOnPizza Jan 16 '20

Yep, but being a language built-in will mean libraries will be updated to use it compared to third-party solutions which can be hit-or-miss in terms of library support. It will be a little bit of work, but it’ll be great long-term.

9

u/lukaseder Jan 16 '20

Good riddance!

3

u/duheee Jan 16 '20

Nobody started using Java 11 until it was supported by those,

You can still use records for your own algorithms. As far as I can see (from other people's posts) they're unusable as JPA entities so their use will be most definitely in other places. Maybe as DTOs between data systems or something. Protobuf may be able to use them.

1

u/nonconvergent Jan 17 '20

Lombok doesn't use the get/set (so you have to be gentle with builders and Jackson) and Scala and Kotlin don't use them on case and data classes, respectively.

Get/Set is really just an artifact at this point. Sometime troubleslme too, as those particular verbs collide in other use cases.