Tuesday, August 23, 2022

Java is more beautiful with Lombok

I have been using Lombok for a couple of years now and find it extremely useful. Below is why you should consider it.

Why should I use Lombok?

  1. Reduces boilerplate.
    - No need to write getter/setter/toString/equalsAndHashcode methods
    - Makes it easy to spot class behavior instead of boilerplate around the state.
  2. Inherent support for Builder design pattern. This means no need for class constructors with lengthy set of arguments.
  3. Annotation based - improves code readability
  4. Industry-wide acceptance (including e-commerce giants and financial services companies)
  5. Intellij Idea v2020.3+ inherently support Lombok. So no need to use any plugin.

What about the not-so-good things?
IDE needs to support Lombok else your code wouldn't compile.
- Intellij Idea v2020.3+ support Lombok inherently. For older versions you would need to use a plugin (available for free).
- For Eclipse and other IDE, download plugin for Lombok website
With most users already on Intellij/Eclipse, I hardly see this as an issue

What do I need to get started ?
  1. Add Lombok maven dependent to your project
  2. Install Lombok plugin for your IDE (not required for Intellij Idea v2020.3+)
Is there a learning curve?
Yes, but its pretty much flat.
Documentation is very good (https://projectlombok.org/features/) and one can get started in well within 30 mins.


Can I get rid of Lombok if I don't like it ?
Yes ofcourse. Delombokify the classes using below simple command which will replace all lombok annotations with the actual code.

java -jar lombok.jar delombok <src-code-dir> -d <delombok-code-dir>


 Code with Lombok annotations






Code which has been delombok-ified using the library's delombok feature

                                                            .... some more getters and setters