6 ms·
Dependency Injection is a Virtue
- h2s 14y agoCouldn't agree more. If the behaviour or outcome of a function depends on the current time, then the current time is an input to that function. Inputs to functions should be declared explicitly as arguments where possible. The existence of Time.now and the fact that it's possible to stub this method are not in themselves a good enough justification for excluding the current time from the list of explicit inputs. The length of the list of a function's inputs is one of the indicators of whether a function has too many disparate inputs and therefore too many responsibilities. Obscuring this number is likely to lead to more cluttered code. In DHH's example, Time.now should be called somewhere higher up the call stack where its return value doesn't matter to the outcome of the function in which it resides. It should look like this: def publish!(time) self.update published_at: time end That way the inputs are clear. It also happens to mean that you can test it without requiring the linguistic flexibility provided by Ruby, but that doesn't mean that testability in inflexible languages is the only reason why it's the right thing to do.
- mpweiher 14y agoWell put! Another reason to grab the time once and distribute it is that you typically don't want the passage of time during processing to affect results, that is, you want your system to perceive "atomic" time. Would suck if half your update is December 31st 23:59, and the other half January 1st, 00:00.
- Xylakant 14y agoIn ruby you'd use timecop for time sensitive tests. It allows to freeze time and move it forward/backwards in specified increments.
- chrismsnz 14y agoI think the argument is that solutions such as Timecop are a hack for stubbing dependencies that are pulled out of global scope.
- Xylakant 14y agoAt some point you will need to test the unit that create the time object. With DI you could swap out the implementation of time that gets created and use some implementation that returns a constant time. But that still pulls the information about the implementation to use from some global scope: The DI library needs to know which implementation to use, and that information will come from some global scope - a configuration of some kind, thus making that argument a moot point. In ruby you can modify the classes at runtime to mock such functionality. You could argue that modifying the time implementation is some form of DI - you're changing the implementation at runtime.
- mercurial 14y agoDI is still relevant for dynamic languages, for two reasons: - it lets you put your dependencies all at the same place - it lets you define clearly what the dependencies of a piece of code are, without having to look at internals which may change tomorrow without warning One thing I find aggravating with dynamic languages is that metaprogramming is a crutch people tend to rely on because they can't be bothered to write clean APIs. In my experience, it's particularly prevalent in Ruby. Of course, the lack of dynamism can be a problem in statically typed languages, giving Spring's or Hibernate's stacktraces of hell with piles of proxy objects.
- mpweiher 14y agoVery succinctly put. Newspeak solves this by eliminating the global scope and eliminating "import". Every module is parametrized with everything it needs.
- h2s 14y agoTimecop.freeze(Date.today + 30) do assert joe.mortgage_due? end vs assert joe.mortgage_due?(Date.today + 30)
- deleted 14y ago[deleted]
- yummyfajitas 14y agoYou can also use the SQL value of NOW(). It's always set to the start of the current transaction. http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT http://www.postgresql.org/docs/current/static/functions-date...
- mpweiher 14y agoWell you could, but now you've coupled your system to the database.
- rapind 14y agoWhich if we're going to be honest is a hugely overhyped issue. Note: I base this on how often I've swapped out the database for a project in the past 15 years (maybe twice?) of writing mostly database backed software.
- mpweiher 14y agoIt's not the swapping, it's the (configuration) complexity and test performance, among other issues. See http://www.confreaks.com/videos/759-rubymidwest2011-keynote-architecture-the-lost-years http://www.confreaks.com/videos/759-rubymidwest2011-keynote-.... I particularly like the example he gives near the end of always thinking they need to add the database and finally, after shipping to a bunch of customers, figuring out that they didn't actually need it. In terms of test performance, I frequently see blog posts about reducing the time for a couple of hundred tests from half an hour to 15 minutes. marcel@localhost[~]time testlogger -a MPWFoundation ObjectiveSmalltalk ObjectiveXML EGOS EGOS_Cocoa MPWSideWeb tests:1: warning: 1055 tests (of 1055) executed, 0 failures (100 % success rate) real 0m2.939s user 0m1.607s sys 0m0.438s
- Xylakant 14y agoYour example is not DI. Your argument is in general sound, but it's tangential to the problem of DI. Your time object needs to be created at some point. DI would allow you to swap out the implementation of the Time class at that point. In general, the time example is stupid. A better example is maybe that you have an app that wants to make http calls. Depending on the interpreter and the os you might want to switch the http library. Net/HTTP is a solid default that runs everywhere, but there are faster alternatives if you're on MRI only. In java, you need DI to solve this problem. In ruby, you don't.
- chrismsnz 14y agoThis is just another example of how we're learning the lessons of FP about 20 years late. I'm no expert - I've only muddled with FP languages (mostly Haskell). But what I learned there deeply affected my everyday imperative and OOP coding. Pure functions make for testable, clean code.
- dasil003 14y agoGood functional languages like Haskell also offer better abstractions so we don't have to suffer tired debates around Java methodologies.
- lmm 14y agoThese are orthogonal axes. Good nonfunctional languages like Python also offer better abstractions than Java, and bad functional languages don't.
- chii 14y agoi dont believe python offers any better abstraction over java. Less verbose, sure, but not "better" abstractions.
- dasil003 14y agoYes they're all just Turing machines. You can implement a Lisp interpreter in Java, etc, etc.
- mercurial 14y agoIt has generators, for instance, which makes it a lot easier to implement lazy data structures. Or context managers (though Java 7 has it too). And first-class functions, which beats defining one-method interfaces to get a callback any day of the week.
- dasil003 14y agoAgreed. However I don't need to waste my breath advocating for Ruby over Java. DHH apparently has plenty of time to devote and never seems to tire of it, even after nearly a decade.
- alexatkeplar 14y agoTotally agree. The oddest thing is the example DHH picked and the language he used: > If your Java code has Date date = new Date(); buried in its guts, how do you > set it to a known value you can then compare against in your tests? Well, you > don't. So what you do instead is pass in the date as part of the parameters > to your method. You inject the dependency on Date. Yay, testable code! It suggests that the whole of functional programming has completely passed him by. To re-write that paragraph: If your Java function has Date date = new Date(); buried in its guts, then you have a side-effect which makes it much harder to reason about what that function is doing. Pass the date in as a function argument, and you now have (or are closer to having) a pure function: pure functions have no observable effect on the execution of your program, other than to compute a result given their inputs. A pure function like this is easier to reason about, easier to test, and easier to parallelize. It just feels like DHH is fighting yesterday's battles. Dependency Injection in Java? David, the 1990s called and asked for their book on Spring back. Why can't we be comparing Ruby's approach to something like the cake pattern in Scala (http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/ http://jonasboner.com/2008/10/06/real-world-scala-dependency...), or even the pure keyword in Rust (http://static.rust-lang.org/doc/rust.html#pure-functions http://static.rust-lang.org/doc/rust.html#pure-functions)? If DHH spent some time building something with Clojure/Haskell/OCaml/Rust/Scala/F# and less time racing cars, he might come up with some interesting views again.
- mattmanser 14y agoDo you pass Random in? Or how about new file streams? Hell why should we even bother with local variables, lets just promote everything up the chain? Sure our method signatures would get longer and longer. We'd have to keep declaring variables again and again even though they're all used in one place. There's a line where it gets absurd as it makes the code more complex. There's no good reason to be adding a date variable to your method signature just to make everything functional. Because you then have to declare and initialize that variable extra times. That's the opposite of code reuse, that's bad programming. What happens when you need to refactor the method? What if the method call is in a different site to the date declaration? Why do that to yourself? This hasn't got anything to do with functional programming. It's just simply about DRY and pointing out dogmatic coding approaches like DI being applied without actually understanding why you're doing it.
- darrencauthon 14y agoSo you'd say that this code is better: post.publish Date.now than post.publish When the publishing will only happen now? I'm a Ruby developer who disagrees with DHH strongly on this issue, as well as his stance on SOLID and testing. I also have years of experience in C#, using TDD and using dependency injection and IoC containers everywhere. However, I think there's a point to be pulled from DHH's post about unnecessary ceremony around dependency inversion (the more important thing) and static languages. If I were to write this "publish" method in C#, I couldn't even put it in the Post object. It all looks fine and dandy now, but what happens when the first wave of change comes in? Say, if we were to have to stamp an "updated_by" field? Then we get to this: def publish(time, updated_by) self.update publish_at: time, updated_by: updated_by end And so on, and so on. The dependencies have to be passed to the object, so every dependency flows up to become dependency on any object that uses it. And since having method calls like ".publish(Date.now, CurrentUser.name)" everywhere really stinks for readability and DRY, and since I can't make Date.now a constructor requirement to create a Post, I always found myself having to do this (in C#): public class PostPublisher private IDate date; private ICurrentUser; public PostPublisher(IDate date, ICurrentUser){ this.date = date; this.currentUser = currentUser; } public void Publish(post){ post.updated_by = currentUser.name; post.published_on = date.now(); // save???? } } ... and let the IoC container wire this sort of stuff up. Dependencies tied to this operation are isolated (as well as every other operation). But UGH, is this any better? So many classes and LOC just to do simple things. I just don't see a good solution to this, and I'm not satisfied writing "good" code that handling is a pain. I decided to try Ruby as it seemed to have answers for this, and it does. You can call the ability to use the Date object and still change it a "global variable" or "linguistic flexibility," but the truth is: In the applications we write, published_at is only going to be set to Date.now and updated_by to the currently logged in user. And as soon as we write the application and deploy, it will serve the business needs wonderfully. As a C# developer, I justified the costs of doing these end-arounds as necessary to maintain a TDD'd codebase that could easily be changed later. As a programmer, though, I can't justify the costs of writing dozens of lines of code when I can TDD one line of code. And I sure as heck don't want to eat the costs myself. So today, I just do it with one line of code really fast and make the client happy.
- wyuenho 14y agoI think most of you are missing the point. I think the example DHH used should be interpreted as calling post.publish! is easier than post.publish! Time.now Since when you publish, the time of publication is the only value that make sense. Now, you can argue that you can set a default argument to Time.now, but that's weird code that only make sense if you need to supply a value other than Time.now, which you will only do in testing, so the code in DHH's example is optimal. The point of DHH's blog post is, in a flexible enough language like JS, Ruby and Python, you can still test pretty much anything without DI. I honestly don't know any other reason why one would want to use DI other than making Java testable. In fact, you don't even need that in Java anymore because you can use Mockito these days and it can mock private stuff. The stupidest reason I keep hearing over and over again is DI makes it possible to change an implementation at runtime. Wait what? Don't you have to change the application.xml file in Spring and restart the app? AFAIK there's nothing in Javaland that lets you listen to config file changes and then magically reconfigure the app context and make a copy of said context as a thread local in every new thread. Correct me if I'm wrong please. Even if there was, what magical alien land would require such a "feature", and such feature is so important that requiring your entire code base to be littered with all these indirect abstractions acceptable?
- Xylakant 14y agoDI to swap out an implementation at runtime is an extreme edge case, though "runtime" may mean "the app can determine at startup which implementation makes sense". If you want to have a look at a nice usage of DI, peek into the source of Elasticsearch. You can swap out pretty much any relevant part of the implementation. You can for example change the HTTP acceptor to provide authentication [1] or add SSL [2] So there are cases where changing the implementation makes sense. [1] https://github.com/Asquera/elasticsearch-http-basic https://github.com/Asquera/elasticsearch-http-basic [2] https://github.com/Asquera/elasticsearch-ssl-transport-plugin https://github.com/Asquera/elasticsearch-ssl-transport-plugi...
- wyuenho 14y agoFor which any simple configuration mechanism will do, and as long as you stick with coding to the interface, the problem is solved. So while technically this is called DI in Java, it's simply code taken as granted in many dynamic languages because of duck-typing. So yes, this is totally valid use case, but one with no need for a DI container in dynamic languages to solve.
- twelvechairs 14y agoRegarding the 'publish!' method, the post linked to in DHH's example (http://weblog.therealadam.com/2013/01/03/design-for-test-vs-design-for-api/ http://weblog.therealadam.com/2013/01/03/design-for-test-vs-...) explains this as a 'trade off' - the summary of the other side being that if you don't pass 'time' to it you get a simper API. This is somewhat more nuanced than your and DHH's conflicting opinions, and perhaps I show my inexperience (as primarily a ruby programmer), but this seems very sensible to me. It also reasonably describes why DHH's opinions may differ from others because of the focus of his work (in addition to the language used).
- masklinn 14y agoI would have gone with "sane defaults": def publish!(time=Time.now) self.update published_at: time end keeps the simple case simple (self.publish!) but allows injection if needed (custom publication time or testing stub). My language of choice being Python, I tend to do that rather often for injecting "classes" which will have to be instantiated: the function or object constructors takes callables (since Python's instantiation is just calling) with defaults provided.
- xrd 14y agoDI is why I see AngularJS as the best of the new JS frameworks out there. Given current JS language limitations DI makes AngularJS so testable, and you cannot say this about other tools like Backbone.
- mdemare 14y agoMostly disagree. Pure functions are beautiful and have their place, but not every function should be a pure function. And a Rails controller method is the opposite of a pure function - it has a database connection, several file descriptors and hundreds of objects at its fingertips. That is its strength, and DI has no place there. That said, if you have a pure function, and you need to know the current time, consider keeping it pure with DI.
- Rygu 14y agoSounds like the controllers described need some major refactoring. How many hundreds of lines are their unit tests? http://en.wikipedia.org/wiki/Single_responsibility_principle http://en.wikipedia.org/wiki/Single_responsibility_principle
- mercurial 14y agoEr. Obviously, not every function can be a pure function, otherwise your program will not work. However: - controllers should not touch the data layer directly (and therefore follow SRP) - controllers should have their business layer services injected from somewhere else This does not make them pure in the sense they remain (indirectly) functions with side-effects, by virtue on relying on business services, which themselves interact with data access services which do IO. However, it does make them testable without resorting to hacks and offers a clean interface.
- mdemare 14y agoThe vast majority of Rails apps fail those conditions. I'd say that it's not the Rails Way to follow those precepts, so I believe your comment dismisses Rails in one stroke. I'm not sure if that's a very productive line of thought.
- mercurial 14y agoIt's not impossible to achieve with Rails, it depends on what you put in your fat models. There is nothing which keeps you from having an intermediate business layer. I'm not sure how you would handle DI cleanly though, maybe it's possible with Rails 3? My brief experiments with Rails date from a few years ago, so I'm a bit fuzzy about this sort of details. I'm confused about your comment regarding productivity. Either my recommendations are mostly valid, in the sense that they offer a distinct advantage over the current, Rails-y way of doing things, or they are not (but in which case you would need to make a cogent argument for why they are not). If they are, surely recognizing the shortcomings of a particular design/set of accepted practices is a benefit. The fact that Rails 3 has undergone significant architectural changes with the merge of Merb shows that Rails did not emerge perfect from the thigh of DHH. And we all strive to improve ourselves and the tools we work with (or alternatively, for those of us for whom this is not an option, we stay with PHP or VB and stare blindly at people talking about "MVC").
- smoyer 14y agoI agree ... I won't even complain about Java's DI implementation. I've used both Weld and Guice and will admit that both have edge cases that aren't ideal, but it's better than the alternative. In general, I think I'll choose MF over DHH anytime both have an opinion: http://martinfowler.com/articles/injection.html http://martinfowler.com/articles/injection.html
- barrkel 14y agoEvery few years, there are fashions in our industry that feel like moral virtues to follow; structured programming, object orientation, patterns, functional, TDD, IoC. They warm the cockles of your heart, make you feel good about yourself. But I am suspicious of this feeling, because it is an emotional in-group attachment, a bit like a religion. All religions have costs to following them, so that disbelievers may be easily recognized and (to a greater or lesser degree) demonized. But the world isn't and shouldn't be so black and white. Actions should be analyzed more carefully, and above all, rationally. Code structured in a fashionably virtuous way makes me suspicious that the situation may not have been analyzed dispassionately. The style may be an end in itself. I'm especially on guard because I used to be an acolyte of the OO religion.
- Jach 14y agoIn other words, you're part of the Programming, Motherfucker (http://progmofo.com/ http://progmofo.com/) religion now? ;)
- cnlwsu 14y agoI have never seen this before... I think I was just converted.
- praptak 14y ago> structured programming Actually this one is alive and well, although it might be hard to see as we have built huge complex structures over it. We still follow its basic principles - one entry point per subroutine, no jumps into middle of loop body, no jumps into branches of "if", only the subroutines are sometimes called procedures, methods or functions. Even those who program directly in machine code seldom break the above commandments. Nope, SP is definitely not a fashion.
- barrkel 14y agoI sometimes even use goto, as well as break, continue, and return before the end of the function. Bite me. ;) (SP adherents used to advocate using flags instead of breaking out of loops explicitly, amongst other things. It's this kind of virtuous action for the sake of the virtue, rather than anything that necessarily makes sense, that I'm talking about.)
- darkxanthos 14y ago"I don't like this tool. I've found ways around using it." "I use this tool a lot." Why is this so controversial? It's a hammer. Ultimately you're trying to get two pieces of wood held together and you've all found a way to do that. As long as both camps can sustainably produce maintainable software the point is moot.
- martinced 14y ago"Having hard-coded class-names like in the example Time.now is effectively the same as communicating via global variables." But having DI is exactly that: DI is about glorified global variables! I realize most HN participants living in the Java/C# + ORM ([N]Hibernate)) + XML + SQL hell are just loving DI because it's a tad bit less worse then global variables but, dudes, there's another world out there. A lot of functions can be made idempotent (always returning the same output for a given set of inputs) and also pure (idempotent and without any side-effect) and this facilitates everything: it gets much easier to "recreate the state" and it makes it easier to reason about your programs. It's sad because HN is a community built by pg, who talked about "beating the average", about using Lisp dialects, about Emacs, etc. I took all his writing as "thinking outside the box". And what do I see here? I see that HN is mostly consisting of enterprisey programmers living in the Java/C# + ORM + XML + SQL hell and not even realizing there's another world out there. It is sadening me.
- laureny 14y ago> A lot of functions can be made idempotent Idempotency is completely orthogonal to the problem of functions accepting parameters that are a mix of live parameters and ones that can be injected at start up.
- brown9-2 14y agoDI is about glorified global variables! Can you expand on what you mean by this?
- _ikke_ 14y agoDI is not about glorified globals at all. Nothing forces you to pass in the same object to all things dependend on it. It just allows you to pass in anything the situation requires. It allows your code to depend on an interface, instead of an implementation, even if the language doesn't have explicit interfaces.
- mpweiher 14y agoDid you bother to read about Newspeak or click on the links? Did you see the part about Java "AbstractAbstractFactory" actually being very close to "worse" than global variables? http://newspeaklanguage.org http://newspeaklanguage.org But apparently clicking on links is too hard, so I'll quote inline: "Newspeak is a new programming language in the tradition of Self and Smalltalk. Newspeak is highly dynamic and reflective – but designed to support modularity and security. It supports both object-oriented and functional programming. Like Self, Newspeak is message-based; all names are dynamically bound. However, like Smalltalk, Newspeak uses classes rather than prototypes. As in Beta, classes may nest. Because class names are late bound, all classes are virtual, every class can act as a mixin, and class hierarchy inheritance falls out automatically. Top level classes are essentially self contained parametric namespaces, and serve to define component style modules, which naturally define sandboxes in an object-capability style. Newspeak was deliberately designed as a principled dynamically typed language. We plan to evolve the language to support pluggable types." Sheesh. Update: I know the creators of Newspeak, but am not affiliated in any way.
- laureny 14y agoI can't for the life of me find the connection that DHH is making between dependency injection and the fact that a language is dynamically or statically typed. These two concepts are completely orthogonal to each other.
- tszming 14y agoI think DHH is actually not complaining DI in a general sense - DHH just want to emphasize DI is not required in dynamic language such as Ruby. He also said that the real problem is when people applying techniques to solve one language's limitation on other language which problem does not exist and think it is okay. (..While we can cross-pollinate some ideas between languages, there are many we cannot. ..) So, if you are using Java, it is perfectly fine to continue to use DI, but think carefully when you are using DI (framework/container) on Ruby..
- jebblue 14y agoI think the Spring concepts confuse things more than solve problems. Example: configuring stuff in a bean.xml file, your code does not own the creation of the instances, some magical springy mechanism does it for you. What's wrong with just letting the program read its startup parameters from a properties file? Stack traces from springy code is just that like trying to eat a pea with a knife.
- brown9-2 14y agoNothing is wrong with letting your program read it's startup parameters from a properties file (and thus doing all the wiring up of classes in code yourself). Spring and it's bean.xml files are meant as a replacement option for that for when you find that doing-it-yourself is too tedious/too much code.
- stephen 14y agoPersonally, I feel that a lot of the Java hate is misplaced. Just because most Java programmers/architects use/love J2EE, crazy XML/DI frameworks, etc. is not a fundamental attribute of the language itself, just an unfortunate result of how it's mainstream usage has evolved. I assert you can write "nice Java", either with something like (shameless plug): http://draconianoverlord.com/2011/03/17/frameworkless-di.html http://draconianoverlord.com/2011/03/17/frameworkless-di.htm... Or, for a better source, GooS, Growing Object-Oriented Software, by Steve Freeman and Nay Pryce: http://www.growing-object-oriented-software.com/ http://www.growing-object-oriented-software.com/ ...although, yes, a more succinct language like Scala helps too.
- ssmoot 14y agoPiggy-backing on your response: The J2EE hate is a cheap shot. It's just a stack of tools/APIs. Which part of it do you hate? The Servlet API is just Java's version of Rack. It's actually mostly pretty nice. It definitely have it's faults, but it's also much more robust than Rack. Is it ejbeans? Is it JMX? I mean, what is the hate exactly? People might be rightfully annoyed if you just said "I hate Rails" when you actually meant "I hate Devise". Anyone running JRuby Rails apps are using a portion of J2EE and they may not even know it. It's what allows you to flop between Mizuno, Trinidad or Torquebox and not have to care.
- deltasquared 14y agoNo, Dependency Injection is a language specific pattern. A pattern useful in Java, but not in Ruby.