-
Notifications
You must be signed in to change notification settings - Fork 4
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
Declarative syntax as the received way to access private state is highly valuable #3
Comments
In Node.js core we have a guideline on using symbols to hide private states (they are not private but there are no better alternatives), because we have caught numerous usage of underscore-prefixed properties/methods in code reviews and have to request changes to use symbol properties instead (Node.js has many existing underscore properties that can't be removed easily out of compatibility concerns so we can't enforce the style with a linter). A declarative syntax would send a clearer signal to developers that this is something they should use to access private state, instead of going with underscores (or nothing at all) to save a few more keystrokes. A project like Node.js may have enough reviewers to catch that kind of usage, but projects with fewer eyeballs have a higher risk of leakier code just because symbols are less ergonomic. Even with the amount of reviewers we have, not everyone is aware of this style as sometimes new collaborators or collaborators who have been absent for a while come and approve patches that leak private state until someone request changes to use symbols. Private symbols provide the means to hide private state, but it does not provide as enough incentive as a declarative syntax would. |
Hi @joyeecheung, Platform and framework authors are definitely the target audience for this feature, so it's important that it works well for the Node.js internals use case. I'm curious about this statement:
Yes, I've used underscore properties from "module" many times. 😄 Would it be possible to annotate the source code with comments, indicating which underscore properties are OK from the linter's point of view? |
I just did a quick search for |
Thanks @joyeecheung. I can see how linting would be a bit of a headache for that scenario. |
A couple of notes on syntax: First, syntax can be layered on top of We would likely want to look at whether there are some syntax tweaks that would better align with private symbols though. We've seen proposals for Also, I think there's a valid argument that says that I know that Node doesn't use TypeScript currently, but there's another potential solution there: possibly we can add a option that instructs the compiler to generate private symbols for In short, I'm not too worried about syntax. |
Certainly. What I intended for this issue was that value is lost if syntax becomes future work instead of part of the core proposal.
I don't have the intuition that hard privacy is an advanced feature. This proposal is an advanced feature, but I'd like hard privacy for the common case to not be an advanced feature, and I feel like the current class fields proposal does provide that better than private symbols do. |
@syg and what do you think about syntax sugar on top of private-symbols described here: tc39/proposal-class-fields#134 ? |
@Igmat I'd like to to avoid debating the particulars of syntax and alternatives here, but for the record I do like the current |
@syg and why do you prefer security over metaprogramming, excluding the fact that existing approach already in |
@Igmat I don't understand the framing of that question. |
tc39/proposal-class-fields#106 (comment) So, since the only reasonable scenario for |
Security is not the only reason to perform brand checks, but I don’t think it’s a significant factor since, as you said, it can be achieved regardless of how private |
@bathos just for curiosity, what other scenarios for |
Polyfilling APIs defined by Web IDL is probably the most common, but also just in ordinary library code that wishes to establish very strict invariants. Just as one might be aggressive about runtime type checking of ordinary arguments; |
I see the declarative syntax of class fields and that class field accesses use
.
instead of computed property accesses as pros of the class fields proposal.Declarative syntax and static property accesses are easier to reason about both for the human and the compiler. And code that deals with private state is more often than not the code that I'd like to be most sure of as having its invariants intact.
The text was updated successfully, but these errors were encountered: