Write less with Lombok

I’m not a fan of code generators,usually I simply hate them but lately, working with Project Lombok and especially writing Java code after a lot of Kotlin, I must confess I grown to tolerate this little library.

People who hate Java, usually invoke its verbosity as on of the main reasons. Unfortunately, they are right, Java is sadly a really verbose and ugly language. Imagine a simple data class you want to have, to store some information:


public class Student {
    private String lastName;
    private String firstName;
    private Integer grade;

    public String getLastName() {
        return lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public Integer getGrade() {
        return age;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setGrade(Integer grade) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PersonSimple that = (PersonSimple) o;
        return Objects.equals(lastName, that.lastName) &&
                Objects.equals(firstName, that.firstName) &&
                Objects.equals(grade, that.grade);
    }

    @Override
    public int hashCode() {
        return Objects.hash(lastName, firstName, grade);
    }

    @Override
    public String toString() {
        return "Student{" +
                "lastName='" + lastName + '\'' +
                ", firstName='" + firstName + '\'' +
                ", grade=" + grade+
                '}';
    }
}

While this code can be mostly generated by any decent IDE, it’s just sad. The first problem is that it’s just noise! The second problem is that every time you change the members of the class you need to maintain the noise. Yeacs.. Now the same example using Lombok annotations:

@ToString
@EqualsAndHashCode
@Getter
@Setter
public class Student{
    private String lastName;
    private String firstName;
    private Integer grade;
}

It’s basically the same logic but in a more concise and elegant way. The only problem is that in order to use Lombok you need some kind of builder (Maven or Gradle) and you also need to install the Lombok plugin in your IDE. Don’t worry, all major Java IDEs: Eclipse, Net Beans and IntelliJ have great support for it. Anyway to read about all the sugar it provides, check out the official website.

pancakes-282222_640

And now, the Pièce De Résistance, the same class in pure Kotlin, without any magical library:

data class Student(var lastName: String, var firstName: String, var grade: Int)

Now that’s what I call sweet as a warm pancake filled with Nutella and bananas 😎 .

If you found this post useful, please feel free to share and give it a LIKE , so others can find it too! Also if you want more content like this please hit the FOLLOW button. Thanks 😊

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s