3

Which of the following two styles of instantiating a class and invoking methods is preferable? The first style is more concise but I found it hard to read the first time I encountered it as I had not previously seen it in any sample code.

Repository.QueryBuilder accountQuery = new Repository.QueryBuilder()
      .selectFields(REQUESTED_FIELDS)
      .whereSpecification(getAccountByCorpIDSpec);

or

Repository.QueryBuilder accountQuery = new Repository.QueryBuilder();
      accountQuery.selectFields(REQUESTED_FIELDS);
      accountQuery.whereSpecification(getAccountByCorpIDSpec);
2

1 Answer 1

4

The first form is generally preferred by experienced developers. While it may be "confusing" or "harder to read" for someone with little experience in the language, or programming in general, once understood, the conciseness helps reduce cognitive load in the long run. It's easier/less "boring" to read, takes less characters to type, and when done well, flows beautifully.

Consider this example, a fictional children's tale about Alice:

Alice went to the store. Alice bought some milk. Alice bought some eggs. Alice bought some bacon. Alice bought some cookies. Alice bought some some pickles. Alice bought smoe tomatoes. Alice bought some cereal. Alice went home.

And now, written concisely:

Alice went to the store, bought some milk, eggs, bacon, cookies, pickles, tomatoes, and cereal, then went home.

You may have felt your brain starting to "tune out" Alice in the first story; it is long and boring, and minds don't like to do boring. If the story were too long, and written this way, you might actually accidentally miss some small detail because of how our brains work. Maybe a typo, or an extra word, might have slipped in, and casual reading might completely miss it.

Similarly, the builder pattern skips the boring parts and gets straight to the information. The builder pattern does what your brain generally does automatically when there's too much repeated data, which is to say, it filters it out so our brains can focus on the important details. By not providing any "boring" details, it can help make subtle mistakes more obvious, because your brain is more engaged in the code, and less on filtering out all the extra "noise."

Of course, there is such thing as going too far with something; builder patterns are not typically well-suited for a small, well defined object with only a few configuration options, but for large objects, like a Query, the builder can help enforce security controls, field level access checks, and other things behind the scene, as well as allow for highly dynamic query building.

It certainly exists in many frameworks, like jQuery, and languages, like JavaScript, often in extreme doses, so you may want to learn how to at least read this, because you'll not be able to understand a lot of the code out there until you're familiar with the builder pattern and the concept of method chaining; or, alternatively, reading a lot of code should help you learn this very quickly.

So, in summary, I would reiterate that the first form is likely preferred by more experienced developers. That said, I'm also fairly sure that not all experienced developers prefer it; after all, preferences are subjective. However, I hope that I've at least provided some relatively objective reasoning why one should want to learn how to read, and use, the builder pattern in appropriate scenarios.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .