angular: Style guide: Leading underscore conflict with TypeScript docs

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

The style guide states:

Avoid prefixing private properties and methods with an underscore.
https://angular.io/guide/styleguide#style-03-04

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:

export class Foo {
  private _bar: string;

  ...

  public get bar(): string { return this._bar; }

Ref:

https://www.typescriptlang.org/docs/handbook/classes.html#accessors

https://github.com/johnpapa/angular-styleguide/issues/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


Angular version: N/A


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 20 (4 by maintainers)

Most upvoted comments

Adding a new rule to my tslint.config fixed to me:

"variable-name": [
  true,
  "allow-leading-underscore"
]

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. https://angular.io/guide/styleguide#style-03-04

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

Closed by https://github.com/angular/angular/pull/30334, all these conventions have been removed.

@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

<b>Consider</b> using a suffix on variables exposed by accessors/mutators

Mentioned this to Google folks at Angular Mix this week and was advised to open an issue.

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.

@pburkindine why did you close this ?

@IgorMinar : Please weigh in on this. Thanks!

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.

I’m not sure what you mean. The case shown is the one from the TypeScript docs:

class Employee {
    private _fullName: string;

    get fullName(): string {
        return this._fullName;
}

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.