[英文版]專訪《AngularJS 2權威教程》作者Nate Murray: Code is More of A Graph Than A Document (圖靈訪談)

劉敏ituring發表於2016-08-19

Guest:

[英文版]專訪《AngularJS 2權威教程》作者Nate Murray: Code is More of A Graph Than A Document (圖靈訪談)

Nate Murray
內特是一名全棧開發人員,編碼工作從深度學習領域的影像識別到手機遊戲的開發。曾供職於IFTTT,處理MapReduce,分散式計算,iOS應用程式和一些Web應用程式中的T級大規模資料集挖掘工作。最近和Ari Lerner、Felipe Coury、Carlos Taborda合著了《AngularJS 2權威教程》一書。

歡迎到Nate個人網頁瞭解更多內容!

>




除了程式設計開發之外,生活中玩音樂、養蜜蜂。

參與開發了一款專為“喵星人”設計的遊戲,該遊戲被《紐約時報》《洛杉磯時報》《ABC 新聞》等多家報社報導並評選入圍“前100款熱門iPad娛樂類應用軟體”。

此外,在Github上有很多的開源專案:

  • cascading-simhash
  • Smoker
  • Similarity
  • Chordjerl
  • ...

《AngularJS 2權威教程》堪稱AngularJS 2領域的里程碑式著作,涵蓋了關於AngularJS 2的幾乎所有內容。

即使沒有任何經驗,本書平實、通俗的講解,遞進、嚴密的組織,也可以讓你毫無壓力地登堂入室,迅速領悟新一代Web應用開發的精髓。如果你有相關的經驗,本書對AngularJS 2概念和技術細節的全面剖析,以及引人入勝、切中肯綮的講解,將幫助你徹底掌握這個框架,在自己職業技術修煉之路上更進一步。

Interview transcript

See Chinese version

Turing: Recently, Turing Book Company is translating your co-authored book ng-book 2. Turing Interview feels obliged and honored to have a talk with Nate. What does Angular mean to you so that you want to write a book for it? Or what are the advantages of Angular impressing you, authors, overwhelmingly?

Nate: Angular provides a fantastic framework for writing and organizing your web apps. Component-based web development has shown itself to be a scalable, comprehensible way to write large, maintainable apps. In the past we would manually read and mutate the DOM using a library such as jQuery, and that kind of code, as many of us know, can become very unwieldy. On the other hand, breaking your code down into components breaks the system down so that each component is concerned with a particular part of the view and so the whole app is easier to reason about.

Turing: Is ng-book 2 an upgrade to ng-book 1?

Nate: No - it's a completely new book. Angular 2 and Angular 1 share a common thought-ancestry but the frameworks are pretty different. The Angular 2 book takes not only the new library, but everything we've learned over the years about web development into a completely new book.

Turing: How is the collaboration between your four authors when writing ng-book 2 ? Which parts are in your charge?

Nate: I've mainly been in charge of outlining, researching, editing and coordinating the whole project and I ended up writing a little less than half of the chapters. Felipe Coury also contributed a huge amount of the content and he's done a fantastic job. The main challenge when working with so many authors is unifying the voice, but thankfully we all share a common vision and so that hasn't been much of an issue in practice.

Turing: What do you think of the relation between code and literature?

Nate: Ideally your code is clear enough that it wouldn't need any supporting documentation. Even though we say that code should be written for humans to understand before machines, in practice technological constraints often dictate ordering of code (and mixing of concerns) that aren't optimal for someone learning a codebase for the first time.

There have been lots of takes on addressing this issue, particularly in the area of "literate programming". I think IPython/Jupyter is one of the most mature tools in this space. However, I think literate programming tools often require all code to be present and presented in a linear order. This often means that the examples you can use with them end up being trivial apps and you can't really learn large structures from them. Code is more of a graph than a document and so you need a way to hop around.

One of my latest attempts on this problem is a tool we call cq. The idea is that you can extract snippets of code using CSS-style selectors to pull them into your documentation. We use this for all of our books and find that it really helps cut down on typos and maintenance (because we always load our code blocks from runnable code on disk). Because the selectors are aware of the code structure it has the advantage of being robust in the face of line number changes.

It's certainly not the last word in mixing writing and code, but it's helped us a lot.

Turing: According to your profile, we know that you've been working with large-scale data since 2009. In this sense, you could give some valuable advice on data problems proposed by readers. There are situations where we will work with data within its parent scope. Then how to implement the communication between a scope and its parent scope? Is $broadcast/$emit/$on the best way?

Nate: When you mention $broadcast/$emit/$on, I assume you're referring to Angular 1 apps. ng-book 2 is primarily about Angular 2 which doesn't have these directly, but the concepts below still apply.

In "classic" Angular $broadcast/$emit was definitely the first tool of choice when passing data between scopes, but my thinking and the tools have evolved a lot since then. This problem is really a specific case of deciding the data-architecture of your app generally. Three other options come to mind:

  • Instead of using $broadcast/$emit use a service which has an EventEmitter and manage your state through that service

  • If you like Observables, instead of EventEmitter, have your service hold RxJS streams and let your components subscribe to changes on those streams

  • Get rid of $scope altogether and use Redux.

Redux is one of the most interesting data architecture patterns to emerge this year. The idea is that you have all of your data in a central data structure which is never mutated directly. Instead you dispatch "actions" and then you pass your old state and the action to a function which returns a new state (the term for this function is a reducer). I'd recommend ditching $emit/$broadcast and $scope altogether and using a Redux store for state for any brand-new Angular projects.

Turing: When dealing with large-scale data, ng-repeat gets stuck somewhere occasionally. What's your advice on its better performance?

Nate: A good idea here is to show only a limited number and then recycle DOM elements (instead of showing the whole list and creating new elements for each one).

Say you're repeating a list of divs that are in a scrollable container. You can limit the maximum number of divs in the DOM to, say, a pool of 100 divs. Then listen to the scrolling on the container element and populate the divs as appropriate. It takes a bit more work to set this up, but you'll get much better performance.

Turing: Now, let's forecsat the future of AngularJS. Is it possible for AngularJS to cover back-end development and become a back-end solution like Django in the future?

Nate: To my knowledge, becoming a backend framework like Django or Rails isn't a stated goal of the Angular project. Angular is a strong front-end framework that works in the browser on the DOM and is able to make HTTP requests to any APIs it needs to populate data.

That said, Angular 2 has been built such that it will support multiple platforms, most notably mobile development. Being able to create mobile clients in Angular 2 with NativeScript or Ionic is really exciting.

On the server-side, Patrick Stapleton and a group of contributors have done some great work on Angular Universal which is a way to have your server run and ‘boot up’ your app which, in theory, should result in better performance for the client.

Turing: Do you think that AngularJS will be encapsulated into Browsers one day?

It's hard to say but probably not as such. If anything it would be the other way around: the browser's functionalities will grow to make some of the parts of Angular unnecessary. Web Components is a good example of something browsers may eventually support more broadly that could make @Component unnecessary.

Another part browsers might provide are change detection like Zones - we almost saw browser implementations of Object.observe, which was a lot like Angular's $watch. But this didn't end up becoming part of the ECMA standard, which is probably fine.

But Angular 2 has a lot of other functionality beyond components like routing, dependency injection, testing frameworks, etc. These frameworks are great, but like any framework they have opinions that may not fit every situation and so I don't know that they will ever be integrated into the browser.

Turing: AngularJS is powerful but complex. Could you give some learning suggestions for beginners?

Nate: The best way to learn any programming framework is to build something with it. So my first learning suggestion to beginners is to go to the official documentation website and try to follow the walkthrough. Both Angular 1 and Angular 2 have walkthrough tutorials on their site.

The next thing I'd recommend to beginners is a resource that walks through step-by-step with lots of sample code. I'm biased, but I think ng-book 1 and ng-book 2 are the best places to get that. Egghead.io also has some great video courses.

——More Interviews


更多精彩,加入圖靈訪談微信!

相關文章