4 ms·
Do 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 m
by mattmanser 14y ago
Do 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?
- jerf 14y agoIn Haskell, they call this a Reader. This parallels the Writer, which lets code just 'write' to the provided values but not use them (more useful than it sounds, for instance, logging). But it is basically a context value, yes.
- Peaker 14y agoWell, the nice thing about this abstraction in Haskell, is that you can give a name to the notion of receiving a bunch of values (Reader), outputting a bunch of values (Writer), threading state that can be both Read and Written (State), non-determinism (List), etc. You can thus compose all these behaviors you want into a single type constructor and build your programs as composites of this type.
- lusr 14y agoFinally, somebody talking sense. You define a class to encapsulate rules (data, permissions) specific to a class of problems in your domain so that you don't have to repeat those rules elsewhere, which has all sorts of problems (the worst of which become impossible to address in some scenarios, e.g. where one executes potentially malicious code). If you're passing in DateTime.Now to your Publish method then the caller (probably a UI element or a web callback) is implementing business logic that does not belong to it, which violates the single responsibility principle and will eventually cause your code to become unmaintainable. Part of the problem with these posts is the authors seldom have experience building software in some of the more tricky real-world software development environments. While DHH is rightfully respected for many things, I can find nothing in his history [1] to suggest he has the relevant experience to criticise "enterprise development": as far as I can see, he's never had to work on a millions-of-lines code base with 3+ years of cruft written and maintained by large disparate teams of mediocre developers using poorly chosen technologies to solve problems within time, quality and scope constraints outside his control. In these sorts of environments, saying "start over" or "choose better" is not an option and things like DI and well designed interfaces make it possible for a small team of architects to ensure the mediocre developers actually accomplish something and software gets released. I'll repost what I posted yesterday on the other post: interface IArticleService { void Publish(int id); } class ArticleService : IArticleService { protected IArticleRepository ArticleRepository; protected ITimeService TimeService; public ArticleService(IArticleRepository articleRepository, ITimeService timeService) { ArticleRepository = articleRepository; TimeService = timeService; } public void Publish(int id) { ArticleRepository.Publish(id, TimeService.Now); } } Some features of this design pattern: - the consumer of the service doesn't need to know anything about the publish time: e.g. whether it's UTC or local time, whether it needs to have +1 second added to deal with a bug in some legacy interface, or perhaps a switch to database time, etc. The problem of "publish time" is strictly limited to the ArticleService, which is an expert on the matter of publish times. - any new business rules regarding publish times or Publish-related activities are strictly limited to this service and none of the consumers have to be modified when the rules change - the ArticleService is easily testable by swapping in one or more testing ITimeService implementations (e.g. one that returns a fixed test date time, one that returns bad times, etc.) - your software has a consistent, single view of time (which is critical in a time dependent application, for example) [1] http://en.wikipedia.org/wiki/David_Heinemeier_Hansson http://en.wikipedia.org/wiki/David_Heinemeier_Hansson
- martinced 14y agoOf COURSE you pass random in. Or the seed + current value of the PRNG. That's how companies like Blizzard do to reproduce any game state in games like Warcraft III. For example a replay file is tiny: the only thing that is saved are player inputs and the time at which they happened, as well as the seed to the PRNG. You're likely confused because you think "random" means "true randomness" while typically we're using seeded PRNGs. In addition to that, in language supporting high-order functions it's very common to pass a function to/from a function. Passing in a PRNG function and the current value the PRNG is at is a very good way to get a 100% deterministic function inside a functional programming language. Stop spreading FUD about FP.
- laureny 14y ago> 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. Exactly, and that's why DI is useful: it forces you to make a distinction between dependencies and real parameters. You really want your method signature to only accept parameters that are going to change throughout the life of your application while being injected with the dependencies it needs. Something like: @Inject private Database db; public void updateName(String id, String name) { User u = db.find(id); u.setName(name); db.save(u); } Your logic is now clearly separated from the dependencies and trivial to test.