Skip to content

Underscore prefixes for "private" properties #490

Closed

Description

@taion

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

  1. 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.

  2. 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.

  3. 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?

  4. 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

  5. hbrls commented on Oct 26, 2015

    @hbrls

    @taion what about v0.14

  6. taion commented on Oct 26, 2015

    @taion
    ContributorAuthor

    What about it?

  7. 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?

  8. 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.

  9. 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.

  10. taion commented on Oct 26, 2015

    @taion
    ContributorAuthor

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

  11. ljharb commented on Apr 12, 2016

    @ljharb
    Collaborator

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

  12. 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.

  13. taion commented on Apr 12, 2016

    @taion
    ContributorAuthor

    Either way would be fine – it's the inconsistency that's galling.

    Though I'd note again that FB do in fact use leading underscores, real privacy or no 😛

  14. 22 remaining items

  15. nicbou commented on May 6, 2016

    @nicbou

    Yes, and I am asking "how?", because there clearly isn't a way to do so with ES6 classes.

  16. ljharb commented on May 6, 2016

    @ljharb
    Collaborator

    This isn't the proper venue for it, but if you ping me in the gitter channel, I'll be happy to show you how it's easily possible for your example.

  17. nicbou commented on May 6, 2016

    @nicbou

    Sure thing! Let's talk about it Monday. The weekend is too short for arguments about code ;)

  18. gaurav21r commented on Aug 29, 2016

    @gaurav21r

    @ljharb Your points are very valid and I guess the Javascript Ecosystem has a history of "working around the language" read CoffeeScript, TypeScript, React & JSX etc.

    But you must agree the leading (and trailing) underscore is pretty much prevalent in the Industry.

    And there are some more heavyweights too apart from the above.

    Your point about working with the language is well taken. I personally feel though that this style guide could be better off taking a solid consistent stand throughout.

    Eg with https://github.com/airbnb/javascript#accessors--no-getters-setters It seems we are encouraging people to go "around" the language and use custom setters like setProperty which is code convention based and not language based (getters & setters).

    I may be wrong but this seems to be the same issue as the leading underscore/

  19. ljharb commented on Aug 29, 2016

    @ljharb
    Collaborator

    @gaurav21r I believe Polymer is from Google, so your three examples are really just two. Also, someone being a "heavyweight" is a poor reason to make any choice.

    This style guide does take a solid and consistent stand: do not use ES5 getters/setters; and do not use underscores at all (trailing or leading), nor anything else that attempts to convey soft "privacy". The reasons are hopefully communicated well. If they can be improved, PRs are most welcome!

    Just because a language provides functionality does not mean it is the best or most appropriate choice to achieve a goal, nor that using an alternative is "going around the language". You can do lots of things with eval or with, but that doesn't mean it's ever a good idea.

  20. MatthewCochrane commented on Jan 5, 2021

    @MatthewCochrane

    I didn't really understand all this fuss about truly private variables until I connected it with the concept of Hyrum's Law.

    With a sufficient number of users of an API,
    it does not matter what you promise in the contract:
    all observable behaviors of your system
    will be depended on by somebody.

    This is intuitive to me and makes clear sense. With that in mind and the scale of Airbnb it makes sense why this rule exists in their style guide and how 'fake private' is really just plain old 'public'.

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

      Issue actions