I hate getters and setters

I hate getters and setters in java. Half of my code is filled with them. They are pointless boilerplate code that says nearly nothing. 99.9% (I made that number up, but it sounds reasonable) percent of the cases they don't have any additional code in them.

And it is really hard to see whether a variable is read-only/read-write/write-only. You have to find the getters/setters to see that.

I have a notation where I put all the modifiers at the end of the class and separate them with a page of whitespace.

I don't like the C# solution - it's not really shorter, only binds the getter and setter together.

I have a proposition: use annotations:

    @Getter @Setter
    private boolean subscribedForEmailNotification;

And one wants to write extra code, one should be able to use the old notation. The compiler will check for collisions.

I don't see any reason why this cannot become the next syntactic sugar in java where one saves a bunch of useless code-writing.

8 thoughts on “I hate getters and setters”

  1. Hi Misho,

    Other way is to encapsulate the getters and setters in aspects, e.g. when Spring Roo creates an entity it separates its getters and setters in an aspect which is woven to the class at runtime.

    Cheers,
    Nikolay

    1. I would like a solution on the POJO level. Everyone can set up a solution for his/hers own framework (unfortunately it will not be universal), but only SUN (now Oracle) can fix that the right way 🙂

        1. It is interesting, however, how does that work and whether it has some side effects.

          1. No idea, Misho 🙂 Just stumbled on it after a quick search. That's why I added the stackoverflow discussion on the topic - in it the people are speaking about Lombok's pros and cons apart of the whole discussion 🙂

    1. My Ruby experience equals my rocket science experience. Anyway seems to be exactly what I want - getters and setters without any code.

Leave a Reply

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.

This site uses Akismet to reduce spam. Learn how your comment data is processed.