Dependency Injection benefits
Modular approach makes life easier for developers, and projects more robust for users.
Developers often manage to do the seemingly impossible, utilising a whole array of tricks and techniques.
Two of our developers, Ian Tucker and Ben Clark, have been explaining how one such technique our developers use make the development process simpler, flexible, and more easily tested. It’s called Dependency Injection, and it’s both deceptively simple and slightly magic.
Dependency Injection is a programming practice that allows developers to easily redefine where software components get the data they need to function. This can be from external sources such as a database, or information from other elements within the software itself. That’s a dependency – the software is dependent on those data sources. If they’re moved or fundamentally altered, it would cause huge problems.
However, if the software is written using Dependency Injection, the developers have made sure that it’s only the data the software needs. The location of the data, and how it’s produced, doesn’t matter as much – the dependency is passed to the software, rather than the software having to locate it based on hardcoded information.
Let’s look at an example.
“Recently I worked with one of our developers on a website,” says one of our marketing team. “He provided the functional elements of the website – the code – and I wrote copy to populate the pages he built. He was dependant on me for that copy, and I depended on him to build the site. In a scenario where we don’t apply the logic behind dependency injection, I wouldn’t be able to do my job if a different developer was to take over the website. Likewise, no-one else could provide the copy.”
With dependency injection, a copywriter would work with a developer. It doesn’t really matter who either of those people are, or how much knowledge of the project they have outside of their own tasks. As long as copy is provided, and coding is done, the project is completed. If each person gets what they need to do their bit, it works.
This is known as ‘low coupling’, as program elements aren’t explicitly dependent on each other; there just has to be something there doing the job of each component. This helps software have high cohesion – all the elements work better together.
So what’s the benefit?
Developers like dependency injection because this modular style means they can test quickly and test early. Being able to pass mock or placeholder data to a program means that on big, multi-developer projects, one developer isn’t reliant on another to finish an element of the software – if you know what that dependency is supposed to do, you can inject the mock data for your testing purposes. This means it’s a pretty big deal when it comes to projects created using Test-Driven Development methods.
Dependency Injection also naturally complements Agile development methodologies – it allows developers more freedom over the order in which bits of the software are built, so it’s far easier to reprioritise, depending on the client’s needs. Which, after all, is what Agile is all about.
High cohesion, low coupling. Dependency Injection helps deliver software that works to these principles, making life a bit simpler for developers, and projects a lot better for clients. And that, in a nutshell, is why we use it.