Skip to content
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

TypeScript - implicit or explicit access modifiers? #42

Open
DanielSchaffer opened this issue Apr 24, 2019 · 1 comment
Open

TypeScript - implicit or explicit access modifiers? #42

DanielSchaffer opened this issue Apr 24, 2019 · 1 comment

Comments

@DanielSchaffer
Copy link

Using implicit public access modifiers seems to be the de-facto standard for Angular - members are assumed to be public unless otherwise specified:

@Component({ ... })
export class SomeComponent {

  // public constructor
  constructor() {
    ...
  }

  // public method
  ngOnInit(): void {
    ...
  }

  private helperMethod(): string {
    ...
  }

}

However, the tslint "recommended" settings require explicit member access for everything (except constructors, I think?)

@Component({ ... })
export class SomeComponent {

  // public constructor
  constructor() {
    ...
  }

  public ngOnInit(): void {
    ...
  }

  private helperMethod(): string {
    ...
  }

}

The rationale for tslint's setting is:

Explicit visibility declarations can make code more readable and accessible for those new to TS.

Other languages such as C# default to private, unlike TypeScript’s default of public. Members lacking a visibility declaration may be an indication of an accidental leak of class internals.

Is the above a risk for SE developers? What are our preferences?

@shanejann
Copy link

I lean towards always showing the public keyword (again, with the exception of the constructor). My argument here is that other languages have different implicit visibilities. My one example of this is Java, where the methods are considered package-visible (I'll admit my Java is rusty, so I don't know how true this is).

In scenarios like this, any dev knows exactly how visible a function or member is. Sure, it's tedious to write out the whole word, but if you ask me, it's always better to be explicit about stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants