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
76
4
内部构造
件还有
notify
功能,用于处理邮件的发送。更进一步地扩展这个想法,或
subscribers.notify
的大部分工作可以通过另一个名为
emails
的服务组
件完成,这个服务组件负责正确地配置邮件发送的功能,也有将可能的邮
件转换为单纯的
console.log
语句的功能,这样在调试期间就可以快速访问
邮件内容了。
一旦原型阶段结束,清晰地定义层(
layer
)对设计出高效且可维护的应用
程序就至关重要。层可以由遵循我们所使用的框架的约定的组件构成,也
可以是自定义的,就像前面讨论的服务层那样。通过使用层以及使用函数
参数而非作用域来传递上下文,我们可以将几个正交的组件放在一起来引
入水平伸缩,而不会让它们碰触彼此的关注点。
4.2
 重构复杂代码
代码是不断发展的,我们的代码最终不可避免会发展成大型项目,变得不
那么容易维护。后面的几节会在架构层面谈一谈关于降低复杂性的实践,
本节将重点讨论如何降低应用程序中已经很复杂的那部分代码的复杂性。
4.2.1
 多用变量,少写“巧妙”的代码
复杂的代码明显比它原本应该有的长度要短,这一点通常具有欺骗性。一
个可能需要
5~10
行短代码表达的逻辑,通常只需要一两行巧妙的代码
clever code
)就可以表述。而这一两行巧妙代码的问题在于,只有在我们
第一次写完这些代码时或者花费了可观的时间进行分析后,对其意图才有
清晰的认识,不然就需要花时间与精力去弄清楚。
阅读复杂代码时可以发现的一个潜在问题是,它们用的变量很少。在编程
4.2
 重构复杂代码
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