Speed up your Java coding using Lombok 🚀

Speed up your Java coding using Lombok 🚀


Abdulcelil Cercenazi

3 years ago | 1 min read

Java has a bad side that everybody hates 😓, what is it?

A lot of boilerplate code is the same for most of the applications we write.

Like what? 🧐

  • Getters
  • Setters
  • Constructors
  • Builder pattern
  • and many more...

Wouldn't it be nice if someone took care of that work for us? That is where Lombok introduces itself.

What does it do? 👀

It generates the byte code for those common tasks (Getters, Setters, etc..) and puts them in our .class files which make them usable by the code we write.

How? 🔧

We need to add the Lombok dependency to our Maven build.
Then, we just need to annotate the wanted classes, fields with some Lombok annotations.

Let's look at some code! 👩🏻‍💻

No Lombok 🧟

Lombok 🤴

The Maven dependency looks like this

Note: the version part will change depending on the last release of Lombok.

What just happened? 🕵️‍♀️ 🕵️

  • we used annotations to generate
    • Getters
    • Setters
    • Constructors
    • Builder pattern
    • toString implementation

We can also make tweaks on some annotations, like the setter for the id field.

  • We set its access level to None, meaning that we don't a setter to be generated for it.

Was it worth it? 📊📊

We wrote 52 lines when not using Lombok.
We wrote 8 lines when using Lombok.

Lombok helped us shrink our code size by nearly 4 times. Those numbers can go up even more when having more variables in our classes.

Lombok has a lot more annotations that offer a ton of help, to view them all visit their website.

Conclusion 👇🏾

Lombok helps us focus on our business code and not worry about the little details of getters/setters/constructors/common design patterns and other Java constructs.

Code 👨🏽‍💻GitHub


Created by

Abdulcelil Cercenazi

I love to learn and teach. Follow me for Software development/Clean coding topics :)







Related Articles