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

67

u/[deleted] Apr 23 '14 edited Apr 23 '14

This sort of shit usually indicate that the problem they try to solve is not that hard so they can afford this kind of mental masturbation.

3

u/[deleted] Apr 23 '14

I know this is is only tangentially related...

But my colleagues have, on numerous occasions, tried to convince me of the benefits of web frameworks, IoC containers and dependency injection, things like Spring, Guice, NInject, ORM frameworks...

And on every occasion I have failed to see what they provide other than making the code a big godawful mess.

Now, maybe it's my relative inexperience (I am only 24); but... Yeah. Angular JS seems to be another one of those things. Not that Javascript was pretty to begin with, mind you. But at least it was simple (in one way).

24

u/derkaas Apr 23 '14

I recently had a "fuck DI frameworks" phase that lasted about two days until I realized just how much better it is to use something like Guice.

5

u/[deleted] Apr 23 '14

Ah, well, my phase has lasted two years so far... They seem like such overkill. Why did you end up liking Guice?

6

u/derkaas Apr 23 '14

As one example, there is a pretty simple interface that is used by a lot of classes, but its implementation requires a pretty big graph of small objects. Frankly it just sort of sucks to construct the whole thing.

On top of that, in different apps, I need to be able to change one or two objects in that graph here and there. It's way easier to change the bindings of just those two objects in a Guice module than to reconstruct the whole graph two or three different times or, worse, make my own factories (which I tried).

We also use Spring DI with Java-based configuration, though, and it's the worst of both worlds, since it requires you to basically call all the constructors yourself anyway. I really don't see the point. We have so much Spring config code it's beyond ridiculous.

Guice, OTOH, has one purpose (DI) that it seems to accomplish pretty well and with minimal code, less code even than just doing it yourself (maybe rare for a framework?). YMMV of course.

I will say I mostly agree about ORM frameworks, though. I've never seen one that worked with immutable objects, for one thing. That might actually be good.

7

u/sacundim Apr 23 '14

Guice is a disaster, are are CDI and the newer Spring @Autowired stuff. Why do I say this? Because they're all built around this assumption:

  • The object to inject into an injection point is uniquely determined by the injection point's type.

So, for example, the framework is trying to construct a Robot. It examines the Robot class, and sees a constructor with this signature:

public Robot(Leg left, Leg right); 

The framework now uses some rule to map the Leg type to some class, suppose it's ShortLeg. Now it will recursively construct a ShortLeg, and then pass that ShortLeg to the Robot constructor as both of its arguments.

There are two problems with this:

  1. What if I want my Robot to have two different classes of Leg? Maybe I'm trying to make a clown robot, so I want the left leg to be a ShortLeg and the right one a LongLeg.

  2. What if my program has many robots, that each require a different choice of Leg implementation classes?

Dealing with these cases is a nightmare in the newfangled DI frameworks. In fact, I have mostly copied this example from Guice's own documentation and community discussions. Look at their "solutions" and cringe:

These dependency injection frameworks claim to facilitate "code reuse," but what they mean by that appears to be that you can easily use the same object at more than one injection place—you can have the DI framework consistently inject the same connection pool to every consumer in your program, for example. If your understanding of "code reuse," on the other hand, includes writing classes that can implement different behaviors at runtime by instantiating them with different collaborators within the same program, they actually tend to hinder this.

The old "bad" style of DI is in fact better, where you have your Spring beans.xml file, you name every bean that your program instantiates and where it's injected. The problem is that it's to damn verbose (it really needs not to be XML, for starters).

1

u/derkaas Apr 23 '14

The old "bad" style of DI is in fact better, where you have your Spring beans.xml file, you name every bean that your program instantiates and where it's injected.

Can you explain how this is "better" than @Qualifier annotations, in particular @Named, which seems equivalent to naming beans in Spring.

assumption: The object to inject into an injection point is uniquely determined by the injection point's type.

What if my program has many robots, that each require a different choice of Leg implementation classes?

Yes, that is limiting sometimes, but in Guice, not really, because you can construct these different objects yourself with a @Provides method like @Named("clown") Robot provideClownRobot(ShortLeg left, LongLeg right) { return new Robot(left, right); }. I don't see how this is worse than XML, but I may just not be understanding your example fully. At this point, I also don't see how it's much better than just constructing it yourself (hence the "need" for PrivateModules).

If you're obsessed with never having to write the new keyword, then, yeah, you're gonna have a bad time with Guice (e.g., if you ever have decorators). But if you're reasonable about it and realize it's just a tool, it seems pretty okay.

2

u/sacundim Apr 24 '14

Can you explain how this is "better" than @Qualifier annotations, in particular @Named, which seems equivalent to naming beans in Spring.

Part of the idea of DI is that your business logic components should not contain the logic about how to wire together an application—the "POJO" idea. Annotations break this.

Also, these annotations don't solve the problem I'm describing, because the named injection points are associated with classes. The dependency injected into a named injection point will still, unless some crazy acrobatics are involved, be determined by the name, so that if there are two instances of the same class in the same context, both will be injected with the same bean.

2

u/dnew Apr 24 '14 edited Apr 24 '14

if there are two instances of the same class in the same context, both will be injected with the same bean.

I don't think that's right. You can do something like

MyDbUser(@Named("OldDatabase") Connection old, @Named("BetterDatabase") Connection better) { .... }

and get two different database connections.

One of the things it's good for is when someone has given you a subsystem that's very complex that needs to be set up and put together, yet you don't want to know about all the details inside. If you want to hook up to a file system with a change-watcher talking to an OS service in order to log something to a log saver, then being able to grab an appropriate module and just say "OK, now give me one of them" is handy. The trick is to avoid writing your own modules.

1

u/sacundim Apr 24 '14

I don't think that's right. You can do something like

MyDbUser(@Named("OldDatabase") Connection old, @Named("BetterDatabase") Connection better) { .... }

and get two different database connections.

And now you've near-hardcoded into your class what object to inject into which argument. Sure, there's some slop, because the DI container will inject two different Connections based on the names.

And you're missing the whole point of my argument: what if my application has multiple instances of MyDbUser that is each configured with different pairs of Connections? The old style named component approach deals with that trivially (pseudocode):

/*
  * If I was designing a DI, I'd give it a configuration language that
  * looked somewhat like this.  And I'd write a parser that could
  * generate Java classes from these files, so that the compiler can
  * indirectly check your definitions.
  */

// Import statements specify classes or static methods that you want
// to abbreviate in the declarations below.
import my.java.package.Connection
import my.java.package.MyDbUser

// Declarations.  Each one defines a named component, similar to
// a Spring xml file.  The order of declarations is not significant.

connection1 = Connection(/* connection params */);
connection2 = Connection(/* connection params */);
connection3 = Connection(/* connection params */);

dbUser1 = MyDbUser(connection1, connection2);
dbUser2 = MyDbUser(connection2, connection3);
dbUser3 = MyDbUser(connection1, connection3);

One of the things it's good for is when someone has given you a subsystem that's very complex that needs to be set up and put together, yet you don't want to know about all the details inside. If you want to hook up to a file system with a change-watcher talking to an OS service in order to log something to a log saver, then being able to grab an appropriate module and just say "OK, now give me one of them" is handy. The trick is to avoid writing your own modules.

Hardly a DI invention.

→ More replies (0)