Live data from Hacker News

Java's records, Lombok's data, and Kotlin's data classes

nipafx.dev

141–150 of 294 posts

Re: Java's records, Lombok's data, and Kotlin's data classes

#141
post #129
post #117

Earlier quoted context omitted.

It can be done in theory, but the JVM does nothing of the sort right now.

False. The JVM already does quite a good job with escape analysis, and record types just add extra semantic information to potentially further improve the situation.

What precisely of what I said is “false”?

Re: Java's records, Lombok's data, and Kotlin's data classes

#142

Earlier quoted context omitted.

You are supposed to create a new copy with some of the fields changed. Just like you do it with the java.time.* classes and others.

Instead of changing few bytes, now I have to copy hundreds of bytes around and add more stuff for GC to collect. Well, they have to obey Wirth's law, I guess.

This has been argued ad nauseam around the time Scala started gaining traction because scala's collections are grouped into immutable and mutable. The consensus at the time is that the GC overhead is well worth the ability to parallelize computation.

Re: Java's records, Lombok's data, and Kotlin's data classes

#143

Curious question — why do people insist so much on fields being private and there being getters and setters, even when all that getters do is return the field and all that setters do is set it? What kind of problem does this arrangement solve? Why not just use public fields?

This protects you from changes to the internal representation of state in the future. Direct field access totally blows away encapsulation.

Re: Java's records, Lombok's data, and Kotlin's data classes

#144
post #129
post #117

Earlier quoted context omitted.

It can be done in theory, but the JVM does nothing of the sort right now.

False. The JVM already does quite a good job with escape analysis, and record types just add extra semantic information to potentially further improve the situation.

Counterpoint: Java programs with memory consumption graphs that have decided sharktooth patterns, which is extremely common.

Re: Java's records, Lombok's data, and Kotlin's data classes

#145

Curious question — why do people insist so much on fields being private and there being getters and setters, even when all that getters do is return the field and all that setters do is set it? What kind of problem does this arrangement solve? Why not just use public fields?

Only argument I heard, not that I agree but, is future proofing by abstraction. What if a simple getter evolves into something more complex. Then You'd have to refactor all the places where it was using the fields.

I personally never had to do this so I'm not sure if this is a real benefit.

Re: Java's records, Lombok's data, and Kotlin's data classes

#146
post #34

IF you're not on Java 14 yet (70% of industry?[1])... this post shows two other alternatives for Lombok: https://medium.com/@vgonzalo/dont-use-lombok-672418daa819 Autovalue & Immutables [1] https://snyk.io/wp-content/uploads/jvm_2020.pdf pg 5

Unfortunately AWS Lambda doesn't support 14 yet :/

Do you care about memory leaks on a Lambda? I don't think they can live long enough to even invoke the GC. I guess if you're going through a lot of data you could blow your memory budget and have it terminated?

Re: Java's records, Lombok's data, and Kotlin's data classes

#147

Curious question — why do people insist so much on fields being private and there being getters and setters, even when all that getters do is return the field and all that setters do is set it? What kind of problem does this arrangement solve? Why not just use public fields?

Depending on the language, getters and setters may not be source and/or binary compatible with fields from the client side, so if you might ever need to refactor to a nontrivial getter or setter (whether because you are changing representation or for other reasons), starting with trivial getters/setters instead of fields limits future breakage.

Re: Java's records, Lombok's data, and Kotlin's data classes

#148
post #51

Earlier quoted context omitted.

1 million is not a lot. I'd begin by asking myselves "can I afford to chase those pointers?", because maybe you can.

No, because my competitors who are attempting to fill the same orders I am attempting to fill are not chasing pointers.

You are chasing nanoseconds with a garbage collected language?

Re: Java's records, Lombok's data, and Kotlin's data classes

#149

Curious question — why do people insist so much on fields being private and there being getters and setters, even when all that getters do is return the field and all that setters do is set it? What kind of problem does this arrangement solve? Why not just use public fields?

I had the same question -- why accessor methods instead of making the fields public, but final. If anything, it seems like a layer of indirection.

The only answer that makes any sense is this one from Brian Goetz.[1] Namely, that it's a workaround to support mutable fields of otherwise immutable objects.

To be honest, allowing you to override the methods on an immutable, auto-generated class to inject custom accessor logic feels like an immediate retreat from the conceptual goal of providing an immutable record implementation in the first place.

[1] https://stackoverflow.com/questions/66702223/why-do-java-rec...

Re: Java's records, Lombok's data, and Kotlin's data classes

#150
post #123

Earlier quoted context omitted.

Sure of what? If your code is not garbage it takes two seconds to see where things change. It should be very places in the code.

It takes two seconds in a hello world demo. It takes probably more in a millions of lines project.

Fields of an object should typically not change. But yes, I had to make a field of a class immutable some time ago. There was a bug and I could not read the code to figure out were the object was changed. Still, caused by bad code. Same object sent around pretty much everywhere.
Post reply on HN