O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering Modular JavaScript

Book Description

If you have a working knowledge of JavaScript and ECMAScript 6 (ES6), this practical guide will help you tackle modular programming to produce code that’s readable, maintainable, and scalable. You’ll learn the fundamentals of modular architecture with JavaScript and the benefits of writing self-contained code at every system level, including the client and server.

Nicolás Bevacqua, author of Practical Modern JavaScript, demonstrates how to scale out JavaScript applications by breaking codebases into smaller modules. By following the design practices in this book, senior developers, technical leaders, and software architects will learn how to create modules that are simple and flexible while keeping internal complexity in check.

  • Learn modular design essentials, including how your application will be consumed and what belongs on the interface
  • Design module internals to keep your code readable and its intent clear
  • Reduce complexity by refactoring code and containing and eliminating state
  • Take advantage of modern JavaScript features to write clear programs and reduce complexity
  • Apply Twelve-Factor App principles to frontend and backend JavaScript application development

Table of Contents

  1. Preface
    1. Who Should Read This Book
    2. Why Modular JavaScript?
    3. How Is This Book Organized?
    4. Conventions Used in This Book
    5. O’Reilly Safari
    6. How to Contact Us
    7. Acknowledgments
  2. Module Thinking
    1. 1.1 Introduction to Module Thinking
    2. 1.2 A Brief History of Modularity
      1. 1.2.1 Script Tags and Closures
      2. 1.2.2 RequireJS, AngularJS, and Dependency Injection
      3. 1.2.3 Node.js and the Advent of CommonJS
      4. 1.2.4 ES6, import, Babel, and Webpack
    3. 1.3 The Perks of Modular Design
    4. 1.4 Modular Granularity
    5. 1.5 Modular JavaScript: A Necessity
  3. Modularity Principles
    1. 2.1 Modular Design Essentials
      1. 2.1.1 Single Responsibility Principle
      2. 2.1.2 API First
      3. 2.1.3 Revealing Pattern
      4. 2.1.4 Finding the Right Abstractions
      5. 2.1.5 State Management
    2. 2.2 CRUST: Consistent, Resilient, Unambiguous, Simple, and Tiny
      1. 2.2.1 Consistency
      2. 2.2.2 Resiliency
      3. 2.2.3 Unambiguity
      4. 2.2.4 Simplicity
      5. 2.2.5 Tiny Surface Areas
  4. Module Design
    1. 3.1 Growing a Module
      1. 3.1.1 Composability and Scalability
      2. 3.1.2 Design for Today
      3. 3.1.3 Abstractions Evolve in Small Steps
      4. 3.1.4 Move Deliberately and Experiment
    2. 3.2 CRUST Considerations
      1. 3.2.1 Do Repeat Yourself, Occasionally
      2. 3.2.2 Feature Isolation
      3. 3.2.3 Trade-Offs When Designing Internals
    3. 3.3 Pruning a Module
      1. 3.3.1 Error Handling, Mitigation, Detection, and Solving
      2. 3.3.2 Documentation as an Art
      3. 3.3.3 Removing Code
      4. 3.3.4 Applying Context
  5. Shaping Internals
    1. 4.1 Internal Complexity
      1. 4.1.1 Containing Nested Complexity
      2. 4.1.2 Feature Entanglement and Tight Coupling
      3. 4.1.3 Frameworks: The Good, the Bad, and the Ugly
    2. 4.2 Refactoring Complex Code
      1. 4.2.1 Embracing Variables over Clever Code
      2. 4.2.2 Guard Clauses and Branch Flipping
      3. 4.2.3 An Interdependency Pyramid
      4. 4.2.4 Extracting Functions
      5. 4.2.5 Flattening Nested Callbacks
      6. 4.2.6 Factoring Similar Tasks
      7. 4.2.7 Slicing Large Functions
    3. 4.3 State as Entropy
      1. 4.3.1 Current State: It’s Complicated
      2. 4.3.2 Eliminating Incidental State
      3. 4.3.3 Containing State
      4. 4.3.4 Leveraging Immutability
    4. 4.4 Data Structures Are King
      1. 4.4.1 Isolating Data and Logic
      2. 4.4.2 Restricting and Clustering Logic
  6. Modular Patterns and Practices
    1. 5.1 Leveraging Modern JavaScript
      1. 5.1.1 Template Literals
      2. 5.1.2 Destructuring, Rest, and Spread
      3. 5.1.3 Striving for Simple const Bindings
      4. 5.1.4 Navigating Callbacks, Promises, and Asynchronous Functions
    2. 5.2 Composition and Inheritance
      1. 5.2.1 Inheritance Through Classes
      2. 5.2.2 The Perks of Composition: Aspects and Extensions
      3. 5.2.3 Choosing Between Composition and Inheritance
    3. 5.3 Code Patterns
      1. 5.3.1 Revealing Module
      2. 5.3.2 Object Factories
      3. 5.3.3 Event Emission
      4. 5.3.4 Message Passing and the Simplicity of JSON
  7. Development Methodology and Philosophy
    1. 6.1 Secure Configuration Management
    2. 6.2 Explicit Dependency Management
    3. 6.3 Interfaces as Black Boxes
    4. 6.4 Build, Release, Run
    5. 6.5 Statelessness
    6. 6.6 Parity in Development and Production
    7. 6.7 Abstraction Matters
  8. Index