Skip to Content
精通模块化JavaScript
book

精通模块化JavaScript

by Nicolás Bevacqua
April 2020
Intermediate to advanced
177 pages
1h 45m
Chinese
Publishing House of Electronics Industry
Content preview from 精通模块化JavaScript
3.2
 
CRUST
原则
61
只要你认为有必要,就可以采取这种方法 :如果能够从跨项目可重用的功
能中获益,就可以这样做,在此过程中还要添加测试和文档。请注意,过
度模块化带来的回报是递减的 ;越是把模块化做到极致,在文档和测试上
花费的时间就越多。如果你打算把开发的每一行代码都作为独立的有完备
文档和测试的包来部署,就会在与所开发的特性或所修复的
bug
没有直接
关系的任务上花费相当多的时间。还是那句话,要根据你自己的判断来决
定将代码模块化到什么程度。
如果一段代码不复杂而且相当简短时,通常不值得为其创建模块。最好把
这段代码放在模块的一个函数中以便调用,或者每次都将其内联。这样的
短代码片段往往会需要进行修改和扩展,在代码库的不同部分中其实现略
有不同。因为代码量非常小,所以几乎不值得花时间找出一种方法来总结
所有甚至只是大多数用例中的这种小代码段。否则,与一开始时只是内联
这段代码相比,我们很可能会遇到一些更复杂的问题。
当一段代码足够复杂,足以独立成模块时,并不意味着值得立即为其创建包。
外部模块通常需要做更多的维护,换来的好处是,外部模块可以跨代码库
重用,并提供更整洁的接口和周详的文档。考虑一下你需要花多少时间来
分离模块和撰写文档,以及这是否值得。如果模块要依赖其所属代码库中
的其他代码,那么分离模块就比较有挑战,因为该模块所依赖的部分也必
须被分离出来。通常,我们不需要为代码库的每个模块都撰写文档。然而,
如果模块是独立的包,则必须为其撰写文档,因为你不能指望其他潜在的
使用者在不知道该依赖包具体做什么或如何使用的情况下就能决定是否要 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
基于Python的智能文本分析

基于Python的智能文本分析

Benjamin Bengfort, Rebecca Bilbro, Tony Ojeda
Lua游戏AI开发指南

Lua游戏AI开发指南

Posts & Telecom Press, David Young

Publisher Resources

ISBN: 9787121387074