Lombok: To use or not to use
Omar Ismail
Senior Software Engineer @ Digitinary | Java & Spring Expert ? | AWS & Microservices Architect ? | FinTech & Open Banking Innovator ?? | Digital Payments Expert ?? | Top 200 IT Content Creator in Jordan ?? | 40K+ ??
Project Lombok is a Java library that automatically plugs into your editor and builds tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully-featured builder, Automate your logging variables, and much more.
Project Lombok is an excellent tool to make coding in Java easy. And it is a good answer to devs who say that Java codes are extremely large when compared to Python. Lombok helps the developers to focus on the implementation by not worrying about writing the boiler plate codes and keeping the final code shorter (relatively ??).
But… Most of the IDEs these days come with the support of generating the boilerplate codes then why do we need Lombok again?
Yes, while we can still generate the code in IDE, it still is a boilerplate code adding up to the total LOC and thereby reducing the readability and maintainability of the code.
In this article, I will discuss some myths, key features, and implementation details of Lombok.
Demystifying the myths
There are some common assumptions and theories when teams discuss using Lombok in their Application. So it becomes important to discuss them first in this article too before we even talk about the implementation.
Yes, most of them are valid points when we talk about adding Project Lombok to our Application but the question is what is the impact? Let us see.
Tight Coupling
All the classes and codes depend on Lombok hence making them dependant on each other resulting in tight coupling. And if it happens that Lombok is to shut down then the whole project will be in trouble.
Code smells and impacts on clean code
This is tricky, many senior developers face the issue of their juniors incorrectly using the Lombok making it more troublesome than advantageous. But there are some tips and tricks that can help to correctly use Lombok. One of those tricks is to make your juniors read this Article
There are three possible scenarios,
For case 1, a team will have enough qualified members to review the code thereby reducing the overall risk of bad code after integrating Lombok, and the rest of the team members eventually pick up the good practices.
For case 2, a little bit of bad code is anyways acceptable. The team will eventually learn going forward and improve the overall quality gradually.
For case 3, it becomes a call for the team, depending on the previous experience of team members on Lombok and the scale of the Application, you can choose to either pick it or drop it.
Setting up Lombok
I will try to keep this blog short and interesting to read. You can find the detailed?implemented projects on Github?for reference.
Adding Lombok to project
领英推荐
Setting up the IDE
Implementing Lombok
There are some really good articles out there that explain in detail on how to use Lombok, so I will focus on how to use them correctly.
These are a few official documents, to begin with
If you need this article to cover the detailed implementation too, then let me know in the comments.
Common mistakes while using Lombok
Incorrectly using?@Data
@Data?is the most commonly used Annotation, but it is an equally risky annotation. Since it adds?@Tostring and @EqualsAndHashCode?too it makes your code dangerous when using it with?Collections?or other similar items
Expectation will be that both the assertions will pass because we are creating two different objects and adding them to a set but since?@Data?adds EqualsAndHashCode too, the set inserts only one value because the contents of the objects are the same and hence second test case will fail.
Many developers are not familiar with this feature and end up spending several days in debugging what is wrong.
Incorrectly using Constructor annotations
Some Good Practices
Few things to remember,
having said that, let us look into some good coding practices using Lombok
Thanks TO : Mohammed Atif