Live data from Hacker News

Classy: Cleaner class-based controllers for AngularJS

davej.github.io

31–40 of 59 posts

Re: Classy: Cleaner class-based controllers for AngularJS

#31

Earlier quoted context omitted.

Angular's dependency injection is still a matter of discussion and a lot of people are not completely happen with it. It'll also be made into something different for angular 2.0. Still, besides that: DRY is IMO not about these 2 lines that you've refactored away here. It's about whole functions/classes that have similar functionality where you're repeating yourself. Therefore, this would be a micro-optimisation with…

The point of DRY is to reduce the types of errors that repetition lends itself to. A very common member of that class of errors is "I should have repeated something, but I didn't". It is pretty easy to run into that error with Angular's injection - you can forget to add either the string or the argument, or (worse) you can get the order wrong. Eliminating the repetition fixes that class of errors, so I think it's wel…

I can see Classy adding value, but repetition with dependency injection is a solved problem if you use ngmin[1].

[1]: https://github.com/btford/ngmin

Re: Classy: Cleaner class-based controllers for AngularJS

#32
post #15

There is really no need for this. It adds nothing imo but another layer of sauce over what angular already does properly. If your controllers are getting huge, go refactor into directives and services. I don't see how this would help since there isn't even inheritance? also: app.controller('AppCtrl', ['$scope', '$location', '$http'], function($scope, $location, $http) { // ... }]); vs app.classy.controller({ name: 'A…

Thanks for the feedback. I would argue that the Classy example is more expressive and crucially it is DRY. Edit: > One of the brilliant things about angular is that it provides a proper structure that anyone that needs to write angular needs to adhere to Angular doesn't provide structure for controllers, they are just javascript functions. If you want to add structure it is up to the individual developer to decide ho…

Wouldn't you need to be doing `var self = this` in every function you have nested anonymous callbacks touching $scope though?

Re: Classy: Cleaner class-based controllers for AngularJS

#33

I've experienced the problem of unwieldy controllers myself, but angular is already such an intricate, opinionated framework that I can't imagine adding another layer with its own DSL. Good code organization solves a lot of these problems.

I've noticed that particularly in web development there is this trend of writing - and using - countless frameworks that add much more complexity with little gain, and where the response to existing complexity is to... add more of it. IMHO that doesn't seem like a very good way of doing things, since now you have to learn not only JS, but also all these extra indirections piled on top, to understand how your web application works. It's not doing any favours for the browser that has to run all this code either. On the whole, this just feels to me like an immensely wasteful way of doing things.

Re: Classy: Cleaner class-based controllers for AngularJS

#34

There is really no need for this. It adds nothing imo but another layer of sauce over what angular already does properly. If your controllers are getting huge, go refactor into directives and services. I don't see how this would help since there isn't even inheritance? also: app.controller('AppCtrl', ['$scope', '$location', '$http'], function($scope, $location, $http) { // ... }]); vs app.classy.controller({ name: 'A…

Look a little closer - It's not about saving bytes. I ended up coming up something very similar for my projects and it made a notable difference in defect rate. - Removes the need for positional arguments for constructor injection, so you only have to list your dependencies once. This one seems minor but can be a major frustration and a source of bugs when working with lots of services. - Moves actions out of the sco…

To be fair ngmin already fixes the injector problem, in most cases.

Re: Classy: Cleaner class-based controllers for AngularJS

#35

There is really no need for this. It adds nothing imo but another layer of sauce over what angular already does properly. If your controllers are getting huge, go refactor into directives and services. I don't see how this would help since there isn't even inheritance? also: app.controller('AppCtrl', ['$scope', '$location', '$http'], function($scope, $location, $http) { // ... }]); vs app.classy.controller({ name: 'A…

The feature of angular-classy that looks appealing to me is reducing the constant repetition of $scope. You still need to use this.$ or this.$scope when manipulating scope variables, but at least it hides it in the case of definition of scope methods.

Re: Classy: Cleaner class-based controllers for AngularJS

#36
post #32
post #15

Earlier quoted context omitted.

Thanks for the feedback. I would argue that the Classy example is more expressive and crucially it is DRY. Edit: > One of the brilliant things about angular is that it provides a proper structure that anyone that needs to write angular needs to adhere to Angular doesn't provide structure for controllers, they are just javascript functions. If you want to add structure it is up to the individual developer to decide ho…

Wouldn't you need to be doing `var self = this` in every function you have nested anonymous callbacks touching $scope though?

Yes or function.bind() or fat-arrow in Coffeescript. It's usually best practice to move that kind of stuff into a service instead though.

Re: Classy: Cleaner class-based controllers for AngularJS

#37

There is really no need for this. It adds nothing imo but another layer of sauce over what angular already does properly. If your controllers are getting huge, go refactor into directives and services. I don't see how this would help since there isn't even inheritance? also: app.controller('AppCtrl', ['$scope', '$location', '$http'], function($scope, $location, $http) { // ... }]); vs app.classy.controller({ name: 'A…

FWIW, Angular.js controllers have inheritance: http://stackoverflow.com/a/20230720/362006

Re: Classy: Cleaner class-based controllers for AngularJS

#38
post #31

Earlier quoted context omitted.

The point of DRY is to reduce the types of errors that repetition lends itself to. A very common member of that class of errors is "I should have repeated something, but I didn't". It is pretty easy to run into that error with Angular's injection - you can forget to add either the string or the argument, or (worse) you can get the order wrong. Eliminating the repetition fixes that class of errors, so I think it's wel…

I can see Classy adding value, but repetition with dependency injection is a solved problem if you use ngmin[1]. [1]: https://github.com/btford/ngmin

I disagree that ngmin is a full solution to the problem. It is a good and very helpful tool, but it's a hack and a pretty leaky one - I think it's often less of a hassle to just write all the explicit injection syntax myself than to fiddle with formatting to get ngmin to work and track down issues when it doesn't. Classy's solution seems much nicer to me.

Re: Classy: Cleaner class-based controllers for AngularJS

#39

Earlier quoted context omitted.

Look a little closer - It's not about saving bytes. I ended up coming up something very similar for my projects and it made a notable difference in defect rate. - Removes the need for positional arguments for constructor injection, so you only have to list your dependencies once. This one seems minor but can be a major frustration and a source of bugs when working with lots of services. - Moves actions out of the sco…

To be fair ngmin already fixes the injector problem, in most cases.

It's the "in most cases" that'll get ya!
Post reply on HN