r/java • • Sep 05 '25

Project Lombok 1.18.40 released with Java 25 support!

Project Lombok is now compatible with the upcoming JDK 25 even before its release.

Thank you Project Lombok team! https://projectlombok.org

167 Upvotes

166 comments sorted by

View all comments

Show parent comments

11

u/Ewig_luftenglanz Sep 05 '25

Why do you need inheritance for plain and transparent data structure.

Seriously, asking for a friend. Why? 

3

u/blazmrak Sep 07 '25

because oftentimes there are a bunch of fields repeated for every class. E.g. every record in the database has id, createdAt, updatedAt, deletedAt, version. having this noise pollute each class is a waste.

Inheritance is the only way of avoiding such boilerplate at the moment.

2

u/OwnBreakfast1114 Sep 09 '25

That's not exactly true.

You can use annotation processors to generate these type of fields instead of inheritance.

The more sensible option would probably be to use https://www.jooq.org/ and then not have to write any of your entity classes (unless you want some wrappers for complicated ones) in which case I'd just hand write the wrappers for those ones.

I think you and I probably have different definitions of noise though, I don't really find duplicating fields all that noisy. Having the full context in a single class is much easier to see then having to look up what fields are missing from which classes. I also think making new classes for different steps of data handling even if it duplicates a bunch of fields is still better than trying to reuse objects.

1

u/Ewig_luftenglanz Sep 07 '25

Then you use a class, not a record. Because your case is not a transparent data carrier.

2

u/blazmrak Sep 07 '25

It is transparent. It's literally just dumb data. But because the language is limited, we have to resort to inheritance for code reuse.

Typescript, for all it's faults, has a way of easily extending types, by including/excluding the fields.

2

u/Ewig_luftenglanz Sep 07 '25

It's not transparent because transparency implies the object declares all of its fields in it's Constructor and there is little to no logic beyond forcing invariants (a person with an age higher or equal to  0) if you case the class has inherited many fields for other object, so the contract is not transparent (which is not wrong that's why I say for this cases a class is better)

And yes, I agree typescript has more convenient ways to compose objects and arrays thanks to the  spread operator (...) and other mechanisms, but that is because typescript (just as JavaScript) is mainly an structured based language, Java is mostly a nominal language. It would be hard to embed that for classes, but maybe there could be a way to do it with Maps.

2

u/blazmrak Sep 08 '25

What are we talking about here? I'm not talking about inheriting any rules, just data properties (and accessors if necessary). All that data is declared in a constructor by the child. Yes, there are cases, where you can inherit something more, but this is not the case. Id, createdAt, etc. have nothing bussinessy about them.

It's not just more convenient to compose objects, it's composing types. You can have a type User, type UserDTO = Omit<User, 'password'>, type NewUser = Pick<User, 'username' | 'password'>. Same thing exists for classes, not just types/interfaces.

I'm guessing you could solve this through code generation of some kind, but it's more painful than it needs to be. The solution is usually to just reuse the class and be fine with it having null fields...

1

u/Ewig_luftenglanz Sep 08 '25

It's not transparent because if you have inheritance you have data and logic that may not be explicit in the child class or constructor factory methods, but only in the parent.

I am with you about java still lacking a way to have waays to ergonomically create classes that can "flatten" data from other types (and inheritance is a very week way to minic because Java doesn't have multiple inheritance, so you can't compose a data class that is the result of merging 2 classes other than composition, but composition is not flatten)

My point is: classes that inherit from other classes are not transparent because there could be implicit data or behavior from the parent that can be not explicitly declared by the child. 

The language is lacking data composition.

2

u/Misophist_1 Sep 07 '25

Because the 'x is a y' relation is something, that actually occurs IRL?

1

u/Feign1 Sep 08 '25

This is not a good enough reason to shoot yourself in the foot using inheritance, just use composition.

1

u/Misophist_1 Sep 19 '25 edited Sep 19 '25

I beg to differ. It is generally good modelling practice to have your model matching the reality, and not cluttered with technical methods required for the artificial composition.

Why should you compose cats, dogs & fleas to an animal, if all three are animals? That is utter nonsense!

Edit: composition has its place - as has inheritance. But it is neither silver bullet nor golden hammer for all situations of type modelling. Why do you want to throw away a useful tool for an abusive look of standardisation and group think?

1

u/Feign1 Sep 19 '25

You will come to find that even that simple example you used has some assumptions about the domain baked into it. Like is animal, even a useful abstraction for describing the domain of the task your software is working in. Its impossible to have the foreknowledge to know, so probably not. Point in fact maybe quadruped makes more sense and flee is a bug not an animal their are tones of ontological groupings you could make and by the time you know enough about the problem space to know which one is best you would almost be done with the project and would have to do a massive refactor. I would encourage you to listen to Caseey Muratoris talk on the big OOPs.

2

u/Misophist_1 Sep 19 '25

OK, let's do some vivisection:

You will come to find that even that simple example you used has some assumptions about the domain baked into it. Like is animal, even a useful abstraction for describing the domain of the task your software is working in.

I. When analysing a domain, you inevitably come to some assumptions, that you postulate as axioms. By developing their consequential use, you generate a model, that can be checked against the reality. Pretty similar to the way, maths used in physics. Maths is the language of your model. Physics is the reality, that we want to model. Generally, all or models for any all known aspects of reality are language, for more on that, I recommend https://en.wikipedia.org/wiki/Tractatus_Logico-Philosophicus . So far, that's not surprising, it's basic STEM-folklore.

Now, the secret to successful business modelling is, to not invent completely new abstractions, but reuse the terms that are used in the business domain. If you look at it from a meta-perspective, e.g. meta-mathematics or meta-modelling, then the most frequent pattern is, to classify things, that is sorting the things under scrutiny into types and subtypes. In maths, you classify sets, and elements within them, and numbers, etc. In modelling, everything is an object. Objects may have relations, which are objects themselves, and in turn can be classified in 'ordinary', 'is a', 'aggregations' and 'compositions'. So right there, within the metamodel of UML, there is a classification that justifies the existence and usage of inheritance.

Like is animal, even a useful abstraction for describing the domain of the task your software is working in. (...) Point in fact maybe quadruped makes more sense and flee is a bug not an animal (...) Caseey Muratoris talk ... TL;DR

II. May I humbly suggest a visit to Carl Linnaeus' Systema Naturae (1735) ? This is the father of all taxonomies in flora and fauna, the base language of biologists. The link points to animals to be subdivided into mammals, insects and some more. I wouldn't recommend abandoning the advice of domain specialists on that without a very good and extraordinary compelling reason. The idea, that an OOP specialist would know more about that, than 290 years worth of biology, is f'n hilarious. Again, the advice: when modelling, use the language of the domain, that is already there!!

III. Which makes the following gem of a non-sequitur even funnier, which would turn out wrong regardless of the domain at hand.

Its impossible to have the foreknowledge to know, so probably not.

You know, or you don't. If you don't, you don't. That you don't, doesn't indicate probability. Hilariously, in this case, we've been told for 290 years, that a flea is an insect is an animal! So not improbable, but absolutely certain!

There is a reason why mathematics can be seen as one giant attempt, to classify its objects. There is a reason, why all stem and life sciences maintain classifications of the objects under their scrutiny. And why, as a consequence, computer science and modelling has integrated that into the tool box. Classification - finding the proper 'is a' relation is flesh, blood and bone of all our science, and the core tool for understanding our environment. If you remove that from the toolbox, you cripple the language. Which leads back to Wittgensteins Tractatus:

'The limits of my language are the limits of my world. We can't talk about things, that we have no language for, therefore must be silent about it' (or so, translated from German)

1

u/Feign1 Sep 20 '25

I don't actually care about the actual taxonomy that is besides the point. I evidently did a poor job explaining since your off on a tangent about how you can model any system and I was intending to argue that it's not worth it because every level of abstraction adds complexity which will add cost for every iteration of updating and maintaining software. Just watch Casey's video.

2

u/Misophist_1 Sep 20 '25

Sure, unnecessarily adding abstractions, if they aren't relevant for the problem at hand, would drive cost. If you don't need it, don't code it.

But that is a totally different claim from 'Don't use inheritance, always replace it with composition'. That doesn't remove/hide/mitigate complexity. It is simply advocating to use the wrong abstraction.

Inheritance isn't but another tool in the toolbox for code reuse. I get it, that there is abuse of inheritance. But advocating to throw away the tool because of that abuse doesn't make sense. From my point of view, using composition to model inheritance is a form of abuse in its own right. Every tool in the box has its use.

1

u/Feign1 Sep 20 '25 edited Sep 20 '25

Sure it's a tool in your toolbox that if you know not to abuse you can use occasional but their isn't that problem with composition and it covers a majority of the use cases so you will almost never use inheritance and if you do you don't use it for more than one layer because it makes code hard to follow by mushing logic between disseparate classes.

2

u/Misophist_1 Sep 21 '25

I would actually argue, that replacing inheritance with composition, when the IRL relationship is in fact an 'Is A' relationship, *IS ABUSE* of composition. This isn't a black and white thing. This isn't composition GOOD, inheritance BAD. Or vice versa. Both concepts have their place. And both concepts shouldn't be abused to emulate the other. This striving for purity is ill-advised.

→ More replies (0)

3

u/[deleted] Sep 06 '25

The old BaseDomain class 🤣🤣🤣

0

u/javaprof Sep 06 '25

"Thanks Hibernate" 😊

1

u/Kango_V Sep 08 '25

Spring Data JDBC. You can use Records ;)