Main menu

“Spaghetti code happens when concerns are mixed together”

As the JavaScript codebase grows, it becomes more complex (or, more of a mess). So the need to structure the code becomes more important.

Andrew Dupont gave a talk at MIX11 about how to make your JavaScript code more maintainable. Watch the presentation below.

Some notes

++ for modularity:

  • Code that accomplish one thing, lives in the same place
  • Keep it in separate files for development, and merge them into one for production
  • Hide the implementation from other modules

Decouple the modules

  • “The fewer friends a module has, the better”
  • Central Message bus: object that ensures message passing from “publisher object” to “subscribe objects”
    • Object broadcasts messages, others listen to it
    • Custom events
  • Although we try to use the message passing object as much as possible, we still do need to call some objects’ public api’s
    • So keep them consistent over time
    • It’s ok for a subscriber to call methods on a broadcaster but not the other way around

MVC pattern for architecture for “one page applications”

Use frameworks such as backbone.js

Keep html classes consistent over redesigns

We might want to use classes only for JavaScript targeting and not for styling