Getters and Setters begone!

Boilerplate code – pain in the… neck for every developer and a major struggle for every programming language that wants to look sexy and appealing. One of the sources of this problem are the get and set methods for the properties of any class. The problem is most noticeable in POJO classes. Imagine you have a User class:

package course;

public class User {
    private String name;
    private Integer age;
    private String Address;
    private String martialStatus;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    public String getMartialStatus() {
        return martialStatus;
    }

    public void setMartialStatus(String martialStatus) {
        this.martialStatus = martialStatus;
    }
}

Look at this madness, for 4 field I had to write 50 lines of code and for each new field that I introduce, the size of the class will continue to grow exponentially. From what I know, developers are a lazy species and we don’t like wasting time on trivial things when we could be exploring new, innovative solutions that were released.

How do we get rid of this boring task that we are bound to do and repeat over and over again? Luckily, some IDEs have solved this for us. For example, in IntelliJ IDEA with pressing Alt+Insert you can ask the editor to generate all these methods for you so you don’t waste time doing it yourself. However, this is not a complete solution as the code is still there to interfere with the reader and prevent him from seeing what is really important for him.

The solution I found most convenient for Java is the Lombok project. It is a java library that automatically plugs into your editor and build tools, spicing up your java. By using annotations it will generate the code for you at run time. Never write another getter or equals method again. Remember that User class from earlier? This is how it would look like with Lombok:

@Data
public class User {
    private String name;
    private Integer age;
    private String Address;
    private String martialStatus;    
}

I hope this post helped you. Enjoy!

3 thoughts on “Getters and Setters begone!

  1. Great article! My favorite blogger!! Much love ❤ <# <№.
    Please write some articles about Kotlin and Android.
    And also try using scala. No getters, no setters, everything is constant. And you don't need 3rd party lib.

    Like

  2. Hello Icakis. Thank you for your support, it’s highly appreciated.
    Yes, Kotlin and Scala are very hot topics right now and I’m definitely going to share some thoughts on them. As a matter of fact, I’m already doing a Kotlin course and as soon as I get a nice grip of it, I’ll make a post about it. Small spoiler – things look promising 🙂

    Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s