- Mastering Node.js(Second Edition)
- Sandro Pasquali Kevin Faaborg
- 266字
- 2021-07-02 19:28:46
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.
推薦閱讀
- Building E-commerce Sites with VirtueMart Cookbook
- 物聯(lián)網(wǎng)網(wǎng)絡(luò)安全及應(yīng)用
- TCP/IP入門經(jīng)典(第5版)
- Learning Karaf Cellar
- 紅藍攻防:構(gòu)建實戰(zhàn)化網(wǎng)絡(luò)安全防御體系
- 全聯(lián)網(wǎng)標(biāo)識服務(wù)
- 圖神經(jīng)網(wǎng)絡(luò)前沿
- Professional Scala
- 物聯(lián)網(wǎng)
- 現(xiàn)場綜合化網(wǎng)絡(luò)運營與維護:運營商數(shù)字化轉(zhuǎn)型技術(shù)與實踐
- React Design Patterns and Best Practices(Second Edition)
- 路由與交換技術(shù)
- 學(xué)術(shù)虛擬社區(qū)用戶社會化交互行為研究
- 目標(biāo)跟蹤中的群智能優(yōu)化方法
- 網(wǎng)絡(luò)是怎樣連接的