Friday, September 5, 2014

Announcing angular-dep.vim, a Vim Plugin for Angular.js Developers

One of the best practices in angular.js development that I have trouble adhering to is minifiable injectables.

I have always been a huge fan of dependency injection, and I am constantly advocating for it in my work projects that aren't fortunate enough to have it. When attempting to convince a developer who hasn't experienced the wonders of dependency injection and the slew of benefits it brings to testability, they only seem to think of it as an API. A verbose API.

That is part of why I am creating the language Wake, to make syntactically beautiful DI and to require its use. Because without a dedicated language doing so, dependency injection is inevitably more work - and that's the work you have to sell to that skeptical developer. Take java.

@Inject
public MyInjectedClass(MyDependency1 mdep1, MyDependency2 mdep2, MyDependency3 mdep3, MyDependency4 mdep4) {
    myDep1 = mdep1;
    myDep2 = mdep2;
    myDep3 = mdep3;
    myDep4 = mdep4;
}

protected MyDependency1 myDep1;
protected MyDependency2 myDep2;
protected MyDependency3 myDep3;
protected MyDependency4 myDep4;

I have stumbled over solving this problem in PHP, I can rarely talk spring developers out of using @Autowired on properties instead of constructors, it seems to be a constant battle.

So I was blown away and thrilled to find angular.js, an MVC framework with so many excellent features including beautiful dependency injection.


app.controller('MyCtrl', function($my, $injected, $items, here, and, here) {
    // code
});

It was like coming up for fresh air, and yes, I now finally have many friends who appreciate and understand dependency injection. It worked.

So when people tell me the truth that I don't want to hear,

You shouldn't use that syntax in your injectables. It isn't compatible with minifiers. Use ["$deps", "here", function($deps, here) { ... }] instead.

I grudgingly nod my head. Beautiful dependency injection without boilerplate had slipped through my fingers.

But this boilerplate is inherently predictable, so I decided I've had enough. I sunk some time into creating a plugin that manages the two for me, with extra goodies to add/remove/reorder the dependencies for me.

Here's a gif of it in action, recorded with the handy-dandy ttygif project.





Check it out on github! 


I suppose a vim plugin will have to do until Wake makes its first production release.