Use Supplier Or Supplied Or Both?

A coding pattern; replace supplied data with a supplier or a supplier with the supplied data.

It is very common to create code with an interface like this:

do( Data supplied )

We then use the data somehow to perform our functionality, whatever a do(…) method does.

On the other hand, sometimes we create code with an interface like this:

do( DataSource supplier )

And its body is basically the same as our starting chunk, but with a prolog that asks the DataSource for the Data and then performs whatever do(…) does.

So when we sketch a new function, we always have to mull over which of these two is the right thing to do. And when we refactor, similarly, we have to wonder which of these two ways to go. There is not a "right" choice you should always make. Sometimes the supplied is the way to go, sometimes the supplier. But there is a right thing you should do, and that is to pay attention to the decision, as it has a variety of potential consequences.

Here are some notions to mull over that might help you make your decision.

First, consider that you can do both. You can write:

do( Data supplied )

and

do( DataSource supplier )

(I’ve assumed overloading, but you don’t need it, just change one of the names.)

Just have the supplier version contain the prologue we discussed and chain into the supplied version.

Note: Please chain. Don’t just copy the body of one into the other. Doing that is purposefully injecting anti-change in to your codebase.

Second, if you don’t do both, make sure that whichever one you do is the least awkward collaboration for you to test with. Remember "awkwardness"? This is any property of a collaboration that makes it annoying for us to test whatever branching logic do(…) performs.

In Java, for instance, streams can come from anywhere including files, but files can only come from a drive. If I have to write and maintain a bunch of files "somewhere else" to do my testing, well, that’s awkward. And when we have awkward tests, we don’t write/run/debug them.

Third, regardless of picking one or both, is the object you’re passing in far fatter in interface than what do(…) needs to do its work? You might want to wonder if you need an intermediate object, one that is neither the supplier nor the supply, quite, but is a thin wrapper.

The business of passing around obese interfaces is extremely common in "it rubs the database on its browser" code, it dramatically increases the mental scope required to grasp and change the method. A typical case: passing a database connection (and an account id) when do(…) really just needs an account’s zipcode. You do understand that your database connection aims at a turing complete system, yes? It can do anything a computer can do. This issue isn’t just limited to the supplier choice. We do the same thing with supplied’s. Again, you just want the zipcode. An account, whether databased or in RAM, has dozens of fields and operations.

You might think introducing an intermediate object is dumb, because even tho you could perform any of an account’s operations, you’re only really going to ask for zipcode, so why go to the trouble?

(ahhhhhh. Here we strike at the very heart of the strictly technical aspects of the modern software development synthesis. Change-enabling code is nearly always more valuable than shipping the next change. So begins another muse.)

The short answer is still twofold.

  1. Cuz code that only gets the zipcode *must* be dead simple, and dead simple makes us go faster.
  2. When we realize we also need a) a state and b) a method to validate that they’re correct, we have a ready place to put it.

So. When you sketch a method or approach refactoring that method, consider your choices around passing supplier vs supplied vs both. Consider both, consider awkwardness, consider interface width.

There’s no right answer every time, but it’s almost always worth taking a minute and mulling it over.

Go geek out, then.

Want new posts straight to your inbox once-a-week?
Scroll to Top