`
leonzhx
  • 浏览: 786726 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 7. Modularity

阅读更多

 

1.  To make interdependence of code easier to keep track of, we need to divide their programs into modules, each with its own task, and minimize the amount of “coupling” between these modules—the amount of detail the modules have to “know” about each other.

 

2.  A module can be any collection of functions and values that, together, fulfill some specialized role. A module should expose an interface just like an object does. In fact, a module can consist of just a single object (and its interface).

 

3.  One obvious way to separate modules is by putting every module in a different file. This makes it clear which code belongs to which module.

 

4.  In JavaScript, “top-level” variables all live together in a single space. When a lot of code is loaded into an environment, it becomes hard to keep track of which variable names are used, which makes it very easy to accidentally use a name that was already used for something else. Big modules should try to use as few top-level variable names as possible and not put their internal variables into the top-level environment.

 

5.  The standard trick for hiding variables from the rest of the world is to use a function as a local module namespace. The whole module is written inside a function, and the interface of the module is explicitly put into the top-level environment. It does that by setting properties in the window object, which is an object whose properties represent the top-level variables. (in the browser environment):

function provide(values) {
  forEachIn(values, function(name, value) {
    window[name] = value;
  });
}
(function() {
  var names = ["January", "February", "March", "April", "May", "June", "July",
               "August", "September", "October", "November", "December"];
  provide({
    getMonthName: function(number) {return names[number];},
    getMonthNumber: function(name) {
      for (var number = 0; number < names.length; number++) {
        if (names[number] == name) return number;
      }
    }
  });
})();

 

6.  Some modules export so many variables that it is a bad idea to put them all into the top-level environment. You can represent the module as a single object whose properties are the functions and values it exports. 

 

7.  When there is another module or part of the standard JavaScript functionality that does something similar to what you are implementing, it might be a good idea to make your interface resemble the existing interface. That way, people who know the existing interface will feel right at home.

 

8.  In your interfaces, try to use the simplest data structures that work and make functions do a single, clear thing—if possible, they should be pure functions.

 

9.  When designing an interface for a complex piece of functionality, Often the solution is to provide two interfaces: a detailed “low-level” one for advanced use and a simple “high-level” one for straightforward situations. The second one can usually be built very easily using the tools provided by the first one.

 

10.  Wrapping the optional arguments in an object make the invoking codes more readable (no need to skip the argument by setting it to null). However, it has become harder to figure out which arguments are supported. You should put the comment before the function, listing the optional arguments.

 

11.  jQuery, YUI, Prototype, and ExtJS are toolkits libraries.

 

 

分享到:
评论

相关推荐

    Java.9.Programming.By.Example.epub

    Chapter 7. Building A Commercial Web Application Using Rest Chapter 8. Extending Our E-Commerce Application Chapter 9. Building An Accounting Application Using Reactive Programming Chapter 10. ...

    和大家分享一些Matlab卡尔曼滤波的资料-ADSP-Chapter3.pdf

    4. 结构特性(Structure):包括模块化(modularity)、并行性(parallelism)、并发性(concurrency),这些特性使得滤波器适合使用VLSI实现。 5. 收敛速度(Convergence speed):滤波器达到稳定状态所需的迭代...

    PHP.7.Real.World.Application.Development

    Chapter 7: Accessing Web Services Chapter 8: Working with Date/Time and International Aspects Chapter 9: Developing Middleware Chapter 10: Looking at Advanced Algorithms Chapter 11: Implementing ...

    Practical.Foundations.for.Programming.Languages.2nd

    Chapter 7 Evaluation Dynamics Part III Total Functions Chapter 8 Function Definitions and Values Chapter 9 System T of Higher-Order Recursion Part IV Finite Data Types Chapter 10 Product Types ...

    The Essentials of Interaction Design

    Chapter 7 - Architecture Representation Chapter 8 - Quality Models and Quality Attributes Chapter 9 - Architectural Design Principles Chapter 10 - Applying Architectural Styles and Patterns ...

    C 程序设计教学课件:Chapter 3 class and object.ppt

    This promotes reusability and modularity in code design. Static members in C++ are shared among all instances of a class. Static data members are common to all objects of a class, and their value is...

    JavaScript.Application.Design.A.Build.First.Approach

    Chapter 7 Leveraging The Model-View-Controller Chapter 8 Testing Javascript Components Chapter 9 Rest Api Design And Layered Service Architectures Appendix A Modules In Node.Js Appendix B ...

    HPL: Vol. IV: Functional and Logic Programming Languages

    5.4.3. Multiple Inheritance: Using Mix-ins to Get Modularity 5.4.4. Inheritance Rules: Precedence 5.4.5. Methods and Generic Functions 5.4.6. Before, After, and Around Methods 5.4.7. Operator ...

    spring dm in action sample chapter6

    The chapter emphasizes the importance of modularity and strict dependency management in enterprise application development. #### Using Traditional Java EE Frameworks in OSGi Environments When ...

    Re-Engineering.Legacy.Software

    Chapter 7 Automating the development environment Chapter 8 Extending automation to test, staging, and production environments Chapter 9 Modernizing the development, building, and deployment of legacy ...

    软件工程教学课件:Chapter_09_Component-Level Design.ppt

    Chapter 9 of the "Component-Level Design" software engineering lecture focuses on understanding and creating effective designs for software components. Components play a crucial role in modern ...

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    dynamic, untyped, lightweight, and interpreted programming language and functional programming is a style that emphasizes and enables smarter code that minimizes complexity and increases modularity....

    matlab教程 - ME160指南

    Chapter 8: Functions and Function Handles introduces the concept of functions, which encapsulate code for reuse and modularity. Function handles, a key feature in MATLAB, enable function callbacks and...

    Scala and Spark for Big Data Analytics.pdf

    discoverability, modularity, and extensibility. In particular, we will see how to deal with variables in Scala; methods, classes, and objects in Scala; packages and package objects; traits and trait ...

    Object-Oriented Software Construction 2nd

    Chapter 7: The static structure: classes 165 7.1 OBJECTS ARE NOT THE SUBJECT 165 7.2 AVOIDING THE STANDARD CONFUSION 166 7.3 THE ROLE OF CLASSES 169 7.4 A UNIFORM TYPE SYSTEM 171 7.5 A SIMPLE CLASS ...

    [Mastering.Node.js(2013.11) 精通Node.js

    Chapter 1: Understanding the Node Environment 7 Extending JavaScript 9 Events 10 Modularity 12 The Network 13 V8 15 Memory and other limits 16 Harmony 18 The process object 19 The Read-Eval-Print Loop...

    Java 9 with JShell

    Each chapter will add to the full picture of Java 9 programming starting out with classes and instances and ending with generics and modularity in Java. What you will learn Engage with object-...

Global site tag (gtag.js) - Google Analytics