5 ms·
What's sad about Java is that the whole type-safety, compile-time-checks, minimal-runtime-surprises etc has gone out of the window since the DI craze, so now we
by namdnay 13y ago
What's sad about Java is that the whole type-safety, compile-time-checks, minimal-runtime-surprises etc has gone out of the window since the DI craze, so now we have a platform that is as "unsafe" as Python et al, whilst still being relatively heavy to set up etc.. I guess there's still the advantage of having extensive libraries..
- davidcuddeback 13y agoDI = dependency injection, right? How does dependency injection throw type safety out the window? You can only inject types that are a subtype of the declared type. Overridden methods can't extend the types of checked exceptions that are thrown [1]. I fail to see the hole in the type system here. I'm not a Java programmer, but I use dependency injection all the time in other language, including dynamic languages like Ruby. I find it very helpful for isolating bits of code and making extendable interfaces. I'm genuinely curious if there's a major weakness lurking in DI that I haven't discovered yet. Please help me remove my blinders. [1] http://stackoverflow.com/questions/5875414/method-overriding-and-exceptions http://stackoverflow.com/questions/5875414/method-overriding...
- ishbits 13y agoDI will defer the injection to runtime instead of compile time, so you don't get the benefit of type checking during the compile. I've been bitten by this in the past with SpringMVC. It usually ripples up quickly and has never been an issue in a released app.
- justinmk 13y agoYou must be talking about xml-configured DI. cf. Guice, which is compile-time checked: https://code.google.com/p/google-guice/wiki/SpringComparison https://code.google.com/p/google-guice/wiki/SpringComparison
- namdnay 13y agoI really need to try that some time, thanks!
- namdnay 13y agoYeah, it's not a big risk to production, but it just makes the development loop that much more frustrating. There's nothing more annoying than building, deploying, starting, before discovering that you mispelt a bean name...
- charleslmunger 13y agoThis sounds Spring-specific. Using Guice, that's not an issue.
- davidcuddeback 13y agoAh! I see where my confusion lies. As others have commented, it sounds like you're talking about using a tool that does dependency injection for you; perhaps something configured in XML. I see how a tool like that would subvert a compiler's type checker. My mental model for dependency injection is a little different. I think of dependency injection as a technique instead of a tool. The only tool I used to do DI is a text editor. Here's an example of how I would use dependency injection (manually) in Java: interface Dependency { void doSomething(); } class Foo { Foo(Dependency x) { ... } } new Foo(new ConcreteDependencyA()); new Foo(new ConcreteDependencyB()); Both instances of Foo receive their dependencies at runtime, but the compiler is going to check that (1) the concrete dependencies are subtypes of the abstract dependency, (2) the concrete dependencies don't throw extra checked exceptions in their doSomething() method, and (3) the dependent class (Foo) only uses the interface of the abstract dependency. I gain the advantages of dependency injection without undermining the language's type safety.
- namdnay 13y agoI don't think this can be called DI, because you're not injecting them, you're passing them to the constructor. The problem with this model is that you're polluting your constructors with non-functional arguments (for example the logging provider), and that if you want to change one of these, you have to change all your constructor calls!
- 13y ago