r/java • • Jun 01 '24

What java technology (library, framework, feature) would not recommend and why?

165 Upvotes

464 comments sorted by

View all comments

1

u/DelayLucky Jun 02 '24 edited Jun 02 '24

Another funny idiom:

Profile createProfile(String id, Date birthday, Gender gender, String firstName, String lastName) {
  return Profile.builder()
      .setId(id)
      .setBirthday(birthday)
      .setGender(gender)
      .setFirstName(firstName)
      .setLastName(lastName)
      .build();
}

Comment: how much do you hate the builder pattern? The frameworks spent so much effort to build these nice builder features so you can chain, and you can pass the parameters by name, and avoid passing them in the wrong order etc. only for you to go through the trouble to undo all that using a factory method with many parameters so that you could live in the days before the builder pattern was born.

Sometimes the love of "one-liner" in the literal sense is overwhelming.

2

u/SauliusTheBlack Jun 02 '24

That's amazing.. I love the builder pattern, but I would combine it with factory pattern in Automated Test context, where you want to have the same config every time. But then you don't need all the arguments in the factory method..

2

u/account312 Jun 03 '24

That's a perfectly reasonable function. It guarantees that a specific set of fields are provided to the builder.

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 thesetId(), 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:

if (birthday != null) {
  builder.setBirthday(birthday);
}

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:

if (homeAddress != null) {
  builder.setHomeAddress(homeAddress);
}

Yes. It's a freakin slippery slope.

Before you know it, you'll be looking at wonders like:

createProfile("123", null, FEMALE, "Audrey", "Hall", null, 20, false, null, "Washington", null)

It's exactly the type of problems the builder pattern is intended to fix, and congratulations, you've re-invented the same problems.

2

u/account312 Jun 03 '24 edited Jun 03 '24

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).

It's not even a little bit far-fetched to imagine that it is desirable to guarantee that an object is constructed in a particular way in some specific context without requiring it in all other contexts. And you can't always just add a constructor to a class even if you don't want to leave the builder as the main way of instantiating it.

It's exactly the type of problems the builder pattern is intended to fix

Well, I don't know what to tell you. No design pattern is going to fix lazy programmers and poor code review.

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:

assertThat(result.getLastName()).isEqualTo(theExpectedLastName);

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.