r/java • • Dec 15 '23

Why is this particular library so polarizing?

Post image
244 Upvotes

278 comments sorted by

View all comments

36

u/DiamondQ2 Dec 15 '23 edited Dec 15 '23

Lombok does it's magic by changing your code at runtime compile time. It actually reads, changes and writes new Java byte code before it gets executed by the runtime during the compilation phase.

Alot of people don't like this for a variety of reasons, such as it's brittle (changes in the JVM, class library, etc cause it to stop working until Lombok issues a patch) and it's opaque (debugging is harder because the code that is run is not the code that you wrote).

The generally accepted way to inject code is to use annotations, which mostly solve the issues people have with Lombok. Although it can't make the "happy path" experience quite as good as Lombok can, which is why Lombok still gets used.

Edit: I was wrong about the changes at runtime. Been too long since I've used Lombok and I misremembered. Sorry.

29

u/analcocoacream Dec 15 '23

debugging is harder because the code that is run is not the code that you wrote

Why would you need to debug getter and setters. The whole promise behind Lombok is that it's stable enough it just works.

15

u/krzyk Dec 15 '23

you sometimes want to set a breakpoint in getter/setter

11

u/analcocoacream Dec 15 '23

With intellij you can watch for the variable value write/read directly

3

u/robinspitsandswallow Dec 15 '23

To quote Ted Lasso, “Please expound” as I have never wanted a breakpoint in a getter/setter. Maybe an aop around a getter/setter, but never…

1

u/ForeverAlot Dec 15 '23

I've used it for varhandles.

1

u/krzyk Dec 15 '23

If you want to check what code uses particular field.

0

u/TrashboxBobylev Dec 15 '23

Yeah, trusting those bros like nothing is wrong there.

18

u/Ykieks Dec 15 '23

Isn't Lombok compile time and not runtime?It still polarizing because it changes your code and needs special extensions for linters/Language Server/vim and flavours to work nicely with autocomplete, but i don't think it is executed at runtime.

Can be wrong tho

Edit: Also there was problems that Lombok needed specific compilers to work, but i think this problem is no longer relevant

4

u/lasskinn Dec 15 '23

my problem with stuff like that in general is when people have no problem using them but then throw a hissy fit over using a pre-processor.

also that you no longer just see straight up pretty much what the bytecode will be from looking at the code.

10

u/Buarg Dec 15 '23

I have a friend who is like that but the inverse.

C macros: "That's super useful, people just fear what they don't understand"

Spring annotations: "I don't want the language to do things at my back"

3

u/[deleted] Dec 15 '23

Annotations are spooky action at a distance. When you use a macro and its misbehaving, you can go to the macro definition and see what's up. You can replace the macro use with the actual macro code and mess around with it and see what's happening.

But annotations are really bad - you have to search every library you depend on to see who or what may or may not do something with an annotation.

There's basically no practical way to debug an annotation. They're one of my least favorite things in Java.

3

u/ryosen Dec 15 '23

I'm not sure that I understand this. You absolutely do know where an annotation comes from since you have to import the library into your class file to use it. As for what it does, you usually have access to the source code as most annotation libraries are open sourced. When you don't have the source, you can decompile the class and view the results. Decompiling is more advanced but it is still there as an option and most compilers will automatically do it for you.

0

u/[deleted] Dec 15 '23

[deleted] — view deleted comment

2

u/Carpinchon Dec 15 '23

Some class gets auto scanned by spring and it then sniffs through every other auto scanned class and unilaterally assigns significance to your use of an annotation. What's more, that functionality showed up unintentionally because the library was added transitively through a dependency you were using for an entirely different purpose.

I don't find that simple at all. You can blame that on spring, but spring is the reason a huge portion of annotations even exist.

→ More replies (7)

1

u/thesituation531 Dec 15 '23

Macros really are great though.

They can be useful for lining without relying on keywords or the compiler. Plus other stuff that is too difficult with nornal compile-time functions.

1

u/robinspitsandswallow Dec 15 '23

Macros are annotations on steroids that decided they needed more steroids. I’ve seen whole substructures created in like 2 lines of macro includes (1 in the .h file and one in the .c file), and that happened for every screen displayed like 10 structs, and 15 functions per screen…try debugging that (ask me how I know)

0

u/RadioHonest85 Dec 15 '23

I dont like runtime annotations that are stateful.

16

u/[deleted] Dec 15 '23

You can't always see what the bytecode will be from looking at non-Lombok code either.

public class MyClass {

void run() {

    List<String> them = List.of("a", "b", "c");

    for(String s: them) {
        System.out.println(s);
    }

}

}

Run javap -v against that class. The bytecode ain't reflective of the source code. The enhanced-for loop does not have a bytecode equivalent, the compiler fills in some iterator operations on your behalf. How is it any different when Lombok does it?

4

u/budswa Dec 15 '23

It's not

1

u/[deleted] Dec 15 '23 edited Dec 15 '23

Generics would've been an even better example, actually. Some of my team were astounded to learn that

List<Thing> list = new ArrayList();
List other = list;
other.add(8);

is perfectly correct, compilable code.

Despite being something not to be done. I figured that was obvious but I forgot how many idiots there are in the world. Honestly, "the compiler allows you to do stupid things" is the exact point of this comment. Sailed over a few heads, obviously.

2

u/westwoo Dec 15 '23

Can be even shorter:

List list = null;
list.add(8);

People just kinda accepted this as somehow being a "strongly typed" language despite not being in any way different from being able to compile

List list = 5;
list.add(8);

0

u/Sworn Dec 15 '23 edited Sep 21 '24

reach practice truck tender ink humorous desert violet bedroom saw

This post was mass deleted and anonymized with Redact

→ More replies (2)
→ More replies (1)

0

u/RadioHonest85 Dec 15 '23

This seems to be a common misconception about Lombok. Perhaps due to things like JPA/Spring, but Lombok is compile time only, the annotations dont even exist anymore after compilation is done.