When I’m having trouble figuring out a code problem, most of the time it’s because I’ve forgotten to pass stuff in.

There’s a fancy term for this: Dependency Injection. But, really, what it’s about is passing stuff in. It’s not that hard a concept.

Let’s say I have a function:

doSomething() {}

Doesn’t matter what, really. I’m just trying to do something. Chances are I need some material to work with to do whatever. So there are two ways I can get what I need to work with:

  1. I can go out and fetch it, or
  2. I can have it on hand the moment I start.

So, I want to make dinner about once a day. There are two ways to go about it.

I could, every time I want to make dinner, go out to the grocery and get the ingredients I need, bring them home and make dinner. Or, I could do a little planning ahead and make a trip to the grocery ahead of time and have everything I need at hand for a week’s worth of meal preparation. Either way, I can get dinner together, but when I have the ingredients at hand, the process is always easier.

Same with code.

I can have my function go out and get what it needs:

doSomething() {
  $a = getSide1();
  $b = getSide2();
  return sqrt($a**2 + $b**2);

Or, I can arrange things so I have everything on hand to do what I need to do:

doSomething($a, $b) {
  return sqrt($a**2 + $b**2);

In either case calculating the hypotenuse happens on the return line, but in the first it takes 3 lines to get there. The second does it in 1.

Moreover, it’s much easier to test the second function. It’s pretty easy to pass in, say, 3 and 4 and know I’m looking for it to return 5. With the first, I have to do a lot more scheming to figure out how to arrange for getSide1() and getSide2() to pull in 3 and 4.

Things can get rapidly more complex when you start dealing with classes.

Say I have a class, Theme{}, that depends on another class, Options{}, to do what it needs to do. One way to go about it is to have the Theme class create an Options class:

public function __construct() {
  $this->options = new Options();

Or I might ask some third class, OptionsFactory, to create the Options for me:

public function __construct() {
  $factory = new OptionsFactory();
  $this->options = $factory::build();

The problem with both of these is I limit my options. And, in the second case, I’m dealing with yet another class and have introduced a chain of dependencies: Theme depends on Options which depends on OptionsFactory. What if I want to use my Theme class with a different set of options? (This is where I often realize I’ve messed up.) Now I have to figure out a way for Theme to get different Options or change the Options it has. If it sounds hard, that’s because it is!

Instead, this is much better:

public function __construct(Options $options) {
  $this->options = $options;

Now I can pass in whatever Options I want, from wherever I want. It doesn’t matter where they come from, so my Theme is no longer dependent on whatever process creates them. Theme is free to do theme stuff without worrying about the rest.

And just like the Pythagorean theorem function above, it’s much easier to test because it’s easy to see exactly how to give it a specific set of Options and evaluate whether the output is what you’d expect it to be.

When I have a code problem, it’s usually because I’ve forgotten to pass stuff in. And when I rework things so that I can pass stuff in, it nearly always cleans up a lot of mess. I highly recommend it.