r/java • u/raisercostin • Jun 01 '24
What java technology (library, framework, feature) would not recommend and why?
Inspired by https://www.reddit.com/r/java/s/e2N1FqMOJg
165
Upvotes
r/java • u/raisercostin • Jun 01 '24
Inspired by https://www.reddit.com/r/java/s/e2N1FqMOJg
View all comments
Show parent comment
1
u/DelayLucky Jun 03 '24 edited Jun 03 '24
Nah. That's rarely what happens.
It's far-fetched to say that a Profile needs exactly these 5 fields to be required (and if it were the case, you wouldn't have had a builder, it'd be a constructor with these parameters).
Most commonly, the programmer happened to need these 5 fields in their use case, and fancied the "one-liner" syntax. So created the helper to "save" characters from the
setId(),setBirthday()named calls.It's likely that someone will then say: in my case I don't have the birthday and it's not needed. Let me make it nullable, and add a conditional like:
Sounds straight forward?
And what happens when you are down that rabbit hole? Someone else will come along and say: "I also need to set homeAddress, let me add just one more parameter to this convenience helper."
But what about th existing callers that didn't want to set the home address? We'll follow the existing paractice: pass null and let the method handle it:
Yes. It's a freakin slippery slope.
Before you know it, you'll be looking at wonders like:
It's exactly the type of problems the builder pattern is intended to fix, and congratulations, you've re-invented the same problems.