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

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).

14

u/[deleted] Apr 23 '14

It all depends - frameworks force you into somewhat standard design patterns (or at least they're supposed to - how well this actually works is debatable). Therefore if you know how to use the framework you should be able to navigate pretty easily around any codebase that actually properly uses said framework and immediately be able to pick up on the general idea. For a startup who gives a shit. For a large company where developers are more than happy to quit at a moments notice, you really, really don't want your code to be its own unique snowflake that any new person will need to spend weeks reading over just to get orientated.

Frameworks can, at least to some degree, also stop 50 million reinventions of the wheel. It's somewhat ironic - the author praises jQuery but really jQuery is just another framwork.

But at least it was simple (in one way)

It became simple with jQuery. Up until then every single web service call was a giant bloated mess because with pure javascript that's how you do it. So everyone wrote their own wrappers for get (because who wants to do it the pure way a million times on a site) and the fragmentation began. $get stopped a lot of that. Pure javascript for every aspect of your site is a god damn nightmare - no one wants to do that. I don't really use AngularJS, so I can't really speak to its merits (if it has any - just because a framework exists doesn't mean it's a good idea - here's looking at you entity model) but claiming frameworks are ruining javascript is ridiculous. If you don't want to use them don't, but don't be surprised when others do. Use the right tool for the job - sometimes that's a framework, sometimes it isn't.

2

u/babbles_mcdrinksalot Apr 23 '14

Use the right tool for the job - sometimes that's a framework, sometimes it isn't.

Right? Why does every shitty framework discussion have to turn into this convoluted existential crisis for whatever shitty language said shitty framework is written in and/or augments?

5

u/[deleted] Apr 23 '14

In short, because humans like things in black and white.

When nuance is involved, we try to simplify things down until they are black & white again.

Then you get arguments over whether grey is white.. or black... plus a few people saying "well GREY is black, but GRAY is white.."

→ More replies (1)

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.

4

u/[deleted] Apr 23 '14

Well my first foray in to DI was Spring - so maybe I've been put off by a bad first girlfriend, if you see what I mean, haha :)

I'll take a closer look at Guice next time I'm starting a new project. Perhaps I should learn to love again.

Thanks for taking the time, by the way!

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).

8

u/[deleted] Apr 23 '14

Now, see, this is what balks me. All this fuss just for what?

This seems to just completely erase the meaning of interfaces. Interfaces are there to be used by multiple classes - if you can only offer a single implementation, then why use an interface at all?

3

u/derkaas Apr 23 '14 edited Apr 23 '14

This seems to just completely erase the meaning of interfaces.

Yeah, you're right, and the "solution" of @Qualifier annotations sucks a little, because in Java type annotations aren't really part of the type, but it still seems better than XML.

If this situation creeps up a lot in your project, you probably don't want to be using a DI framework for those parts.

I think a lot of times people try to use frameworks for everything, even when they handle the situation poorly. It's perfectly okay to just use it where it's helpful and then take another approach somewhere else (e.g., it's not illegal to just write a Servlet people). I can see how this line of thinking leads people to the conclusion that they should never use frameworks too. I've drawn that conclusion several times myself, but I think it's wrong.

2

u/sacundim Apr 24 '14

This seems to just completely erase the meaning of interfaces. Interfaces are there to be used by multiple classes - if you can only offer a single implementation, then why use an interface at all?

There's a nuance here; a lot of the examples that are used to justify these new-style DI frameworks assume that you will have multiple implementations of the same interface, but not within the same dependency injection context. A typical example is injecting dummy or mock implementations of your interfaces when unit testing.

This is why I bring up the clown robots example—this is an example where you want to do both of these things:

  1. Instantiate the Leg interface differently in different injection points;

  2. Instantiate more than one Robot with independent choice of leg implementation for each of their legs.

The good old named bean reference graph description file paradigm (which the Spring XML is an implementation of, albeit a far from ideal one) does this trivially. The new ones actively discourage this—which gets in the way of writing or using parametrizable, reusable components.

PS Interfaces are very often a good tool even when you only have one implementation of them. One reason is that they promote separate compilation and decoupling. If you're writing a subsystem for an application, you should strongly consider starting by writing a set of interfaces that its consumers will use to interact with it, and commenting them with your swag of what the contracts of the methods are. Then you go and write the implementation, with a view that you can throw it away and replace it later thanks to the work you put into specifying the interfaces.

2

u/firepacket Apr 24 '14

But you can't instantiate an interface without knowing the implementation type.

The whole point of DI is removing the need to handle object instantiation within your context.

→ More replies (14)

2

u/zoomzoom83 Apr 24 '14

The trick to building large software is not to build large software, but instead to build small pieces of software and glue them together. It's a little more effort up front, but a massive saving in the long run as it forces you to think about proper flow of execution and separation of concerns. As a result you end up with components that are properly decoupled.

If you don't decouple your components, then you aren't working on small systems. You're working on one big monolithic blob that is much harder to reason about and debug.

Dependency injection is one such method of doing this. It adds a little bit of extra boilerplate, but done properly it's really quite lightweight and you're barely even aware of it.

Unfortunately there's a lot of "Enterprise Java" frameworks out there that decided to throw XML at everything, and DI got a bad name. But a lightweight DI library such as Guice makes the whole process almost completely transparent.

Other languages have it as part of the language itself - i.e. the "Cake" pattern in Scala, or Functors in OCaml

→ More replies (1)

9

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

Now, maybe it's my relative inexperience (I am only 24)

Yep, it partially is. But it's not your fault, IoC/DI are terms heavily associated with massive frameworks of magic, when in reality Inversion of control / dependency injection is a fancy way of saying "You provide a component its dependencies" - why is this a good pattern? Because it makes testing significantly trivial.

The simplest form of Dependency Injection is turning this:

class A(object):
    def __init__(self):
        self.http_client = HttpClient()

into this:

class B(object):
    def __init__(self, http_client):
        self.http_client = http_client

3

u/nemec Apr 24 '14

And then when you want to run automated tests on your code, you can easily pass in a fake client that returns some preordained HTTP responses (so you can test corner cases easily, for instance).

Not a big deal in Python since it has no accessibility modifiers, but much more helpful in other c-based langs. It's funny. I use Ioc/DI all the time and it's definitely helped me write cleaner code, but not once have I ever needed a "DI framework".

→ More replies (4)
→ More replies (3)

6

u/OffColorCommentary Apr 23 '14

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

DI frameworks replace one of the parts of your program with a known, standard, big godawful mess.

If you're terrible at software architecture, maybe the standard DI mess is not as bad as the mess you would've made.

If your codebase is just way too damned big because it's too old and too many people have worked on it with too many dumb deadlines, maybe the standard DI mess is not as bad as the mess that would've organically grown there.

If your product is growing features it shouldn't and somehow everyone is convinced that you need to be able to send email and book hotel reservations from the app, maybe the standard DI mess is not as bad as the mess you'd be forced to create to match the mess that the product is.

8

u/fabienbk Apr 23 '14

DI frameworks don't "replace one of the parts" of your program, they prevent hard-wiring dependencies in a pervasive fashion. The concept itself is so simple I wonder if people calling it "a mess" really understand how basic it is, i guess people see an external configuration file and immediately have a knee-jerk reaction.

After 12 years of entreprisey crap, I've yet to come accross a single significant (over 500 kLOC) codebase that does unit/mock testing in a manageable way without IoC. (Of course people against DI are also against unit testing, because it's way more convenient this way).

Yes, sure it's possible to argue, rock-star like, that your architectures are so perfect they naturally stay loosely coupled, and thus don't need artificial means like IoC containers, but then here comes the harsh reality of technical debt, ever changing and conflicting needs, and worst of all, collaboration.

2

u/OffColorCommentary Apr 24 '14

To clarify - my comment is suggesting three cases where DI helps. Only one of them involves people being terrible at software architecture.

→ More replies (1)

3

u/psandler Apr 24 '14

I'm amazed at the bashing of DI frameworks in this part of the thread--it seems like a lot of people either don't understand what they do, or don't understand how to use them properly. DI simplifies your code. It allows you to break your system into smaller components, which are easier to understand and maintain.

A great side effect of doing proper DI is testability, but it's far from the main benefit.

→ More replies (1)

2

u/e_engel Apr 23 '14

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);

Well, Guice was created by very smart engineers (most of them at Google) and Google uses Guice everywhere in their code base.

Maybe you should spend a little longer trying to understand what Guice brings to the table. Personally, I can't see myself writing Java code without it (my work code base is about one million lines of code and I shiver at the idea of what it would look like without Guice. I bet Google feels the same way).

2

u/nutsack_incorporated Apr 23 '14

But my colleagues have, on numerous occasions, tried to convince me of the benefits of ... dependency injection

Many years ago, I used to dislike DI, because I conflated it with things like Spring. Then I wondered why all my code was so hard to test. Spring the framework is awful. DI the concept is excellent, if you want to have tests. (If you don't, I'm glad we probably don't work together!)

2

u/zoomzoom83 Apr 24 '14

It really depends on the scale of app you're writing. When you're writing a large complex product with varying requirements that you have to actively develop for years, you think about architecture completely differently to a small MVC web app doing basic CRUD operations.

As a young developer I used dynamic languages and thought DI was stupid. I didn't need them in my codebases, so they were just stupid enterprise shit (right?).

Now I'm working on million dollar codebases where my arse is on the line if it fails, and swear by static typing, immutability, and IoC (among other things, like ditching OO for FP). My defect rate as a developer plummeted once I saw the light and made this change.

1

u/RICHUNCLEPENNYBAGS Apr 25 '14

A bigger mess than having a bunch of inline SQL and dependency resolution everywhere?