Explain Codes LogoExplain Codes Logo

Omitting one Setter/Getter in Lombok

java
lombok
getters
setters
Anton ShumikhinbyAnton Shumikhin·Aug 10, 2024
TLDR

If the motto of Lombok is "less code, more time for coffee", then using @Getter(AccessLevel.NONE) or @Setter(AccessLevel.NONE) is living the dream: cutting down on "boilerplate" code, and being specific about what accessors are generated

@Getter @Setter public class Sample { private String keepAccessor; // Open to all, like free coffee at an office @Setter(AccessLevel.NONE) // No touching! Like my precious espresso machine private String noSetter; @Getter(AccessLevel.NONE) // 'Shrooms? No peeking under this carpet! private String noGetter; }

With this making explicit access choices, we effectively say: "No setter, please" for noSetter, and "No getter, thank you" for noGetter, letting Lombok do the rest.

Why control access with Lombok?

In an ideal JavaBean world, we'd often want every field to have a getter and setter. But our code isn't always ideal (nor are our coffee blends!). There are cases when you might want to go against the tide:

  • Immutable fields: These fields are like our inborn coffee preferences. Once set, they never change.
  • Calculated fields: It's like asking coffee beans to explain the magic of becoming coffee. These fields calculate their values dynamically, making setters ineffectual.
  • Sensitive data: Some things are better off hidden, like the sugar you snuck into your perfect espresso.

The 'Strong Espresso' Plan: Highly Customized Access Levels

Lombok lets you be the barista. You can determine the right strength for each field using @Getter and @Setter annotations. This allows you to operate independently of the @Data pomp and circumstance by defining which fields are accessible and to what degree.

Sip Different: Advanced Use Cases

With Lombok's @Data annotation, your code can savor unique flavors:

  • Hot Coffee: Develop features faster by conditionally suppressing accessors during development stages. Then, add granulated sugar with profile-driven Lombok configurations for your production blend.
  • Cappuccino Coding: Enhance Builder and Factory patterns. Omitting some setters clears out the froth and ensures smooth brews.
  • Espresso Shot: When dealing with class hierarchies, use Lombok to ensure that subclass modifications to fields are kept bitter-free.

Behind the Counter: Potential Complexities

Using Lombok feels like owning a high-tech coffee machine. While it's efficient and time-saving, it doesn't grant automatic barista skills. So beware:

  • Serialization: The grumpy police officer of Java. If your getters go on strike, it might spit your objects out.
  • Framework compatibility: Some stick to traditional JavaBeans conventions like a caffeine-seeker sticks to his morning brew. Laying off accessors might make them grumble.
  • IDE integration: As with coffee machines, cleaning, updating, and maintaining your IDE's Lombok plugin will help perfect your codebrew.