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 #214

Open
anselmbradford opened this issue Jul 22, 2021 · 0 comments

Comments

@anselmbradford
Copy link
Member

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?

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

No branches or pull requests

1 participant