r/softwarearchitecture • • Jun 18 '25

Article/Video Why JavaScript Deserves Dependency Injection

I've always valued Dependency Injection (DI) - not just for testing, but for writing clean, modular, and maintainable code. Some of the most expected advantages of DI is the improved developer experience.

Yet in the JavaScript world, I kept hearing excuses like "DI is too complex" or "We don't need it, our code is simple." But when "simple" turns into thousands of tangled lines, global patches, and copy-pasted wiring... is that still simple? Most of the JS projects I have seen or were toy-projects or were giant-monsters.

I wrote a post why DI matters in the JavaScript world, especially on the server side, where the old frontend constraints no longer apply.

Yes, you can use Jest and all the most convoluted patching strategies... but with DI none of that is needed.

If you're building anything beyond a toy app, this is worth your time.

Here is the link to the post https://www.goetas.com/blog/why-javascript-deserves-dependency-injection/

A common excuse in JavaScript i hear is that JS tends to be used as a functional programming language; In that context DI looks different when compared to traditional object-oriented languages, in the next post I will talk about DI in functional programming (using partial function application).

0 Upvotes

18 comments sorted by

View all comments

Show parent comment

0

u/clickrush Jun 18 '25

i'm not sure this makes sense at all. a malformed file or a malformed string are the same .

Not every file represents a string.

But more importantly, trying to read a file can fail for several reasons. A mock has to simulate that. You're also passing in the filename into the function, which means it could be any path. Parsing JSON can also fail.

(Aside: It's perfectly valid to only guaruantee that the code works under the assumption that these files can be read and are valid JSON, but then there's also no point of writing a test and mock.)

Now the reason I'm pointing these things out, is not because I want to be pedantic, but it highlights a real problem: It's that DI frameworks, inversion of control (IOC) and generally abstractions and indirection can easily introduce accidental complexity, while the real complexity is glossed over.

That is sort of the main gripe that people have with these design patterns, so adressing them in the article more proactively would make it more useful.