Should you use Lombok project in modern Java?
Short answer: yes

Lombok is a popular Java library that helps developers reduce boilerplate code in their projects. It provides a set of annotations that generate code automatically at compile-time, reducing the amount of manual coding required.

Lombok is compatible with popular IDEs such as Eclipse, IntelliJ IDEA, and NetBeans, and can be easily integrated with Maven and Gradle.

Annotations examples

Lombok's annotations provide various features that enhance code quality, readability, and maintainability. Some of the commonly used annotations are:

//Generates getter methods for class fields
@Getter
// Generates setter methods for class fields
@Setter
// Generates a no-argument constructor
@NoArgsConstructor
//Generates a constructor with all arguments
@AllArgsConstructor
// Generates a toString() method
@ToString
//Generates equals() and hashCode() methods
@EqualsAndHashCode
//Generates getter and setter methods, toString(), equals(), and hashCode() methods, as well as a constructor with all arguments
//Very useful with framework like Hibernate
@Data
//Generates a builder pattern for the annotated class
@Builder

Why I like it

Lombok's annotations help developers write cleaner, more concise, and more maintainable code by reducing the amount of boilerplate code. This makes the code easier to read and understand, and reduces the likelihood of errors.

Lombok has become popular among Java developers because it helps them write better code faster. The library has a large and active community of developers who contribute to its development, provide support, and create new features.

I use it in every java project for the following reason:

  • Reduced Boilerplate Code: One of the primary reasons why Lombok is a good option for Java developers is that it helps in reducing the amount of boilerplate code that developers need to write. Lombok provides annotations such as @Getter, @Setter, and @ToString that generate the code automatically, reducing the amount of manual coding required
  • Increased Productivity: By reducing the amount of boilerplate code, Lombok increases productivity by enabling developers to focus on writing business logic instead of repetitive and time-consuming code.
  • Improved Code Readability: Lombok’s annotations improve code readability by providing a more concise and expressive syntax. This helps in reducing the amount of code that developers need to write, making the code easier to read and understand.
  • Easy Integration: Lombok is easy to integrate with existing projects and frameworks. It works seamlessly with popular IDEs such as Eclipse and IntelliJ IDEA, and can be integrated with popular build tools such as Maven and Gradle.
  • Active Community Support: Lombok has an active community of developers who contribute to its development, provide support, and create new features. This ensures that Lombok is constantly evolving and improving, making it a reliable and efficient tool for Java developers.

Why (some) does not like it

Some developers have raised concerns about the use of Lombok in production code. Some argue that the generated code may not be as efficient as hand-written code, and that the annotations may make the code less readable and maintainable in the long term. Additionally, some developers may find it challenging to debug generated code.

Java Records

Starting from java 14 we have some similar functionality built in into the language

public record Person (String name, String address) {}

Records are immutable data classes that require only the type and name of fields. The equals, hashCode, and toString methods, as well as the private, final fields and public constructor, are generated by the Java compiler.

It's similar to the @Data annotation in lombok, but java record are immutable object so private, final field for each piece of data and only getter are created.

In my opinion Java record are a good option but misses a lot of flexibility and functionality present in Lombok.

I can give you another example, we all use slf4j to log our code execution, usually the pattern is always the same:

public class HelloWorld{
    Logger log = LoggerFactory.getLogger(HelloWorld.class);
}

I ugually copy past it from other classes. With Lombok, it's a simple annotation, and the log is ready. The variable will always be called log and you don't need to remember to change the class name neither how to get an instance of a logger.

//Generates a logger field for the class, which can be used to log messages
@Slf4j
public class HelloWorld{
}