Lombok: To use or not to use, that is the blog

If you use then how to use, that is also the blogProject Lombok is a Java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a f…


This content originally appeared on Level Up Coding - Medium and was authored by Mohammed Atif

If you use then how to use, that is also the blog

project lombok

Project Lombok is a Java library that automatically plugs into your editor and build 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 a support of generating the boiler plate 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 about using Lombok in their Application. So it becomes important to discuss about 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 with 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 whole project will be in trouble.

  1. But that’s not the case, since Lombok comes with DeLombok tool, even if you have to remove Lombok from your application you can easily DeLombok it and remove the dependency from Application.
  2. Since all the dependency to Lombok is unidirectional i.e. classes depends 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 impact 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. Project is for a well established organisation 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 rest of the team members eventually pick up the good practices.

For case 2, little bit of bad code is anyways acceptable. Team will eventually learn going forward and improve the overall quality gradually.

For case 3, it becomes a call for team, depending on previous experience of team members on Lombok and 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 detailed implemented project 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 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 most commonly used Annotation, but it is 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 it to a set but since @Data adds EqualsAndHashCode too, set inserts only one value because the contents of the objects are 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 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 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 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 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 the documentation. It takes less than 5 minutes to read the documentation but 5 days to identify and fix a silly bug

Find detailed implementation here : https://github.com/mohammed-atif/medium-project-lombok


Lombok: To use or not to use, that is the blog was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Mohammed Atif


Print Share Comment Cite Upload Translate Updates
APA

Mohammed Atif | Sciencx (2021-10-08T07:21:30+00:00) Lombok: To use or not to use, that is the blog. Retrieved from https://www.scien.cx/2021/10/08/lombok-to-use-or-not-to-use-that-is-the-blog/

MLA
" » Lombok: To use or not to use, that is the blog." Mohammed Atif | Sciencx - Friday October 8, 2021, https://www.scien.cx/2021/10/08/lombok-to-use-or-not-to-use-that-is-the-blog/
HARVARD
Mohammed Atif | Sciencx Friday October 8, 2021 » Lombok: To use or not to use, that is the blog., viewed ,<https://www.scien.cx/2021/10/08/lombok-to-use-or-not-to-use-that-is-the-blog/>
VANCOUVER
Mohammed Atif | Sciencx - » Lombok: To use or not to use, that is the blog. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/08/lombok-to-use-or-not-to-use-that-is-the-blog/
CHICAGO
" » Lombok: To use or not to use, that is the blog." Mohammed Atif | Sciencx - Accessed . https://www.scien.cx/2021/10/08/lombok-to-use-or-not-to-use-that-is-the-blog/
IEEE
" » Lombok: To use or not to use, that is the blog." Mohammed Atif | Sciencx [Online]. Available: https://www.scien.cx/2021/10/08/lombok-to-use-or-not-to-use-that-is-the-blog/. [Accessed: ]
rf:citation
» Lombok: To use or not to use, that is the blog | Mohammed Atif | Sciencx | https://www.scien.cx/2021/10/08/lombok-to-use-or-not-to-use-that-is-the-blog/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.