-
Notifications
You must be signed in to change notification settings - Fork 26.6k
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
Explain why not to prefix methods with underscores #1025
Conversation
@@ -450,6 +450,7 @@ | |||
``` | |||
|
|||
- Do not use underscore prefix for internal methods of a React component. | |||
> Why? As Doug Crockford writes: "Do not use _ *underbar* as the first or last character of a name. It is sometimes intended to indicate privacy, but it does not actually provide privacy. If privacy is important, use closure. Avoid conventions that demonstrate a lack of competence." Source - http://javascript.crockford.com/code.html. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like Crockford's advice, but I don't think we need to propagate his tone here by quoting him. Could we write our own statement that conveys the same meaning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might take me a couple days, its a busy work week this week. Also want to run this by a few developers I work with.
5bd2769
to
93bf976
Compare
@@ -450,6 +450,7 @@ | |||
``` | |||
|
|||
- Do not use underscore prefix for internal methods of a React component. | |||
> Why? Underscore prefixes are sometimes used as a convention in other languages to denote privacy. But, unlike those languages, there is no native support for privacy in JavaScript, everything is public. Regardless of your intentions, adding underscore prefixes to your properties does not actually make them private, and any property (underscore prefixed or not) should be treated as being public. See issues #1024, and #490 for a more in-depth discussion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there shouldn't be two spaces after "intentions,", and let's do "underscore-prefixed".
In what other languages is a single underscore used as a convention for privacy? I'm only aware of it in JS. PHP (and I think Python) use double underscores, but that's not the convention we're addressing here.
Otherwise, looks good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PHP uses protected and private keywords. there's no need to use underscores
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, I was referring to PHP's magic methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ljharb I'm only aware of the convention in three other languages (there may be more):
Python
_single_leading_underscore : weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore. - PEP8 StyleGuide
C#
I'm not an ex-C# programmer, but my colleagues are, and they tell me this is where their use of the underscore prefixes to denote privacy came from.
We use camelCase for internal and private fields and use readonly where possible. Prefix instance fields with , static fields with s and thread static fields with t. When used on static fields, readonly should come after static (i.e. static readonly not readonly static). - .NET team styleguide
Objective-C
Make sure the name of the instance variable concisely describes the attribute stored. Usually, you should not access instance variables directly, instead you should use accessor methods (you do access instance variables directly in init and dealloc methods). To help to signal this, prefix instance variable names with an underscore (_), for example - Apple Guidelines for Cocoa?
LGTM - would you mind rebasing this down to a single commit? |
11177bf
to
9b035cc
Compare
9b035cc
to
a43c16e
Compare
@ljharb reabsed into one commit, and up-to-date with master. |
Fixes issue #1024