Hacker News new | ask | show | jobs
by TheAceOfHearts 3821 days ago
This is sorta tangentially related...

I used to think DI was a good idea, but as I've gotten more experience... I'm no longer convinced using a DI framework nets you huge wins relative to the added complexity. My hypothesis is that good design can remove the need for a DI framework in many cases, or at least that's been my experience so far. Maybe I just haven't worked in an application that's big and complicated enough to justify a DI framework.

Don't get me wrong, there's usually going to a few moving parts you'll wanna inject, in order to make testing easier. But you can just... do it manually.

Perhaps I'm wrong about this, but I get the impression DI tends to encourage people to obsess over unit testing and abuse mocks. While I certainly believe that unit tests add a lot of value, there's definitely a point where they start to add little to no value.

Anyway, with that being said... The framework looks very interesting. I love looking through frameworks like these and seeing how people decide to tackle certain problems.

Thank you for sharing!

3 comments

Agree. Especially in a language like Javascript. For testing -

  var myLib;
  if(process.env.NODE_ENV === "test") {
    myLib = require("../test/my_test_lib")
  } else {
    myLib = require("./my_lib")
  }
or similar (you can obviously tighten this code up, too; I'm just being very explicit). Tada, you're injecting your test stubs in place of the real library when running in test.

For the actual production use that a DI framework gives you, you can do similar, or manually set things up, or create a layer of indirection yourself, that only applies where you need it (say an IIFE that has the logic to determine which implementation to pull in, and return that function/module as appropriate).

But this is, as you say, largely tangential.

There's also stuff like rewire [0], which lets you change required values inside modules. Although every time I've tried it, I found it created more problems than it was solving.

[0] https://www.npmjs.com/package/rewire

Proxyquire - https://github.com/thlorenz/proxyquire - is a nicer way to solve many of the problems that people tend to use rewire for.

We use it to stub out imports in our unit tests and find it very easy.

Disagree with the DI/IoC comments - for more complex projects, it allows dependencies to be pulled in in a nice terse and readable fashion. Runtime complexity also can be harder to manage, as it often ends up in a spaghetti of knowledge in scattered code. DI isn't a particularly complex construct to grasp.

The benefits of mocking are often understated - it is not only to isolate logic, but for longer term maintenance, it is for also easily being able to detect what change breaks what part of the code. When people write more integration tests than unit tests (I have found many developers guilty of this), what often ends up happening is that when someone breaks one of those tests due to having to change behavior of code from new business requirements or maintenance (i.e. abstracting out common behavior), it can become very tricky to untangle what exactly broke, and how to fix it. Not that there isn't value in those types of tests, but without being paired with more controlled unit tests, there is a huge con that hampers productivity of teams.

Ideally, one should have the ability to easily mock and make testing each piece painless & clear, so that finding bugs/breaking changes can be made clearly and consciously. Not all tests need to mock, but having that level of control greatly helps on stable & quick iteration of code.

What is DI?
Dependency Injection