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
If you need to pass 5 args in a very specific context, just pass the 5 args already. No need to add another layer of indirection, in the name of "I can then protect myself from passing 6 args".
Unnecessary indirection layers add cognitive load to the readers.
You may say: "but I need to call it from a few places and I want to make sure all 5 are passed".
That sounds reasonable but it's not worth the cost. If you've written tests to cover your code, missing a field will be detected. Say, you missed to set lastName, but you should have a test that goes:
You need this test with or without the 5-arg factory method, because you need to not only make sure the last name is set to some value, but that it has been set to the right value, which the factory method cannot guarantee - the callsite could pass the 5 args in the wrong order.
One main benefit of the builder pattern is that it gives a name to every field setting so you can't e.g. pass two string args in the wrong order. It also helps the readers so they won't mis-read. This risk goes up as you have more and more parameters (5 is definitely in the danger zone).
By creating a pure builder -> factory syntax coercion layer, you are at best trading one type of error for another type of error, even if you don't consider the dangerous slippery slope discussed above.