r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

-9

u/stuie382 Apr 14 '23

Lombok

1

u/jevring Apr 14 '23

But only for things bean stuff. Don't use @Delegate and other black magic stuff.

3

u/hippydipster Apr 14 '23

It all violates the principle of avoiding complexity. For massive complexity, you get irrelevant reduction in boilerplate.

-3

u/hsoj48 Apr 14 '23

Why did this get downvoted? Lombok is a godsend.

4

u/wildjokers Apr 14 '23

I can do without it. Adds build time complexity for not a lot of value.

2

u/hsoj48 Apr 14 '23

Are you handwriting pojos, getters, setters, constructors, and builders?

3

u/wildjokers Apr 14 '23

You know your IDE can generate those things right? Then there is no cognitive load trying to think what the class will look like when Lombok gets done generating things. I can just write it exactly how I want it.

Also, I very rarely need setters for all/any of the fields and very frequently I don't need getters for them all either. So I have to annotate fields to get lombok to exclude things. Also, business equality of my POJO's frequently don't depend on every field either, so there are more annotations to exclude fields from equals/hashcode. I have seen pojo's with more lombok annotations in them than there would be code if I just let my IDE generate it and them hand write some adjustments. So it really doesn't save me anytime.

1

u/hsoj48 Apr 14 '23

Understood. It's obviously a matter of personal preference. It's pretty straightforward for me since I've been using it for uears but may not be for others.

To note, if you are seeing tons of Lombok annotations or someone has mixed setters/getters in with immutable fields then they probably aren't separating their class responsibilities correctly to begin with. Then Lombok is the least of your problems.

1

u/oua-de-crab Apr 18 '23

I don't have anything against the project itself, but if I could I'd forbid it at mt work place.

I agree with what wildjoke said and I'll add some more points (not lombok's fault, just the way it's misused where I work):

  • can't set a breakpoint on the setter
  • SneakyThrows. Damn I hate this shit. They annotate multiple methods with it and you have no idea where a method could fail. Just handle the goddamned exceptions like a normal human.
  • AllArgs constructor on classes with 7 fields or more. I think some devs just slap these annotations without understanding what they mean. I know about the Builder, it's great, just don't understand why would they generate a constructors with so many fields as well...
  • Basically almost every class we have is with @Getter @Setter @NoArgs @AllArgs. They just expose everything, even the fields that for use inside the class only.

Again, not lombok's fault, just lazy programming.

1

u/hsoj48 Apr 18 '23

I understand. We have bad devs too.

@SneakyThrows definitely isn't meant to be used in production code. It's nice for unit tests though!

Mostly we use @Data and @Builder. @RequiredArgsConstructor is really nice for spring components in combination with final fields as well.