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

[Enhancement] Proper ways of adding getters to classes #5

Closed
peterzhuamazon opened this issue Sep 25, 2024 · 0 comments · Fixed by #11
Closed

[Enhancement] Proper ways of adding getters to classes #5

peterzhuamazon opened this issue Sep 25, 2024 · 0 comments · Fixed by #11
Assignees
Labels
enhancement New feature or request

Comments

@peterzhuamazon
Copy link
Member

These are the getters setup today:

public getName(): string {
return this.name;
}
public getEvents(): string[] {
return this.events;
}
public getTasks(): Task[] {
return this.tasks;
}
}

A better way:

export class Operation {
  private _name: string;
  private _events: string[];
  private _tasks: Task[];

  constructor(name: string, events: string[], tasks: Task[]) {
    this._name = name;
    this._events = events;
    this._tasks = tasks;
  }

  public get name(): string {
    return this._name;
  }

  public get events(): string[] {
    return this._events;
  }

  public get tasks(): Task[] {
    return this._tasks;
  }
}

Even better we can just use readonly:

export class Operation {
  readonly name: string;
  readonly events: string[];
  readonly tasks: Task[];

  constructor(name: string, events: string[], tasks: Task[]) {
    this.name = name;
    this.events = events;
    this.tasks = tasks;
  }
}

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

2 participants