# Modular Thinking

2 min read, 280 words modularthinking

Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.


# Modularity in javascript

A brief history

  1. Initially using <script> tags
  2. Dependency Injection (opens new window) Pattern
    1. Dependencies are injected to the client as a service
  3. CommonJS module system, or CJS for short.

Dependency Injection

In CommonJS, each file is a module with its own scope and context. Dependencies are loaded using a synchronous require function that can be dynamically invoked at any time in the lifecycle of a module, as illustrated in this snippet:

const mathlib = require('./mathlib')

# Modularity Design Principles

  1. Cyclomatic complexity is the number of unique code paths a program can take, and it may be a better metric when measuring the complexity of a component.
  2. single responsibility principle (SRP)
  3. API first, A module is only as good as its public interface
  4. CRUST
    1. Consistent: Humans excel at identifying patterns, and we do so while reading as well
    2. Resilient: to error conditions and boundary values
    3. Unambiguous: The output shape for a function shouldn’t depend on how it received its input or the result that was produced.
    4. Simple and Tiny: configuration driven

# References

Subscribe to our Newsletter

If you like my work and think it was helpful kindly support my work


Buy Me A Coffee