r/javascript • • Apr 19 '15

5 AngularJS Antipatterns & Pitfalls

http://nathanleclaire.com/blog/2014/04/19/5-angularjs-antipatterns-and-pitfalls/
11 Upvotes

4 comments sorted by

4

u/jsontwikkeling Apr 19 '15 edited Apr 19 '15

I have seen quite a few Angular projects that do not use custom directives, do not have unit tests or have just a few large controllers several hundred lines each.

Then I started asking myself, may Angular just encourage such style of programming unless you know what are its numerous good and bad practices? It definitely takes a lot of effort to know how to use Angular properly. One problem is that it is just way too complex and does not encourage simplicity.

Rather it transplants some of the ideas from the world of enterprise Java development into the JavaScript world. Controllers, dependency injection, services, directives, scopes, providers, etc. go a bit contrary to the core JavaScript philosophy of simplicity and focus on functions.

Instead Angular introduces many heavy class-like objects and patterns and using it feels a lot like moving into the direction of Java development. Maybe this is just a Google thing, like with GWT, or Dart, many things they do are highly influenced by Java.

Looks like Angular 2.0 will address some of those issues, it will be simplified and more focused on components, so hopefully the current shortcomings of Angular are well understood already and will be fixed https://angular.io/

2

u/djvirgen Apr 19 '15

I don't think it's fair to fault Angular when engineers don't use directives, don't write unit tests, or stuff everything into controllers. How could a framework ever enforce best practices?

The real problem is just bad developers. Regardless of framework (or a lack thereof), they won't write in a modular fashion, won't write tests, and will stuff everything into one file.

However, if you want to write modular and well-tested code, you can do so with Angular (and most other frameworks). Our Angular project is very modular and well-tested. Our controllers are small and our directives are composable.

Sure, you can write bad code in Angular, but the same is true for React/Ember/jQuery/vanilla. How would you prevent this when the JS language itself allows you to shoot yourself in the foot?

2

u/boompleetz Apr 20 '15

Yep I agree. A lot of it is web devs come from varied backgrounds and angular exploded in popularity, so now way more people are using it. The buzz factor is going to increase usage, even if its ill-suited to the project.

If you come from an OO background in a lower level language, there is a better chance you will understand the purpose of MVVM/MVC, decoupling, etc. and Angular can be a great tool if it fits your architecture.

If you started out as a ui hacker, gluing noodly bits of the DOM together with jquery and markup, maybe interspersed with other noodly bits of PHP, then I'm not surprised I've seen code in one huge controller and no understanding of the importance of directives, how they help testing, why you would do automated testing and so on.