官术网_书友最值得收藏!

Considerations

Any developer is regularly making decisions with a far-reaching impact. It is very hard to predict all the possible consequences resulting from a new bit of code, or a new design theory. For this reason, it may be useful to keep the shape of your code simple, and to force yourself to consistently follow the common practices of other Node developers. These are some guidelines you may find useful, as follows:

  • Generally, try to aim for shallow code. This type of refactoring is uncommon in non-evented environments. Remind yourself of it by regularly re-evaluating entry and exit points, and shared functions.
  • Consider building your systems using distinct, composable microservices, which we'll discuss in Chapter 9, Microservices.
  • Where possible, provide a common context for callback re-entry. Closures are very powerful tools in JavaScript, and by extension, Node, as long as the context frame length of the enclosed callbacks is not excessive.
  • Name your functions. In addition to being useful in deeply recursive constructs, debugging code is much easier when a stack trace contains distinct function names, as opposed to anonymous.
  • Think hard about priorities. Does the order, in which a given result arrives or a callback is executed, actually matter? More importantly, does it matter in relation to I/O operations? If so, consider nextTick and setImmediate.
  • Consider using finite state machines for managing your events. State machines are surprisingly under-represented in JavaScript codebases. When a callback re-enters program flow, it has likely changed the state of your application, and the issuing of the asynchronous call itself is a likely indicator that state is about to change.
主站蜘蛛池模板: 永州市| 永川市| 江津市| 泸定县| 尉犁县| 泸溪县| 鹤壁市| 延边| 平阴县| 集贤县| 乐东| 泉州市| 墨竹工卡县| 耒阳市| 筠连县| 长宁区| 册亨县| 巴林右旗| 灵山县| 无极县| 柳河县| 珠海市| 定陶县| 宜阳县| 香格里拉县| 襄汾县| 潜山县| 淅川县| 怀远县| 金湖县| 林甸县| 兴隆县| 康乐县| 富蕴县| 察隅县| 通海县| 瓮安县| 大邑县| 孙吴县| 图木舒克市| 视频|