Skip to content

Reducing boilerplate code with Lombok

Often when we are building applications, there is the need to build POJO’s classes and with this type classes, also comes a lot of boilerplate code for Getters and Setters. Also using the builder design pattern can add a lot of code.
Project Lombok provides a set of useful annotations that replace Getters and Setters, constructors and makes easy to use the builder design pattern.

Example without Lombok

Here we have a sample Builder class (extracted from Effective Java – Third Edition) with six attributes.

public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    public static class Builder {
        // Required parameters
        private final int servingSize;
        private final int servings;

        // Optional Parameters
        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        public Builder(int servingSize, int servings) {
            this.servings = servings;
            this.servingSize = servingSize;
        }

        public Builder calories(int val) {
            this.calories = val;
            return this;
        }

        public Builder fat(int val) {
            this.fat = val;
            return this;
        }

        public Builder sodium(int val) {
            this.sodium = val;
            return this;
        }
        public Builder carbohydrate(int val) {
            this.carbohydrate = val;
            return this;
        }

        public NutritionFacts build() {
            return new NutritionFacts(this);
        }
    }

    private NutritionFacts(Builder builder) {
        servingSize = builder.servingSize;
        servings = builder.servings;
        calories = builder.calories;
        fat = builder.fat;
        sodium = builder.sodium;
        carbohydrate = builder.carbohydrate;
    }
}

In the above example, we can how much code is needed to construct a Builder class

Example with Lombok

In order to use Lombok in your application, you have to add the dependency to your pom.xml:

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.8</version>
</dependency>

Install Lombok plugin

Before starting developing on your IDE, you need to install the Lombok plugin. Follow these steps to install on IntelliJ:

  1. Click on IntelliJ IDEA
  2. Go to Preferences > Plugins
  3. Click on Browse Repositories
  4. Search for Lombok
  5. Click on Install

Now let’s see the NutritionFacts class with the same attributes but using Lombok

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class NutritionFactsLombok {
    private int servingSize;
    private int servings;
    private int calories;
    private int fat;
    private int sodium;
    private int carbohydrate;
}

The above example provides a Builder class with a no args constructor, all args constructor and the toString and hashCode methods.
Explaining the annotations:

  • @Data: adds the toString(), equals(), hashCode(), getters on all fields, setters on all non-final fields and required args constructor
  • @Builder: adds support for easy object creation through builder pattern
  • @NoArgsConstructor: as the name suggests, adds a constructor without arguments
  • @AllArgsConstructor: as the name suggests, add a constructor with all attributes

Let’s the two examples usage:

import builder.NutritionFacts.Builder;

public class Main {

    public static void main(String[] args) {
        NutritionFacts cocaCola = new Builder(240, 8)
                .calories(100).sodium(35).carbohydrate(27).build();

        NutritionFactsLombok cocaCola2 = new 
            NutritionFactsLombok.NutritionFactsLombokBuilder()          
 .servingSize(240).servings(8).calories(100).sodium(35).carbohydrate(27).build();
    }
}

On the first example, the class is initializing the class with vanilla Java. On the second, the class is initialized using Lombok. As we can see the initialization are quite similar, but on the second example less code as used.

Further readings

In this post I showed only some of Project Lombok capabilities. You can achieve much more productivity with other provided features. For more information about all you can achieve, visit their documentation.

Published inJava

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *