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

Explain why not to prefix methods with underscores #1025

Merged
merged 1 commit into from
Aug 29, 2016

Conversation

ttmarek
Copy link
Contributor

@ttmarek ttmarek commented Aug 21, 2016

Fixes issue #1024

@@ -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.
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll think of something that'll encapsulate all the main ideas expressed in #1024, #490, and Crockford's notes.

Copy link
Contributor Author

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.

@ttmarek
Copy link
Contributor Author

ttmarek commented Aug 27, 2016

image

@@ -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.
Copy link
Collaborator

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!

Copy link

@skyrpex skyrpex Aug 27, 2016

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

Copy link
Collaborator

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.

Copy link
Contributor Author

@ttmarek ttmarek Aug 28, 2016

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?

@ljharb
Copy link
Collaborator

ljharb commented Aug 28, 2016

LGTM - would you mind rebasing this down to a single commit?

@ttmarek
Copy link
Contributor Author

ttmarek commented Aug 29, 2016

@ljharb reabsed into one commit, and up-to-date with master.

@ljharb ljharb merged commit a43c16e into airbnb:master Aug 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants