-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Style guide: Leading underscore conflict with TypeScript docs #26393
Comments
This has been discussed before #16893 (specially this one), angular/angular.io#1108, johnpapa/angular-styleguide#703. Really old issues though. |
Yeah, have read the threads and I do understand the rationale. Unfortunately, it's in conflict with other official documentation and the behavior of the tooling. Docs should at minimum suggest an alternative convention imo. |
Mentioned this to Google folks at Angular Mix this week and was advised to open an issue. |
May I work on this if no one has taken it? |
You should notice that you have a special case. An attribut and a getter can't have the same name. For the rest, get inspiration by the coding guidelines of the typescript project. https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
|
I'm not sure what you mean. The case shown is the one from the TypeScript docs:
If there is another pattern that is recommended, the style guide should include it. This pattern is the one autogenerated by the tooling in both Storm and VSC. |
@pburkindine Yes, and the attribut must not have the same name as the getter. The style guide says you should avoid it. If it is unavoidable, do what you want. |
Still not sure what you mean... The attribute does not have the same name as the getter in the example shown. The leading underscore is the way the tooling and TS documentation are solving the issue that the names cannot be the same, as far as I can tell? What I'm asking is that if this pattern is not to be used in Angular (this is avoid and not consider in the docs), an alternative pattern should be included. Our department guidelines and linting cannot abide the conflict in direction. We must encapsulate, and the way this is done in TypeScript by default is at odds with the Angular docs. Must enforce "the official answer" in the pipeline and can't at the moment. |
@pburkindine You should avoid prefixing every private attribute with an underscrore. In your case it's the best solution to the problem. IMO Avoid doesn't mean forbidden. |
That answer would be fine if it could be included in the docs, 'Avoid using leading underscore, except for backing variables of public properties...' From the linting perspective that's not an easy thing to enforce, though? |
As far as avoid vs forbid goes, folks should understand that this style guide is being treated as "official best practices from Google" by consumers, and as such nobody's home if I go around claiming I think an exception should be made in such and such cases. If Google says avoid, we have to avoid, period - but here we can't, at least without an alternative pattern. Since this is a very common use case, the style guide needs to address it imo. |
@IgorMinar : Please weigh in on this. Thanks! |
@pburkindine The TS handbook doesn't explicitly require prefixing with an underscore (even if in their example implementation they chose it for their workaround).
If linting (or avoiding rather than considering) are issues then would using _ as a suffix work as a compromise for get/set? This seems to align with some existing guidelines in the ecosystem - e.g. $ suffixes for ngxs selectors - however personally I'm happy with prefixing. IntelliJ allows editing of the templates used for autogenerating code, I'd expect that WebStorm supports template editing also. |
@mikesm I am fine with the idea of a suffix. My main concern is that some official recommended workaround be included in the docs, even if it's vague like
|
Adding a new rule to my tslint.config fixed to me:
I'm using PHPStorm and the "no-underscore" rule makes no sense to me. It's much easier and more efficient to use an leading underscore. Adding prefixes like "get" or "set" to getter/setters makes the code ugly. That's not Java, it's Typescript. Furthermore the angular guidelines describes only that underscores shouldn't be used but explain nothing about any alternative when using getter/setters. Renaming the getter/setters manually is not an option. |
@jenniferfell @IgorMinar any update here? |
Closed by #30334, all these conventions have been removed. |
@trotyl Thanks, I very much appreciate you all working on this |
@pburkindine why did you close this ? |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
The style guide states:
This is in conflict with how TypeScript documentation suggests we create getters and setters, and the behavior of both WebStorm and the prominent VSC extension when autogenerating getters and setters.
The pattern is like:
Ref:
https://www.typescriptlang.org/docs/handbook/classes.html#accessors
johnpapa/angular-styleguide#861
Expected behavior
Style guide should either provide a recommendation on preferred alternative or remove this guideline.
Preferred alternative should be lintable, by e.g. tslint-consistent-codestyle.naming-convention (should be a universal rule for private fields and methods).
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Our organization is put in friction because we are required to encapsulate, required to follow TS documentation, and required to follow the style guide.
Environment
The text was updated successfully, but these errors were encountered: