this post was submitted on 08 Jul 2023
572 points (97.2% liked)

Programmer Humor

19544 readers
589 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] bjornsno@lemm.ee 18 points 1 year ago (40 children)

Why is the joke with Java always factories? Factories are really super useful in a dependency injection context.

[–] fuck_u_spez@lemmy.fmhy.ml 1 points 1 year ago* (last edited 1 year ago) (15 children)

I still haven't really understood the use (and use case) of "dependency injection" (and it feels to me I read now everything about dependency injection I could find), to me it seems to be yet another ProblemFactory.

[–] offbyone@reddthat.com 2 points 1 year ago (1 children)

Generally speaking the use case is writing tests. If your tests just call all the dependencies and new directly then it's harder to write tests for your specific component while avoiding setting up a whole bunch of stuff (to make all those other classes work). By requiring all the dependencies to be provided to the class, you can swap them out at test time for something else that's easier to work with.

That said, IMO it's a symptom of problems in language design. Using DI is only necessary because languages like C# don't make it easy to mock out new or classes used directly, so we resort to wrapping everything in interfaces and factories to avoid those features and replace them with ones that are easier to mock. If the language was designed such that those features were easy to replace during testing then DI probably wouldn't be a thing.

[–] fuck_u_spez@lemmy.fmhy.ml -1 points 1 year ago

It's probably a general symptom of what people call OOP nowadays, in a more functional composeable world (where I'm living in currently). You just use function parameters and interfaces (or as Rust calls it "Traits"). But I still think in OOP, this is enough as well and the dataflow is more clear.

load more comments (13 replies)
load more comments (37 replies)