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

Programmer Humor

19512 readers
330 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
[–] philm@programming.dev 0 points 1 year ago (3 children)

Yeah I guess you can call it like that, I'll just call it function invocation...

[–] bjornsno@lemm.ee 5 points 1 year ago (1 children)

You're gonna have a tough time talking to others about your code if you don't agree on common terminology. Function invocation is just function invocation, it doesn't say anything about the form of the parameters or composition. Dependency injection is a well known and commonly understood method of facilitating decoupling and composition by passing a function's dependencies as parameters. From your comments you're talking about the second, but refusing the name, for... reasons?

[–] philm@programming.dev 1 points 1 year ago (1 children)

I guess I'm a little bit too long already in the functional/data-driven world (after being a decade in OO languages (IMO too long...)). In OOP you may need a separate term for that.

But I think it' just not really necessary in functional programming, it's just another parameter when calling a function, that may be a somewhat type-constrained generic (for testing e.g. with a mock implementation).

I mean function parameters are somewhat dependencies anyway, so should I call all my parameters now dependencies and invocation "injection"?

[–] bjornsno@lemm.ee 2 points 1 year ago (1 children)

Thought you were OP for a second there, as they were talking about composability. Whether it's dependency injection or not depends on what shape your parameters take. If you're doing functional programming and you're passing handlers and connections etc. as params, that's dependency injection. If you're only passing strings and objects and such and the function has to do a bunch of logic to decide how to handle its params, that's not dependency injection.

[–] dreugeworst@lemmy.ml 1 points 1 year ago

I think the main reason OOP has a well-known term and pattern for dependency injection is to differentiate these two (out of multiple) options:

  • the constructor of my object creates other objects it depends on itself
  • I construct the dependencies of my object elsewhere and pass them in to the constructor and use an interface to make it easy to swap behaviour

However, this becomes less of a pattern in functional programming as you wouldn't make such objects to begin with. In FP, you pass all parameters where a function is invoked, and DI just becomes using generic parameters. You wouldn't instantiate a dependency on each function call after all.

As this is such a minor change, it's not really talked about much and it's not really a pattern,

[–] jvisick@programming.dev 3 points 1 year ago

“Dependency injection” is just a term for providing a function or method with its dependencies rather than making the function go and gather them itself.

It’s (typically) done through parameters, but it’s still more specific than just invoking a function. It describes how that function was written and the reasoning for certain parameters. To the other commenter’s point, you’ll have a hard time communicating about your code with other developers if you refuse to use the term dependency injection just because you don’t like OOP.

[–] dudinax@programming.dev 1 points 1 year ago* (last edited 1 year ago) (1 children)

Di is just good functional practice. I'm not sure it's a super importent idea to someone who knows how to write a good function.

Edit: "a function should do one thing and its operands should be passed as arguments" for the OO world.

[–] MrShankles@reddthat.com 2 points 1 year ago

I'm just lurking around, so don't mind me. But I gotta say, it really does sound like y'all are just making shit up sometimes lol. Like a mechanic trying to charge you an extra 50 bucks because your jindle shaft needed an alignment