Module definition styles and dependencies

In a previous post I wrote about using amdefine to solve the RequireJS front-end CommonJS back-end module definition issue. In this post I'd like to focus on applying some stylistic polish. I gave the simple example:

RequireJS – Horizontal dependency listing styles

if (typeof define !== 'function') { var define = require('amdefine')(module) }

define(['underscore', './example-dependency'], function(_, ExampleDependency) {  
  //return an object to define the module
  return {
    ...
  }
});

The definition part of the code uses the standard require style of providing an array of dependencies, then defining the names of the variables to be used inside the module in the arguments of the closure. In modules with many dependencies these dependencies are usually listed vertically as shown below:

RequireJS – Vertical dependency listing styles

if (typeof define !== 'function') { var define = require('amdefine')(module) }

define([  
  'underscore',
  './example-dependency1',
  './example-dependency2',
  ...
  './example-dependency100'
  ],
  function(
    _,
    ExampleDependency1,
    ExampleDependency2,
    ...
    ExampleDependency100
  ) {
  //return an object to define the module
  return {
    ...
  }
});

This is hard to follow at best, and at worst can lead to errors when adding or removing dependencies as the closure arguments are directly linked to the order of the items in the array. The standard Node.js style of expressing dependency is much cleaner allowing items to be added without the risks highlighted above:

CommonJS style

define(function(require, exports, module) {  
  var _ = require('underscore');
  var ExampleDependency1 = require('./example-dependency1');
  var ExampleDependency2 = require('./example-dependency2');
  ...
  var ExampleDependency100 = require('./example-dependency100');

  return{
    ...
  }
});

Fortunately, the combination of RequireJS and amdefine allows us to express dependencies using the syntax more familiar to Node.js development, yielding the following tidier hybrid:

A hybrid solution

if (typeof define !== 'function') { var define = require('amdefine')(module); }

define(function(require, exports, module) {

  var _ = require('underscore');
  var ExampleDependency1 = require('./example-dependency1');
  var ExampleDependency2 = require('./example-dependency2');
  ...
  var ExampleDependency100 = require('./example-dependency100');

  return {
    ...
  };

});