Inject This!

Dependencies or Codependencies?

Before I start this discussion I want to refer you to my previous rambling, This is My Tool as it may keep your head from exploding. 

There are many tools that, while founded with good intention, create more messes than they solve. This article will look at dependency injection (DI). In my experience, I have never seen a project that uses DI that was not a complicated, horrific mess.

I will attempt to catalogue how these messes manifest, reasons they seem to be inevitable and solutions/patterns to prevent their occurrence.

What is it

To be honest I never considered dependency injection to be a thing in and of itself. I use it frequently as part of the Inversion of Control (IOC) pattern by injecting concrete instances of an abstract class into an object through its constructor.

This is incredibly useful for testing code that accesses external resources, abstracting dependencies so you can switch out implementations. For example, it is very useful to be able to swap Google AI, ChatGPT, OpenAI, etc. during development and testing.

With emergence of Spring Boot, Lombok, etc. it seems to have taken a life  of its own. Teams use these frameworks to ‘wire up’ all objects. The results are frequently horrifying.

The intent (no free lunch)

The intent of DI is pretty straight forward: to decouple the construction of a needed object from its use. 

I find the misuse of DI is invariably linked to one problem. Programmers never stop to ask the question: “Now why in tarnation would I want to do that?” They see it as a cool new tool (refer to the aforementioned article) and use it everywhere.

They forget that all tools have overhead. Does the savings from using this tool overcome this cost or are you simply adding more to the accidental complexity of your project.

The costs

I find two main costs of DI. First, you typically don’t know your type until runtime and second, you don’t know life cycle of the object you are using. This makes maintaining state rather difficult.  These costs are a direct result of the tool itself. 

Arthur Riel, in his seminal work “Object Oriented Design Heuristics’ described how objects get the objects they use. One of the five ways was “God gives it to you”. This is the problem. Can you trust the god who gave it to you. I mean, which god Thor or Loki?

One project I worked on, no-one remembered how a particular set of objects were being injected. It was problematic because it was unknown whether the calls to these objects were synchronous or asynchronous. It took me days to figure it out. After searching through config files, configuration classes, under my desk (I mean it had to come from somewhere) I launched the debugger to see what concrete class was being injected, then reversed engineered where it came from.

If we had simply instantiated the class and passed it through the constructor, this search would have taken seconds.

Why

I had to go through several projects to figure out why anyone would use this. I mean there is a compelling argument to use spring boot or similar to set up a web service or bind a db implementation. And as long as you keep the bindings simple and minimal there is no significant overhead. 

Interestingly, in every project that used DI before I got on it, the framework was used everywhere. These projects were ungainly, overly complex, buggy and unscalable.

Why would anyone do this to themselves? I can think of two reasons.

First, many developers like the declarative nature of the DI tools. They seem to think that annotations to sew things together is simpler, hence it goes everywhere. The trouble is that declarative languages do not scale in terms of features. 

It might be fine when you can remember all of the objects, but as the project grows it becomes impossible to track all of the relationships.

Second, many people don’t understand the principals of object oriented design. I had one developer tell me that he did not like OOD because object structures are tall, skinny, deep things. When you instantiate and object it creates objects that creates objects. Using DI he was able to expose all of the objects.

The fundamental simplifying principle of OOD is containment. Successfully calling a constructor means that that object and all of the contained objects are valid and ready to chose. These DI tools blow away this assumption and condition.

This leads to exponential costs of change. I don’t care about, within reason, the cost of implementing the first few features. The real question is how expensive is the thousandth or the ten thousandth. The only reason we design / architect is to keep the cost of change flat. 

DI tools are at odds with this goal. It is essential that if you are using DI it is forced to the outside of you business logic. The layer it lives in will suffer from all of the above effects, but if it is small it will be useable.

Feedback Loops and Pristine Myths

The Reaper Will Come

I was originally writing an article that looks at the time and costs for engineering decisions. Addressing both the immediate and long team effects.It will follow on the heels of this one. However, I noticed that I needed to introduce the idea of feedback and its longterm effects. Hence, this article was born.

My mother used to say, “It is not a question of if the reaper will come but when he will come and how much will he reap.” Yes, she was amazing, but this one quote has always guided my life and career.

Guided by mother’s tail of the reaper, I approach any decision in software engineering I ask the question: “How much will this decision cost us and how much will it cost to reverse it”.

People constantly tell me this is a cynical view. In reality, it is a pragmatic approach to couching each decision in consequences. 

Originally I used to pull effects from my bum but then I found “interaction diagrams”. These allowed me to discuss cause/effect with teams1. Not perfect but a visualization to your decisions and their effect.

This is an unfortunate example of how local optimizations caused the demise of a promising startup.

I was VP of Engineering for a startup in a highly volatile space. I was hired to revive a team going into a first round of funding. I catalogued the problems but got the go ahead when their live site went down for 3 days during a board meeting.

My team spent a month implementing CICD with full automated regression tests. We got to 4 to 8 releases a day with zero defects. Note we used new features to drive these changes. 

The end to end build to stage with tests took less than ten minutes. The regression tests making solid releases gave the company a distinct advantage in a volatile market.

We had a solid, evolving platform for a few months. At a staff meeting two team leads clamored for long lived GIT branches for “reasons” at a staff meeting. I am using “reasons” here derogatorily because they really had none: it was basically an appeal to emotion. It was essential I isolated how much it will cost and try to see if we got value from it.

Apparently, the teams were worried about code that was not correct getting into the branch. I asked, 

“Why would you check in broken code?”

Of course I got some guffaws and wails claiming I was attacking them. I mean, honestly, you should ever check in broken code? How is it possibly a good thing to share knowingly damaged code? So I switched to how much will this cost us.

I did the sequence diagram that showed the result of the long lived branches and that because of merge/integration times people would tend to go longer between pulls from and pushes to master which created a feed back loop making integrations more painful. There are actually a few feed back loops including big merges makes pulls from master longer because it breaks things, and multiple side branches because the long lived branch has become unstable dilates the full integration more.

I have been in companies where the integration cycle from these long lived branches takes months(!)… yah months.

Internally my main fear was that the reason for long lived branches was a lack of development hygiene on the part of the mid managers, tech leads and developers. Long lived branches just hide an obfuscate the lack of discipline.

Fast forward 4 weeks: All progress stopped (deployment/integration times were now weeks long).  developer asked why it was so painful to merge when we used to do it effortlessly. 

My only consolation, since the company was going down hard, was I got a kick ass “I told you so” moment when I brought up the original interaction diagram to answer the developer’s question and his response was (bless him), “Why did we do that?”

I think the disillusion that longed lived branches comes from Linus and his rightful demand that master branch of the Linux kernel should be pristine.

I will lean on Linus and say all checkins should be pristine. Indeed all code should be as pristine as possible at all times (generally I use15 to thirty minute intervals). The real question is: 

“How do you define pristine?”

In the Linux case, Linus had an amazing ability to keep the entire kernel in his head, so coxs 55de that passed his scrutiny is pristine.

For us mere mortals, the only z

We should demand ‘pristine’ branches, but they should be