Skip to content

Underscore prefixes for "private" properties #490

Closed
@taion

Description

@taion
Contributor

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.

Activity

goatslacker

goatslacker commented on Aug 28, 2015

@goatslacker
Collaborator

A bit. The thing about React components is that all methods should be private so we just save ourselves the typing and don't insert the leading underscore.

taion

taion commented on Aug 28, 2015

@taion
ContributorAuthor

What do you mean? If I have e.g. a ref to an element, I can call methods on it just fine. I've seen this used to implement e.g. getValue methods on things like complex, uncontrolled input components for forms.

If you consider this an anti-pattern, it could be useful to add a clarifying comment to say to not do this. There are definitely problems with this pattern - it's not container-safe, for one.

afilp

afilp commented on Oct 25, 2015

@afilp

I am also interested in an explanation to that. Not all methods are "internal" if we consider that we may call a component's method from the parent.

Example:

parent.jsx

    _handleOpenChildDialog() {
        this.refs.childComponent.showDialog();
    }

childComponent.jsx

    showDialog() {
        this.refs.myDialog.show();
    }

So, what is our general take on this? Do we still write all methods without any underscore?

taion

taion commented on Oct 25, 2015

@taion
ContributorAuthor

👍 Facebook, for example, use this pattern extensively: https://github.com/facebook/relay/blob/v0.4.0/src/container/RelayRootContainer.js

hbrls

hbrls commented on Oct 26, 2015

@hbrls

@taion what about v0.14

taion

taion commented on Oct 26, 2015

@taion
ContributorAuthor

What about it?

ljharb

ljharb commented on Oct 26, 2015

@ljharb
Collaborator

Personally, I think this pattern is horrifically bad. Things that are not private are not private - sticking an underscore in front of them doesn't make them private, it just lulls you into the false belief that you can make a breaking change without bumping a major version.

It's not private if it's accessible in the runtime. If it's private - ie, via closure or WeakMap - no convention or unit tests are needed. If it's not, it's public, and you must test it, and whether you document it or not, it is public and breaking it counts as a breaking change. At that point, using a leading underscore is just fluff, so why would you need a convention for it?

taion

taion commented on Oct 26, 2015

@taion
ContributorAuthor

@ljharb

That's totally fine - I personally don't mind writing my code either way (as long as I can point to a style guide and say that I'm following it).

The issue is that this style guide appears to be inconsistent between https://github.com/airbnb/javascript#22.4 and https://github.com/airbnb/javascript/tree/master/react#methods. I don't see how it's possible to follow both.

ljharb

ljharb commented on Oct 26, 2015

@ljharb
Collaborator

React is a special subset - you could follow both by using _-prefixes in non-React code. I don't see a logical disconnect there.

taion

taion commented on Oct 26, 2015

@taion
ContributorAuthor

Okay. If that's intentional, I can live with it.

ljharb

ljharb commented on Apr 12, 2016

@ljharb
Collaborator

JS doesn't have privacy. Underscore-prefixed things are just as public as anything else.

goatslacker

goatslacker commented on Apr 12, 2016

@goatslacker
Collaborator

We should probably just remove this section from the main style guide or rephrase it. Same with the React style guide. This whole attachment to "private" is what makes the rules seem confusing.

46 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @goatslacker@ljharb@nicbou@jquense@danactive

        Issue actions

          Underscore prefixes for "private" properties · Issue #490 · airbnb/javascript