Should Lombok make people love and hate?

Posted Jun 16, 20204 min read

1 Introduction

Lombok, an island in Indonesia, Lombok. But in the Java world, it is a convenient class library that can provide a lot of convenience, so it is favored by many people. But there are also many objections. Why is this?

Lombok

The sunset taken before going to Lombok.

2 Conveniences provided by Lombok

In general, when we use POJO in Java, it is easy to think of Lombok, such as VO, DTO, DO, etc. To use Lombok, you need to install the corresponding IDE plug-in, and you need to introduce dependencies:

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

For example, if Lombok is not used, the amount of code to implement getter/setter, equals, hashCode, and toString is very large, as shown below:

package com.pkslow.basic.lombok;

import java.util.Objects;

public class Book {
    private String name;
    private int id;
    private double price;
    private String author;
    private String desc;

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public boolean equals(Object o) {
        if(this == o) return true;
        if(o == null || getClass() != o.getClass()) return false;
        Book book =(Book) o;
        return id == book.id &&
                Double.compare(book.price, price) == 0 &&
                Objects.equals(name, book.name) &&
                Objects.equals(author, book.author) &&
                Objects.equals(desc, book.desc);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id, price, author, desc);
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name +'\'' +
                ", id=" + id +
                ", price=" + price +
                ", author='" + author +'\'' +
                ", desc='" + desc +'\'' +
                '}';
    }
}

And most of it is boilerplate code without much actual logic.

If you use Lombok, it is very simple. An annotation @Data can complete the above work(the method on the right can only be displayed after the Lombok plugin is installed):

Common annotations of Lombok are:

@NonNull:Used for short pointer anomaly detection;

@Cleanup:automatic resource shutdown;

@Getter/@Setter:Automatically generate get/set methods;

@ToString:Generate toString method for printing and debugging;

@EqualsAndHashCode:Generate equals and hashCode methods, note that these two should be implemented at the same time;

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor:constructor;

@Data:Equivalent to @ToString+ @EqualsAndHashCode+@Getter+ @Setter +@RequiredArgsConstructor.

@Builder:Generate builder method;

@Log:log related:

@CommonsLog:org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger:com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog:org.jboss.logging.Logger.getLogger(LogExample.class);
@Log:java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j:org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2:org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j:org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j:org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
@CustomLog:com.foo.your.LoggerFactory.createYourLogger(LogExample.class);

So Lombok can really bring us great convenience, reduce a lot of repetitive, no business logic code, the code looks relatively clean; modify the name of the field, do not need to be modified in many places; improve development efficiency.

3 Problems caused by Lombok

Of course, there are also different opinions:

There is no specific code when debugging, which is not convenient for Debug;

Need to force others to install third-party plug-ins, otherwise it will display an error;

When checking the test coverage, it is impossible to visually show which codes are covered and not covered;

Cannot be implemented according to your wishes, such as the toString method, sometimes you need to output a different String format, which is not necessarily implemented according to Lombok;

For some commonly used methods, the IDE can already be automatically generated, and it can be developed efficiently without the need of Lombok.

IDEA does not provide Builder, but it can be used by installing plugins.

After successful installation, the Builder code can be generated:

4 Summary

At the beginning, I supported the use of Lombok. After a period of use and some problems, I still think that the way of automatically generating code through the IDE is more suitable. After all, it is too savage to force others to install plug-ins. When IDEA generates code, others will not report errors if they do not install plug-ins.

In addition, a few more lines of code can reflect the workload, ha ha ha ha ha ha ha...


Welcome to Pumpkin slow talk www.pkslow.com for more exciting articles!

Welcome to pay attention to the WeChat public account <Pumpkin slow talk>, will continue to update you...

Read more and share more; write more and organize more.