r/programming • • Apr 23 '14

You Have Ruined JavaScript

http://codeofrob.com/entries/you-have-ruined-javascript.html
283 Upvotes

327 comments sorted by

View all comments

Show parent comments

1

u/kazagistar Apr 24 '14

Having done a lot of programming in python and in functional programming langauges, I get rather confused by the crazy abundance of factories in code. Is there a good explination for what problems it solves that cant be solved easier in some other way? Is it more then a hack to get around not having first class functions? Because as I see it, if I want code that does an action with a hammer and a screwdriver, then it just takes a tool in the first place. I dont need a blueprint for a factory.

When you create something, you inherently know exactly what it is. When you use something, you use a general interface that lets you do any general actions you need to with it. And I get that dependancy injection lets you match up the two in in a more declarative and loose fitting way. And I see why sometimes it might be useful to have something stateful that lets you create other things. But it seems like the definitions used is Spring or whatnot go way beyond that, and I am not sure how those factories fit in to this.

1

u/Poltras Apr 24 '14

if I want code that does an action with a hammer and a screwdriver, then it just takes a tool in the first place.

That's basically it, really. The factory is there to create the tool, so you can design the class and say "bring me a hammer, ni!" and the Provider will use a Factory to build a hammer and give it to you. You don't ever create one. Super simple. That's the problem it solves.

When you create something, you inherently know exactly what it is.

The thing is you shouldn't create it yourself, even in python, and that's a good thing. For tests and mocking.

And I get that dependancy injection lets you match up the two in in a more declarative and loose fitting way. And I see why sometimes it might be useful to have something stateful that lets you create other things.

Then you get why you need a factory. But factories are not just doing new Hammer(), most of them are stateful and need to get injections into them as well. Sometimes you need a Factory of Factories.

I think that kind of level of indirection is mostly business oriented, but I also think it's necessary in Java because of the design of the language (interface heavy). In python you can probably save most of it because duck typing.

1

u/kazagistar Apr 25 '14

Then you get why you need a factory. But factories are not just doing new Hammer(), most of them are stateful and need to get injections into them as well. Sometimes you need a Factory of Factories.

Right, but this actually comes up extremely rarerly when I am coding. It seems like it is baked into every crevice of every framework, yet stateful factories are exceedingly rarely needed from everything I have seen. I can't actually find examples where complex, difficult code is made easier by making a factory factory, or abstract factory, or anything similar.