5 ms·
Totally 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
by alexatkeplar 14y ago
Totally 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.
- dgregd 14y agoSomeone asked Java DI related question. All answers were dogmatic. http://stackoverflow.com/questions/131975/what-are-the-benefits-of-dependency-injection-containers http://stackoverflow.com/questions/131975/what-are-the-benef... I tend to think that sometimes guys choose (or are forced to choose) solutions like configuration xml files over source code files just to keep quiet all these inquisitors.
- flyinRyan 14y agoI didn't see any concise answers there so I'll say why I use this a lot at work: - If I use Spring.Net I never have to put stupid trace logging on any functions but I get it for free. I make one change in the XML config files and suddenly every function for every inject object has full tracing turned on. - Getting new code installed into production is a huge drama/pain but changing a config file doesn't have the same level of bureaucracy.
- Peaker 14y agoYes, you pass in Random, and file streams. If you need to pass the same set of multiple things to multiple functions, you declare a type that represents all these arguments. At least this is the approach used in Haskell, with semi-explicit (explicit in type, implicit in the value) passing of parameters. If you take a time argument, you have to declare an extra argument, not an extra variable, and in a powerful language, you can abstract its passing around. If you move the method to a new location where its input is not available, you propagate up the input requirement in the form of argument passing, which conveys the new dependency up the call chain.
- adamauckland 14y ago> If you need to pass the same set of multiple things to multiple functions, you declare a type that represents all these arguments. At least this is the approach used in Haskell, with semi-explicit (explicit in type, implicit in the value) passing of parameters. So might one call this a Context object?
- brazzy 14y ago> David, the 1990s called and asked for their book on Spring back. Psst... the first milestone release of Spring was in 2004.