DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Production-Ready Kubernetes Service Checklist
  • Challenges of Using LLMs in Production: Constraints, Hallucinations, and Guardrails
  • Efficient Artificial Intelligence: Training to Production
  • Mastering Safe Production Deployments

Trending

  • Debugging Core Dump Files on Linux - A Detailed Guide
  • How to Convert Between PDF and TIFF in Java
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  • Key Considerations in Cross-Model Migration

5 Tips For Using Lombok In Production

This look at using Lombok to reduce boilerplate code in production examines useful annotations and a few general practices to keep in mind.

By 
Alex Collins user avatar
Alex Collins
·
Jun. 01, 17 · Tutorial
Likes (20)
Comment
Save
Tweet
Share
44.8K Views

Join the DZone community and get the full member experience.

Join For Free

I was reminded the other day about the excellent Project Lombok. If you're not familiar with this tool, it helps reduce boilerplate AND testing. You annotate your Java source code with some special annotations, and it generates code for you. For example, if you annotate a field with a @Getter, it generates a public getter method for that field.

Having previously used it successfully on a number of production systems, here are my top five tips.

Tip 1: No Lombok With Logic

Bytecode compiled for generated code is hard to debug. This is made more confusing where generated code gets mixed with logic. Lombok works well if you don't mix it with logic.

A useful side effect of this tip is that classes only contain generated code.

Tip 2: Don't Test the Generated Code

The generated code is trustworthy, so you can save a lot of time by not testing it or performing static analysis on it.

If a class only contains fields, then you don't need to test it or perform static analysis on it.

Tip 3: Use @Data for Your DAOs

Where is Lombok especially useful then? In DAOs. These objects typically don't have a lot of logic and carry a great deal of boilerplate. Specifically, three annotations were the most useful, two of which we'll get to in the next sections.

@Data creates your getters, setters, to string, and equals/hashcode. Great for DAOs in either the database layer or the API layer.

Tip 4: Use @Value for Immutable Value Objects

@Value is essentially an immutable version of @Data. Very useful for immutable value objects. Use in many of the cases you might use a Scala case class.

Tip 5: Use @Builder

@Builder is useful when you have an object with many fields with the same type. Rather than having a constructor with many string fields, use the builder instead.

Tip 6: Think About Avoiding the Other Annotations

There are a number of annotations that we never found widely useful:

  • val: A great idea, but hobbled by poor IDE support.
  • @Cleanup: Use try-with-resources.
  • @SneakyThrows: Throw only runtime exceptions, and perform exception mapping where needed.
  • @Synchronized: Just never found a place to use this.

Tip 7: Exclude Generated Classes From Sonar Reports

As the generated code typically ends up with many untested methods (e.g. you never test the generated equals, as you don't need to, but they tend to end up being very complex for classes with many fields), these classes are excluded for static analysis and code coverage. If you are using Maven and Sonar, you can do this using the sonar.exclusions property.

Production (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • The Production-Ready Kubernetes Service Checklist
  • Challenges of Using LLMs in Production: Constraints, Hallucinations, and Guardrails
  • Efficient Artificial Intelligence: Training to Production
  • Mastering Safe Production Deployments

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!