Lombok: To use or not to use

Lombok: To use or not to use

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.

— Team Lombok

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.

  1. It adds tight coupling to the code
  2. It reduces the ability to foresee the code smells
  3. It hinders the good design practices

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.

  1. But that’s not the case, since Lombok comes with the DeLombok tool, even if you have to remove Lombok from your application you can easily DeLombok it and remove the dependency from the Application.
  2. Since all the dependency to Lombok is unidirectional i.e. classes depend on Lombok but Lombok doesn’t directly depend on the classes, it doesn’t create the real tight coupling. Also, the compiled code is finally Lombok free hence making it safe to use.

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,

  1. The project is for a well-established organization where code quality is of utmost importance
  2. Project is a fast-faced Application where completion is important and code quality is of medium importance
  3. Project is a medium scale Application where both time and quality is crucial

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

  1. Adding Lombok to Maven project
  2. Adding Lombok to Gradle project

Setting up the IDE

  1. Eclipse
  2. IntelliJ
  3. VS Code

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

No alt text provided for this image
No alt text provided for this image

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

  • In the same example above, you can notice that I have added AllArgsConstructor but have not marked the fields final. This process has two flaws, anyone else cannot access the default constructor and fields can be modified using setters which is a bad design combination.
  • Similarly, if there are more than 5 parameters in a constructor then it is assumed to be a bad class as it is most probably deviating from the Single Responsibility Principle. When using AllArgsConstructor or RequiredArgsConstructor a developer can easily oversight this and code can easily become unmanageable going forward

Some Good Practices

Few things to remember,

  • Lombok is just a tool to make your life easy. It does not provide any assistance or support for clean code.
  • If clean code and design principles are not of importance (trust me it’s not that bad as it sounds in most of the cases), then using Lombok to simplify the development process is the best option.

having said that, let us look into some good coding practices using Lombok

  • Use only the features that are absolutely required
  • If in doubt, always refer to the documentation. It takes less than 5 minutes to read the documentation but 5 days to identify and fix a silly bug

Thanks TO : Mohammed Atif



要查看或添加评论,请登录

Omar Ismail的更多文章

社区洞察

其他会员也浏览了