Spring boot Lombok and Builder, please help a junior
Lombok @Builder and @Setter Differences
Videos
Consider:
Order order = new Order("Alan", "Smith", 2, 6, "Susan", "Smith");
What do the parameters mean? We have to look at the constructor spec to find out.
Now with a builder:
Order order = Order.builder()
.originatorFirstName("Alan")
.originatorLastName("Smith")
.lineItemNumber(2)
.quantity(6)
.recipientFirstName("Susan")
.recipientLastName("Smith")
.build();
It's more wordy, but it's very clear to read, and with IDE assistance it's easy to write too. The builders themselves are a bit of a chore to write, but code-generation tools like Lombok help with that.
Some people argue that if your code needs builders to be readable, that's exposing other smells. You're using too many basic types; you're putting too many fields in one class. For example, consider:
Order order = new Order(
new Originator("Alan", "Smith"),
new ItemSet(new Item(2), 6),
new Recipient("Susan", "Smith"));
... which is self-explanatory without using a builder, because we are using more classes with single-responsibilities and fewer fields.
This is not a lombok specific feature, this is called a builder pattern.
Imagine you have a class with 20 parameters, and 10 of them are optional. You could somehow make tons of constructors taking care of this logic, or make a constructor with all those arguments and pass nulls in some places. Isn't builder pattern simply easier?
What exactly does @ Builder annotation do? I need to deliver JUnit tests (it's my first contact with tests) of a really small challenge project, but without using Lombok and I'm not finding tutorials that don't use @ Builder to do them. Thanks in advance.
ps: english it's not my first language, sorry
edit: thank you all very much for the help, I managed to solve the problem through the documentation and some videos
Hello everyone. I'm new to java and springboot. I found some project by some develpoer in github and see alot of developer use Lombok Builder and Setter on their project. some use both some use one of other. But when I look at the code I see both have the same usage. like when creating User. both can achieve it. Why some developer choose one of other and some use both. What is the reaseon?