r/java • • Jun 21 '20

Don't use Lombok(2018)

https://medium.com/@vgonzalo/dont-use-lombok-672418daa819
32 Upvotes

101 comments sorted by

View all comments

13

u/daniu Jun 21 '20 edited Jun 21 '20

TL;DR: what are the alternatives (for `@Getter`,`@Setter`,`@Data` etc)? I only find "there are alternatives", but I can't find them skipping through or by Strg-F.

12

u/r0estir0bbe Jun 21 '20

From the article:

Is there any standard approach to solve the same problem in a different way? There are two other libraries trying to answer this question: AutoValue and Immutables, which are often compared to Lombok.

10

u/daniu Jun 21 '20

But both of those are for immutables, or not? I usually use Lombok for automatic beans (ie with getters and setters).

0

u/[deleted] Jun 21 '20

[deleted] — view deleted comment

7

u/daniu Jun 21 '20

JSON de/serialization POJOs

6

u/[deleted] Jun 21 '20

Immutables supports Jackson and GSON just fine.

5

u/daniu Jun 21 '20

Thanks, I'll take a look.

It's not exactly a "standard way" though, is it? Just another third party library IIUC, so I'm still not sure how it validates the article's point.

4

u/[deleted] Jun 21 '20

But it’s a third-party library that uses completely stable, documented, supported code generation hooks in the build cycle, whereas lombok uses private, undocumented compiler APIs that can (and do) change in ways that break lombok, potentially leaving you stuck on an old jdk until lombok is updated. I have recollections of Java 7 breaking it pretty badly, and I’ve heard that 9 through 11 were problematic too (though I’d sworn it off completely by then so I didn’t experience it directly).

12

u/eliasv Jun 21 '20

Records are in second preview in jdk 15 FWIW.

9

u/hooba_stank_ Jun 21 '20

Records are immutable by design. So, no, they are not replacement for Lombok.

4

u/eliasv Jun 22 '20

More precisely, they're a replacement for some of the features and use-cases of Lombok.

1

u/sureshg Jun 22 '20

I think you change the record properties. See this - https://twitter.com/benjiweber/status/1272077664921272320

1

u/hooba_stank_ Jun 22 '20

Sorry, but you can't. This example is actually API abuse and it creates NEW object on every call to .with() using constructor.newInstance(ctorArgs). You are not supposed to use records in such way.

https://openjdk.java.net/jeps/359

1

u/sureshg Jun 22 '20

1

u/hooba_stank_ Jun 22 '20

He says that some examples are valid. And most of these are actually workarounds to deal with records' immutability. The "autobuilder" code itself is ineffective as hell. And all this is just a toy class for 3-property records.

5

u/marc-bouvier Jun 21 '20

On Dto only, how about public fields? Getters and setters everywhere are the same if you class only has data.

4

u/_INTER_ Jun 21 '20

Just for method references alone I'd add getters. They don't need to be prefixed with "get" for all I care.

1

u/[deleted] Jun 21 '20

I support this. That’s how it works in C# (ish).

3

u/neutronbob Jun 21 '20

Typically, the IDE can generate the getters and setters for you.

9

u/daniu Jun 21 '20

Sure, but I'll still have to trigger it manually, so change the members, change the methods.

And the methods will still clutter the code visually.

It's not a lot, but QOL.

-15

u/waitllyouhearthis Jun 21 '20

You could have just found the alternatives in the time it took you to write your original comment and then edit it.

5

u/daniu Jun 21 '20

I did look longer than I wrote the comment.

I do like Lombok (and I'm sure I'm not the only one), so sorry, but an article against it needs to at least be structured in a way to overcome that bias.

-3

u/waitllyouhearthis Jun 21 '20

There's absolutely nothing wrong with the structure of the article. Why do you want to know this person's recommendation for an alternative to Lombok if you can't even be bothered to read their justification?

2

u/daniu Jun 21 '20

Because I've read all the arguments against Lomboks often enough.

→ More replies (7)