Eliminate Boilerplate Java code with Lombok

I’ve been writing a lot of boilerplate Java code, lately — getters, setters, hashCode, equals and toString. Actually, I’d use Eclipse to generate these methods. There were some minor annoyances though, like when I needed to add a new member variable and then I had to regenerate code to include it.

It wasn’t pretty, but I had made my peace with it. Until I got to a class that had a lot of fields and I needed a builder to construct it. For those unfamiliar with the builder design pattern, here’s a quick primer. In a nutshell, I wanted to be able to write code like this:

Unfortunately, Eclipse doesn’t generate these builder classes and I sure as hell wasn’t going to write them by hand. So, I did what all developers do and headed to StackOverflow and stumbled upon this thread that mentioned Project Lombok. Project Lombok generates boilerplate code for you based on annotations. All I needed to do was to write the Java class like this:

The Data annotation generates getters, setters, hashCode, equals, toString and the Builder annotation generates the builder class. That’s it! I just followed the instructions on the Project Lombok website and was able to use it in my project in 10 mins.

Siva Narayanan

I am known to be "the CTO of one, the father of two, and the roasting baba of many."

More of our stories from

Engineering
How we created a Medium-like blurry background effect

Here's how we improved user experience, decreased load time and made Fyle accessible for users without a fast internet.

Read more...
Bye bye WordPress, welcome Webflow.

This blogpost documents our journey as we bid goodbye to WordPress and migrated to Webflow.

Read more...
How we reduced our website build time by 59%

I came up with five 3 second changes to reduce the build time by over 59%. Here's more about my experience.

Read more...
Hello, Web Technologies!

I’m a first-time entrepreneur and I’ll be recording my learnings and experiments over time. I am always eager to interac

Read more...
The Non-Boring Guide to OAuth 2.0

If you’re developing an application that needs access to a user’s Google / Facebook / LinkedIn information, you’ll need

Read more...
Dealing with Nested Objects in your Web Application

A couple of weeks ago, I ran into a peculiar problem that I think might be useful to talk about. It took me a bunch of

Read more...
Eliminate Boilerplate Java code with Lombok

I’ve been writing a lot of boilerplate Java code, lately — getters, setters, hashCode, equals and toString. Actually, I’

Read more...
Hello, Web Technologies! — Part II

This is a follow-up to my first post about technology choices I made while building out our product. I wanted to pen my

Read more...
Sharing Files using S3 Pre-signed URLs

Amazon’s S3 is a reliable, cheap way to store data. We use it to store user-uploaded images and documents as s3 objects

Read more...
JSON Web Token Concepts

There are many technical articles about JSON web tokens (JWT) on the interwebs, but I haven’t found one that explains...

Read more...

All Topics