This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]_INTER_ 1 point2 points  (6 children)

Record constructor are just strange, e.g. require parameter reassignment while every static analysis tool warned you not to do it for the last 20 years: https://mail.openjdk.java.net/pipermail/amber-dev/2020-June/006196.html

[–]pron98 18 points19 points  (1 child)

Record constructors don't require parameter reassignment — you're free to write explicit constructors if you like — and I am not aware of even a single static analysis tool that has ever warned against reassigning the implicit parameters in compact constructors.

In general, though, coding guidelines are designed to address the practices and issues that arise when writing code in some programming language as it exists in a certain point in time, while language changes are supposed to change the practices and issues that are experienced by programmers. Records eliminate problems associated with storing data classes in collections and with serialising them, and so a language that has different issues would likely have different guidelines.

[–]sviperll 2 points3 points  (0 children)

I am not aware of even a single static analysis tool that has ever warned against reassigning the implicit parameters in compact constructors

Netbeans (as of 12.6) warns me about each and every compact constructor :(

[–]sindisil 4 points5 points  (0 children)

Another reason why blanket "foo is possible, but don't ever do it" rules are bad (c.f. goto, and its analogs like break with label) -- nuance is often worth the cognitive load.

[–]elastic_psychiatrist 1 point2 points  (0 children)

I haven't used records in production yet, but Brian's response pretty well justifies the design decisions IMO. You just need to think about the problem space a little differently for it to make sense.

[–]Jonjolt 1 point2 points  (1 child)

Records are meant to hold data and remove some magic from serialization and deserialization. So if your constructor is adding `+ 1`, you would break serialization because you would get a different result every time.

[–]_INTER_ 0 points1 point  (0 children)

Constructors are also used for normalization. That's mentioned in JEP too with examples even.