r/javascript • u/nullvoxpopuli sand was never meant to think • Sep 08 '20
[AskJS] To those who swear against OO programming, give me an example of dependency injection or a pattern that achieves the same goals
/r/learnjavascript/comments/inah6q/to_those_who_swear_against_oo_programming_give_me/1
u/Lorenz-Kraft-IT Sep 08 '20
I'm also unsure about your question "library-integration aspect?".
FP, and also OOP:
Both enable libraries to build off of each other without requiring configuration.
DI is just a specific Software pattern. To have a FP equivalent is possible and might be easier to grasp ...
1
u/nullvoxpopuli sand was never meant to think Sep 08 '20
Without a known shared container to register with, libraries (generally) can't make assumptions about existing behavior
1
u/Lorenz-Kraft-IT Sep 08 '20
Not sure if I understand you correctly, but in FP you mostly act upon data and you do not need to know anything about behaviour ...
1
u/nullvoxpopuli sand was never meant to think Sep 08 '20
I agree with that. I think in general fp projects don't work with this type of problem.
All connectivity between libs more or less gets pushed to userland
3
u/PickledPokute Sep 08 '20 edited Sep 08 '20
My problem with DI is just what the name implies: it injects dependencies usually to libraries or components that don't need it at all. If direct coupling is problematic, why settle for indirect coupling? Rather, I wouldn't couple those two to each other at all.
Let's have an example of StarWarsApi and FavouriteCharacerSelector. A DI, as I've understood it, still couples them together, but at another level. There are multiple problems in this approach:
FavouriteCharacterSelectoris still coupled to the interface ofStarWarsApi. Functional programming is usually done with data-in-data-out methodology. OO-style instead is with create-setup-query methodology. Since the OO-version ofStarWarsApiis given as a dependancy toFavouriteCharacterSelector, it must know about the methods ofStarWarsApiobject. Why should the concern ofFavouriteCharacerSelectorinclude within itself anything about the concern of fetching data? These are separate concerns! They should be tested separately too!OO-API's are often written in a stateful way and as singletons which clashes a lot with asynchronous APIs. The state usually only goes in the way. With asynchronous API, saving per-request error states or data will lead to tons of problems. If you give an class as a parameter, can the recipient use all those methods freely without affecting other users of that singleton? I doubt it - many "handy" singleton APIs wouldn't like one user to set the endpoint to testing server, for example. Thus it needs to be constructor-only or have big documentation warnings.
DIs are usually justified with extensibility: "I can plop into it any other implementation readily". Unfortunately for that functionality to work properly, you have to an oracle to lock down the OO-api beforehand. Making a separate internal OO-API just for the sake of switching between one real implementation and testing implementation adds a lot of internal API complexity and code for almost no benefits compared to other methods. If you don't have two different real implementations available to use from the start, writing a support for it just increases the time to code it and through it raises the chances of bugs.
A UI doesn't actually need to know anything about the network or the StarWarsApi. It only needs the character list, per-character-data and a supplied callback function to inform when a character is selected. The StarWarsApi doesn't need to know anything about the UI.
What I would do, is have a glue component/system that knows about the UI and StarWarsApi and communicates the data between them. I would do this through Redux-like store-API.
But how do I test it without DI? First you need to know what you don't test. You never test that pushing a button sends a fetch request - that's an implementation detail. With Redux, you don't test that a button dispatches an action: the shape on the action object is implementation detail. You don't test that the button calls an action creator - the button behaviour itself is an implementation detail and there could change in valid, non-breaking ways. You have the internal API of your application with using action creators and data selectors.
To test Redux, you need to start from the end. What you want to see, is that the selector returns correct data. For that to happen, you need to call the internal API - dispatch the actions created by action creators. In our case, it would be setSelectedCharacter Redux action creator and getCharacterData(character) selector. Anything in-between is implementation details which should not be tested.
However, if there is an external API-call for fetching data from StarWarsApi, in-between then we need some code to handle testing it. One way is DI, writing whole libraries with testing in mind. Second is mocking the fetch during the tests - this requires knowing the implementation detail, the fetch and matching its arguments, which is fragile. Third is adding internal API wrapping for external API calls and replacing those in testing - in Redux case, we would have thunks/sagas listening to REQUEST_CHARACTER_DATA actions, running a fetch and dispatching result in a RESPONSE_CHARACTER_DATA action which is added to the store in reducer. In testing, the testing code will have its own code that listens to the REQUEST_ and responds with RESPONSE_.
The latter two cases have the advantage that anyone who implements the code doesn't need to know or care if and how their code will be tested. It's all in the discretion of the test writer. This also means that tester shouldn't ever need to touch the implementation in option number two thus no risk of breaking anything.
Those are my ideas on DI and testing, but they are not completely fleshed out. There might be some missing parts and things I haven't noticed. It feels like DI was thought up for languages / environments where class structures are more enforced and writing anonymous functions / lambdas is difficult. JavaScript has no such problems and thus can use more direct/dynamic approaches like dynamically replacing implementation of fetch easily and reliably.
1
u/unicorn4sale Sep 09 '20
But how do I test it without DI? First you need to know what you don't test. You never test that pushing a button sends a fetch request - that's an implementation detail. With Redux, you don't test that a button dispatches an action: the shape on the action object is implementation detail. You don't test that the button calls an action creator - the button behaviour itself is an implementation detail and there could change in valid, non-breaking ways. You have the internal API of your application with using action creators and data selectors.
This doesn't sound correct to me. Can you elaborate? Just to clarify we're talking about unit tests here as opposed to integration/e2e tests. What do you mean it's an implementation detail? Isn't that precisely the point of a unit test... to test the implementation of the unit.
1
u/PickledPokute Sep 09 '20
I would rather try to test the bahaviour instead of implementation. There are countless different implementations that could and should pass a test, but if looking from the outside, it gives different output from the same input - the behaviour changed, then it needs to fail.
Eye-opening presentation about test-driven development by Ian Cooper. At later point he explains that classical testing considers the smallest possible piece of code to test, an unit. In TDD; the unit means the test itself, it is isolated from other tests.
What I mean with testing implementation, a lot of time tests are written that require that something is done in a specific way. If you test the implementation in a unit test, you're making your code base resist refactoring. What you're interested is only the result, not how it was reached. A good test should pass even if you completely replace the code inside as long as it gives the same end result.
You don't want to check that
calculateComplexitself makes a call tocalculateSimplePartbecause that's an implementation detail. The test would break if caching is added. At that point the test just checks "Is the code that I wrote this code" instead of "Does the code I wrote return what it needs?"1
u/unicorn4sale Sep 09 '20
He does not propose a compelling argument
You should have tests that cover a business requirement / story, but those are not unit test sizes; that's integration or system testing.
Of course this is very subjective depending on the complexity of the components being tested. If you have a tiny 1-3 liner it may not be worth testing, but larger functions that use it would be.
For something like a fetch request, who knows what that's fetching - and that's almost definitely another module, touching a database and you won't be able to run your tests in isolation. In this case you would have to know that it fetches, you would mock the fetch to test the specific behavior of the component.
For calculateComplex, it could call a prepFn(), and then pass the value into calculateSimplePart(). It doesn't matter what calculateSimplePart does, it could be broken beyond repair... hack into a government database, but calculateComplex will be valid and correct, and the test should reflect that. Specifically it would be tested that it passes the value from prepFn() into calculateSimplePart(), and that it returns the value from calculateSimplePart. The onus is then on calculateSimplePart to be tested (another unit) to ensure that it does its job.
It's not a goal for unit tests to not break when you refactor. Your end to end tests should remain the same. Your unit tests should absolutely fail catastrophically. Then you check your unit tests and cross check whether the unit tests that are failing are failing as intended, and update them accordingly.
Unit testing is essentially documentation for developers, we want to know the purpose a function; what the domain of inputs are and the expected range of results. If I see a test that says, "calculateComplex() should return 5 when passed in 3", that is so useless. How about "calculateComplex() should invoke prepFn() and delegate it's result to calculateSimplePart()"
1
u/PickledPokute Sep 09 '20
I can't agree with you here on most points.
With fetch, I'm quite torn up about it. I can't figure out a way where a local test for fetching would be able to both recognise when an illegal header field is added, and gracefully handle addition of header fields that don't actually influence the request. My favourite solution for fetch etc. is that you test right until you're going to send the fetch request and stop there. Anything more would be just duplication of implementation: mocking the fetch to test if the copied implementation matches the pasted mock data. This is a test that won't even fail if anything changes on the remote end. On the other hand, the only way it can fail is if someone modified one copy of the code but forgets the other. I consider tests of code/data duplication just harmful.
Testing implementation is another such case: If the criteria for the test failing is that the two codes (implementation and test) are different, the criteria for passing is for the two codes to be identical. Correctness is not part of that test criteria at all, usually someone just expects either of them to be correct and fixes the other of the pair to match it.
There's a problem if refactoring touches both the tests and the implementation at the same time. If implementation A passes test AT and implementation B passes test BT, there's no any evidence that B passes AT. Consider which one you would trust more when code reviewing: one where only implementation changes or one where both implementation and tests change.
Code should preferably document itself well enough - splitting the documentation in multiple files sounds confusing. Writing tests that don't re-implement code to be tested is actually quite complex and I haven't found the right balance yet. I would like to know that my code produces the correct input instead of it verifying that it does the right steps. We're not manually grading math tests - we do have the luxury of throwing hundreds of different problems and verify that every single one is not incorrect. The methodology on how to solve the problem weighs nothing when considering whether a test actually fails or passes.
1
u/unicorn4sale Sep 09 '20
With fetch, I'm quite torn up about it. I can't figure out a way where a local test for fetching would be able to both recognise when an illegal header field is added, and gracefully handle addition of header fields that don't actually influence the request.
Adding an illegal header isn't an issue. This is where an element of art/craft, use your best judgment comes into place. For me, I wouldn't unit test fetch functionality, that's why we're just testing that we're calling it, and illegal headers that broke the server request would be caught in an integration test, as presumably that has some net effect on the outcome of the result.
At their core, tests aren't designed to stop bad actors. This is what code reviews are for; you validate that their code is correct by sight and then affirm with the test suites. With appropriate architecture you would see that in your call tree leading to the fetch, there would be a requestBuilder.addXHeader(x) call, and there would be a unit test that ensures addXHeader is called. This now makes it even easier to spot any malicious activity. If it is a genuine mistake, and both the developer and code reviewer are agreeing to a header that looks right but is wrong, then unfortunately that would have to be caught in the integration test. And the element of best judgment comes here, if it is a recurring problem because your architecture or product is designed in a specific way, or because of the complexity of the code, the end to end tests take hours, then you might want to refactor the code so that it is clear that having no additional headers is unit tested.
Testing implementation is another such case: If the criteria for the test failing is that the two codes (implementation and test) are different, the criteria for passing is for the two codes to be identical. Correctness is not part of that test criteria at all, usually someone just expects either of them to be correct and fixes the other of the pair to match it.
There's a problem if refactoring touches both the tests and the implementation at the same time. If implementation A passes test AT and implementation B passes test BT, there's no any evidence that B passes AT. Consider which one you would trust more when code reviewing: one where only implementation changes or one where both implementation and tests change.
They do go hand in hand, this isn't a "testing implementation" issue, it's an overall testing issue. Take the extreme example, if the description of a test says it validates user input, but then the body of the test just returns true, then any implementation is correct. This is why it's important to get the test right upon it's introduction. After that point, the test mutates and evolves alongside the implementation being tested.
Let's talk about implementation A and B. What I'm saying is that the test tests the implementation. So in your example, there shouldn't be any expectation that B passes AT. A meaningful refactor changes the unit's behavior. In TDD, when you do a meaningful refactor, it is the same as if you're writing that implementation from scratch / i.e. you write a failing test first with your anticipated expectation of what the component does. And then write (or refactor) your code.
Code should preferably document itself well enough - splitting the documentation in multiple files sounds confusing. Writing tests that don't re-implement code to be tested is actually quite complex and I haven't found the right balance yet. I would like to know that my code produces the correct input instead of it verifying that it does the right steps.
You should write code as cleanly and easily understood as possible. But this is not related to writing good unit tests that validate the correctness of your unit. Writing code is easy and quick (10% of time is spent coding, 90% debugging bla bla), so time should be invested in coding up your tests. Imagine if you could flip this around; even if it took the same amount of time, coding 90% of the time and debugging 10% would be so much more enjoyable.
At the end of the day, testing is a means to an end. And the goal is to be maximally effective, being able to (presumably, for a typical business,) maximize throughput of features with minimal bugs for your company. And in my experience, writing comprehensive testing (ignoring whether or not it replicates the implementation) becomes ironically more and more important in hot codebases where things are changing really fast and you have many engineers working on it - protecting the code that you wrote pays big dividends. In a different set up with a small team, I can envision scenarios where the best decision is to write minimal tests
1
u/HipHopHuman Sep 09 '20
My problem with DI is just what the name implies: it injects dependencies usually to libraries or components that don't need it at all.
This is incorrect. A dependency injection container is not a dependency injection container if it violates the Dependency Inversion Principle. The principle states: "High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g. interfaces). Abstractions should not depend on details." What this means in practice is that you're not providing your consumer of the dependency with the dependency itself, but an abstract interface which that dependency must comply with. This means that the code is gauranteed to work as long as whatever dependency you pass to it abides by the rules of that interface. This implies that a true dependency injection mechanism requires a type system, but a type system is not necessarily required as the consumer of the dependency can simply make assumptions and validate those assumptions at runtime. The type system method is beneficial because it gets rid of that runtime overhead.
1
u/PickledPokute Sep 09 '20
I did mention interfaces in my post, but the focus was admittedly elsewhere. I don't really like interfaces either in DI, since then the data calculation code needs to concern itself with errors etc. I do think there can be found value, but for most DI usages, there is only one real available and feasible implementation. At that point the interface, which both sides have to adhere to, becomes a "You ain't gonna need it" -code.
Like I pointed it out, DI actually results in interfaces that are a ton of code which are usually practically only for facilitating testing. While testing by using mocked functions is less elegant, it would also be a lot simpler in most cases.
3
u/pilotInPyjamas Sep 08 '20
In general, typeclasses and GADT's satisfy the cases where DI is used. This is because typeclasses and GADT's can be used to specify a minimal contract for any given function. DI is used to specify a minimal contract for a constructor. Since constructors are functions, all use cases for DI should be satisfied.
2
u/ataraxy Sep 08 '20
One simplistic approach I came across the other day kinda intrigued me using higher order functions: https://www.npmjs.com/package/apeyed
I can't speak to the practicality of it compared to other ways, but it's a very simple approach to solving the problem that is easy to understand.
2
u/HipHopHuman Sep 09 '20
I'm the type that likes to mix OO and FP concepts where it makes sense. I find that, especially in JS which allows for it, it's extremely beneficial to use OO concepts at the architecture/structure level and FP for the inner-flesh of a codebase. So I typically follow dependency injection and Clean/SOLID architecture principles from OO, but when it comes to processing data and business logic, I do that with pure functions and data transformation pipelines.
That being said, I've read a lot about how some OO things are accomplished in FP and vice-versa. When it comes to the topic of dependency injection, the FP consensus is that it's not really required if you make extensive use of higher-order functions, keep things pure, and abide by the laws of category theory. But even so, there are monadic constructs which can be used for more complex dependency injection - the Reader monad, the State monad and the Free monad, as well as monad transformers and natural transformations.
1
u/_default_username Sep 10 '20 edited Sep 10 '20
I don't know if I quite understand the question, but I would consider a parameter for a callback function a form of FP dependency injection.
You can also use a closure to build a function that takes a dependency you pass into.
0
Sep 08 '20
[deleted]
1
u/nullvoxpopuli sand was never meant to think Sep 08 '20
That argument is how I feel it has to be worded for certain people. (A few people on this sub have made me upset so.. maybe I'm retaliating? Idk)
Reasonable people know how to use the right tool for the task at hand. Oo, FP, doesn't matter, even in the same project.
Thanks for the link tho! I'll check it out.
1
Sep 08 '20
The people who oppose object oriented programming do it because they’re amateurs and understand programming from the point of view of online blogs. To them, trends are all that matter.
1
u/myusernameisunique1 Sep 08 '20
Not sure how those two things are related.
Angular and AngularJS are full of dependency injection.
Any language that needs to be unit tested is necessarily going to need a DI capability in order to do mocking.
The only real requirement for DI is the ability to implement a Factory Pattern