Speed up your Java coding using Lombok

Java has a bad side that everybody hates , what is it?

A lot of boilerplate code is the same for most of the applications we write.

Like what? 🧐

  • Getters
  • Setters
  • Constructors
  • Builder pattern
  • and many more…

Wouldn’t it be nice if someone took care of that work for us?
That is where Lombok introduces itself.

What does it do?

It generates the byte code for those common tasks (Getters, Setters, etc..) and puts them in our .class files which make them usable by the code we write.

How?

We need to add the Lombok dependency to our Maven build.
Then, we just need to annotate the wanted classes, fields with some Lombok annotations.

Let’s look at some code! ‍

No Lombok 🧟

public class Human {  
    private int id;  
    private String name;  
    private int ageInYears;  

    public Human() { }  

    public Human(int id, String name, int ageInYears) {  
        this.id = id;  
        this.name = name;  
        this.ageInYears = ageInYears;  
    }  

    public int getId() {  
        return id;  
    }  

    public String getName() {  
        return name;  
    }  

    public int getAgeInYears() {  
        return ageInYears;  
    }  

    public void setName(String name) {  
        this.name = name;  
    }  

    public void setAgeInYears(int ageInYears) {  
        this.ageInYears = ageInYears;  
    }  

    // Builder pattern 
  public Human id(int id){  
        this.id = id;  
        return this;  
    }  
    public Human name(String name){  
        this.name = name;  
        return this;  
    }  
    public Human ageInYears(int ageInYears){  
        this.ageInYears = ageInYears;  
        return this;  
    }  

    @Override  
  public String toString(){  
        return String.format("Human(id=%s, name=%s, ageInYears=%s)",  
                this.id, this.name, this.ageInYears);  
    }  
}

Enter fullscreen mode Exit fullscreen mode

Lombok 🤴

import lombok.*;  
@Getter @Setter  
@AllArgsConstructor @NoArgsConstructor  
@Builder @ToString  
public class Human {  
    @Setter(AccessLevel.NONE)  
    private int id;  
    private String name;  
    private int ageInYears;  
}

Enter fullscreen mode Exit fullscreen mode

The Maven dependency looks like this

<dependency>  
    <groupId>org.projectlombok</groupId>  
    <artifactId>lombok</artifactId>  
    <version>1.18.16</version>  
    <scope>provided</scope>  
</dependency>

Enter fullscreen mode Exit fullscreen mode

Note: the version part will change depending on the last release of Lombok.

What just happened? ️‍️ ️

  • we used annotations to generate
    • Getters
    • Setters
    • Constructors
    • Builder pattern
    • toString implementation

We can also make tweaks on some annotations, like the setter for the id field.

  • We set its access level to None, meaning that we don’t a setter to be generated for it.

Was it worth it?

We wrote 52 lines when not using Lombok.
We wrote 8 lines when using Lombok.

Lombok helped us shrink our code size by nearly 4 times. Those numbers can go up even more when having more variables in our classes.

Lombok has a lot more annotations that offer a ton of help, to view them all visit their website.

Conclusion

Lombok helps us focus on our business code and not worry about the little details of getters/setters/constructors/common design patterns and other Java constructs.

Code ‍

GitHub

原文链接:Speed up your Java coding using Lombok

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容