Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work out use of underscore in JS in this repo and document in front-end standards #919

Closed
anselmbradford opened this issue Aug 20, 2015 · 3 comments

Comments

@anselmbradford
Copy link
Member

As we grow our JS it would be good to work out in FEWD standards how we use underscore-prefixed names, as it's not entirely clear currently.

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? We use both conventions:

underscore imports
underscore imports
underscore imports
underscore imports - well, actually handlebars is almost definitely a convention

no underscore imports
no underscore imports
no underscore imports
no underscore imports
no underscore imports
no underscore imports

Underscore

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?

@anselmbradford
Copy link
Member Author

The browser tests are also a mix of the two styles.

@cwdavies
Copy link
Contributor

@anselmbradford Has the use of underscore in JavaScript been documented in front-end standards? Can this issue be closed?

@anselmbradford
Copy link
Member Author

Has the use of underscore in JavaScript been documented in front-end standards? Can this issue be closed?

It has not, but I've moved this issue to our development standards repo cfpb/development#214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants