What is Lombok?
Every developer knows that every time you're going to develop something, there is a lot of code to be written because Java, in some cases, is too verbose. As a developer, you and I know that any technology which helps us write less but achieve the goal is fantastic; that's what IDES are trying to do in every update they have.
So Lombok is here to prevent you from writing repetitive code like getters, setters, toString, etc.
Let me show you, but first.
How to install Lombok in my project?
Most of my projects are based in Maven, if you're using another project management, you can see in this link how to install Lombok in your project.
As I said, I use maven, so it's pretty simple, add the dependency in my pom.xml, and we're good to go.
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
Tired of filling your classes with Getters and Setters?
So, how are your class nowadays? Look at this example above:
public class User {
private String name;
private Long id;
private String phoneNumber;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(String id) {
this.id = Long.valueOf(id);
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
That's huge, right? I just wrote four variables, and we got 40 lines of code, and I didn't even start to code my program yet.
You can say: "Dude, I don't worry about it. My IDE writes all that stuff for me.."
Yes, I know. Do you think I wrote all these 40 lines of code? No way! But that's not the bigger problem here, we are developers, and we're always trying not to waste our time with simple problems and want our code to look clean as possible. That's why Lombok was created. It helps us to get much more productive.
How to use it
First, let's remove all these getters and setters that we just wrote. So now, when I instance a variable of User class, IDE shows an error.
It makes sense because now there is no method called getName inside our User class. So to use Lombok to fix this, we only need to use this **annotation **above our variable.
import lombok.Getter;
public class User {
@Getter
private String name;
private Long id;
private String phoneNumber;
private String address;
}
IDE understands that a getter is implicit inside the User class with this annotation. We can now use the same idea with the @Setter, @toString, @EqualsAndHashCode, etc.
And if you don't want to write these annotations in each variable, you can put them above your class name. Like this:
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@EqualsAndHashCode
@ToString
public class User {
private String name;
private Long id;
private String phoneNumber;
private String address;
}
So much cleaner, right? And we can make it even better! Instead of writing all these 'getters, setters, equals, etc.' annotations, we can only use @Data annotation.
Final Thoughts
Of course, we didn't show all the features that Lombok has. Here, the main goal is to convince you to boost your productivity and focus on what really matters.
Thanks for having me, and I'll see you in my next post, bye!