Enterprise Java

Lombok – you should definitely give it a try

Lombok is not a new thing in a Java ecosystem, but I must admit I always underestimated its value until I tried it or I was “convienced” to try it. I did not see much value in adding a library that generates code that can be easily generated by any modern IDE these days. So I ignored the library and I have been writing or generating tons of boilerplate code. Not anymore. In 2016 I joined a Spring-based project where project Lombok was already in place. And since then I can’t work without Lombok anymore… Why?

So what is Lombok anyways?

Shortly speaking, Lombok is a Java library that generates tons of code for the developer by plugging in into the IDE and building tools. For example, instead of adding getters, setters, equals, hashCode and toString methods to POJOs, single [@Data](https://projectlombok.org/features/Data) annotation can be used.

Build tools support, like Gradle or Maven, does not bring issues

Lombok works with Gradle with no issues. You add compileOnly dependency on Lombok and that’s basically it:

compileOnly ("org.projectlombok:lombok:${lombokVersion}")

I did not expierience any issues with Maven neither, although I mainly work with Spring related projects and recently they all are Gradle based.

IntelliJ support is good enough

I am working with IntelliJ on a daily basis and its support for Lombok works just fine. Lombok is supported by 3rd party plugin: https://github.com/mplushnikov/lombok-intellij-plugin.

The configuration of the plugin is extremely easy: you need to enable Lombok plugin and annotation processing for the project. Of course, Lombok must be in the classpath. With project configured you can start importing Lombok annotations and start using them immediately in the source code.

I did not notice issues with code completion in IntelliJ. I did not notice any delays or missing features. When I want to display code definition for the generated method it shows me the Lombok annotation – which is fine – it would nice though to see the generated code.

On the negative side, sometimes it happens that the code is not immediately available – and then manual compilation needs to be executed. This is really rare in my case.

With Lombok enabled, some features cannot be reached directly from the code editor. For example, when using @Builder annotation a lot of code is generated, including the builder class. To find usage of certain builder methods you need to do this from the Structure view..

Navigating to symbols by name in generated code is not possible but this seems not an issue: when working with Lombok you know the generated code is related to certain classes. For example, UserBuilder is related to User class so you jump into the Userto see its builder (if you really need to).

All in all, on a daily basis there are no show stoppers as it goes to IntelliJ.

Reading the code is easier

One of the main advantages of using Lombok is less code that one needs to read. This is extremely useful during the code reviews – I open the class and I immediately see if it is a anemic @Data class or maybe a @Value object, if it provides @Builderetc. And although Lombok requires even more annotation in the source code (Lombok annotations, JPA annotations, Jackson annotations, Spring annotations …) it still makes the code more concise and easier to read / review.

Lombok standarizes (some) team practices

For example, before I started using Lombok, in every project there were several approaches to create builders . With Lombok it is much easier to maintain these practices (@Builder and @Singularity) .

Lombok works fine with other libraries

I did not expierience issues with JPA or Jakson annotations mixed with Lombok annotations. I have heard, though, about issues with MapStruct and Lombok in the past but it seems to be fixed now: (https://github.com/mapstruct/mapstruct/issues/510)

Lombok annotation can be easily used with Spring components so that less code is needed while creating. For example @AllArgsConstructor can be used to inject bean’s dependencies as Spring does not require contructors to be annotated with @Autowire:

@Service
@RequiredArgsContructor
class SomeService {
    private final Dep1 dep1;
    private final Dep2 dep2;
}

Worth noting (maybe) is the fact the Spring Boot Initializer (http://start.spring.io/) provides Lombok dependency in the generated project files (one of core dependencies to be added to your new project).

Consider Lombok for your next project

Lombok is a great library that speeds up the development, makes the code more concise, easier to read and maintain. Lombok seems mature enough to give it a try. Even if you decide to use only for simple cases it can bring a lot of value to your project. Believe me or not, but I was very sceptical about Lombok until I tried it for several weeks.

Reference: Lombok – you should definitely give it a try from our JCG partner Rafal Borowiec at the Codeleak.pl blog.

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ruurd
Ruurd
7 years ago

If you want to increase the useability of Lombok, then add POJO-tester (http://www.pojo.pl) to your set of tools. Profit!

Riccardo Cossu
Riccardo Cossu
7 years ago

Since it is so easy to setup in gradle is definitely an option if for some reason you’re forced to use Java; but if you’re not it is just as easy to add support for another language like Groovy, Kotlin, Scala and others which also give you interesting language features.

Back to top button