-
Notifications
You must be signed in to change notification settings - Fork 34
Description
Moved from cfpb/consumerfinance.gov#919
My understanding of the underscore convention is:
Use an underscore to designate a variable or function in the export scope (the nesting level where module.exports is called) as private, except if it's used to refer to an imported class or third-party naming convention (jquery, lodash, etc.).
That last bit is where it becomes unclear. Do imported packages that are not using a convention or are not a class get an underscore or not?
Pro: Makes it consistent with how other variables are used.
Con: Things like var handlebars are not clear on their convention.
No underscore
Pro: Makes all imported modules consistently not use underscores. A variable without an underscore is either: an imported module, a function parameter, or a local variable.
Cons: Not consistent with how other variables that don't get exported are defined in the export level scope.
Example file:
var $ = require( 'jquery' ); // No underscore needed b/c it uses a well-established convention name.
var Widget = require( 'Widget' ); // No underscore needed b/c it's a class instantiated with `new`.
var pkg = require( 'pkg' ); // No underscore needed b/c it's a third-party package to this module, name follows filename.
// or
var _pkg = require( 'pkg' ); // Underscore needed b/c it's a third-party package that doesn't have an established convention and it's not being exported.
var _globalVar = true; // Underscore needed b/c it's in the module.exports scope.
// Underscore needed for `_helper` b/c it's in the module.exports scope,
// no underscored needed for `thing` b/c it's in a local scope.
function _helper( thing ) {
// private api implementation.
var localVar = new Widget(); // No underscore needed b/c it's in the scope of _helper.
}
function Example() { // No underscore, b/c it's exported publicly.
// public api implementation.
_helper( 'stuff' );
}
module.exports = Example;
Which convention seems best to you?