question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Underscore prefixes for "private" properties

See original GitHub issue

From the main section:

Use a leading underscore _ when naming private properties.

From the React section:

Do not use underscore prefix for internal methods of a React component.

These two seem a bit inconsistent.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:11
  • Comments:36 (10 by maintainers)

github_iconTop GitHub Comments

21reactions
ljharbcommented, May 4, 2016

@taion if your goal is helpful suggestions, then name it this.private_address_do_not_use, or SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED. Naming it _address is just obscure.

20reactions
nicboucommented, May 4, 2016

In principle, yes, but underscores are a widely understood indicator that a variable is “private”. You don’t walk through a door with a sock on the handle, even if it’s unlocked.

Since there are no private properties in ES6 classes, how can I indicate “private” methods and properties? This is especially problematic with getters/setters in ES6, since it needs to be stored in a separate variable.

// Simplified example
export class User {
  get address() { return this._address; }
  set address(address) { this._address = address; }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is the underscore prefix for property and method names ...
_ prefixed variable names are considered private by convention but are still public. This syntax tries to be both terse and intuitive, although...
Read more >
Configure Visual Studio to Name Private Fields with Underscore
Configure Visual Studio to Name Private Fields with Underscore · Click on Tools in the menu. · Click on Options. · Click on...
Read more >
use of "_" prefix to designate "private"/"library" scope...
defines not only underscore as private, but and 'm' prefix as library ... and it isn't as easy to tell which properties are...
Read more >
Using underscore to prefix private member fields
I personally prefer to use the underscore prefix on private member fields and mainly so they would be easily identifiable from local ...
Read more >
Underscore prefix for private method names : r/PHP - Reddit
Underscore prefixes on function/method names is an old PHP 4 convention to indicate private. It's used in a number of languages that have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found