Should You Ban Lombok? Weighing the Pros and Cons of This Java Tool
This article examines Lombok—a Java library that reduces boilerplate through annotations—by outlining its installation steps, showcasing code examples, and critically discussing its drawbacks such as team dependency, readability, debugging challenges, upgrade constraints, and potential violation of encapsulation principles.
Lombok: Benefits
Lombok is a practical Java tool that helps developers eliminate boilerplate code, especially for simple POJOs, by using annotations.
How to Use Lombok
1. Install the Lombok plugin in your IDE
Lombok supports major IDEs such as Eclipse, IntelliJ IDEA, and MyEclipse.
2. Add the Lombok dependency
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>3. Use Lombok annotations in code
Common annotations include @Data, @Getter / @Setter, @Builder, and @NonNull.
import lombok.Data;
@Data
public class Menu {
private String shopId;
private String skuMenuId;
private String skuName;
}Lombok: Drawbacks
Team Dependency
All developers must install the Lombok plugin in their IDE; otherwise, projects using Lombok will fail to compile, forcing the whole team to adopt the same setup.
Readability and Debugging
Since Lombok generates code at compile time, the source code appears incomplete during development, making it harder to read and debug, especially when tracing generated getters or setters.
Hidden Pitfalls
Over‑reliance on Lombok can lead to unexpected behavior if developers do not understand the underlying generated code. For example, using @Data without configuring @EqualsAndHashCode(callSuper=true) may cause equality checks to ignore superclass fields.
Upgrade Constraints
Lombok’s strong coupling to the JDK and its own release cycle can hinder upgrading to newer JDK versions if Lombok does not yet support new language features.
Encapsulation Violation
Automatically generated getters and setters expose all fields publicly, which can break object‑oriented encapsulation. For instance, a ShoppingCart class annotated with @Data provides public setters for itemsCount and totalPrice, allowing external code to modify them independently of the items list, leading to inconsistent state.
@Data
public class ShoppingCart {
// 商品数目
private int itemsCount;
// 总价格
private double totalPrice;
// 商品明细
private List items = new ArrayList<>();
}Conclusion
Lombok can dramatically reduce boilerplate and make code concise, but it also introduces team coordination overhead, readability and debugging challenges, upgrade risks, and potential encapsulation breaches. Use it judiciously: enjoy its benefits while being aware of its drawbacks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
