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

Coding Guidelines: Accessors namingn convention #11227

Closed
helmutgranda opened this issue Sep 28, 2016 · 4 comments
Closed

Coding Guidelines: Accessors namingn convention #11227

helmutgranda opened this issue Sep 28, 2016 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@helmutgranda
Copy link

TypeScript Version: 2.0.3

Code

let passcode = "secret passcode";

class Employee {
    private _fullName: string;

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

    set fullName(newName: string) {
        if (passcode && passcode == "secret passcode") {
            this._fullName = newName;
        }
        else {
            console.log("Error: Unauthorized update of employee!");
        }
    }
}

let employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
    console.log(employee.fullName);
}

Expected behavior:
Based on the Names Coding Guidelines #6 it states:
Do not use "_" as a prefix for private properties.
However the sample code uses the underscore for provide properties:
source: https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Classes.md#accessors

Actual behavior:
A different demonstration for private properties specially around accessors.

@RyanCavanaugh
Copy link
Member

I'm not sure what you're asking for here. The coding guidelines are for our own codebase and are not meant to be the only correct way to write TypeScript code; other codebases do commonly use _ as a private prefix and it's useful in the example because the backing field and the name itself otherwise have the same spelling so it's more clear what the relation between them is.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Sep 28, 2016
@helmutgranda
Copy link
Author

Hey @RyanCavanaugh,

Thanks for the response and I do agree and see the benefit of using the _ for a private prefix. For one I thought the coding guidelines where an overall recommendation and thanks to your correction I can see I was assuming wrongly.

Thanks again for the clarification.

@CSchulz
Copy link

CSchulz commented Oct 20, 2016

@RyanCavanaugh Would it be possible to get a common coding guideline?

@RyanCavanaugh
Copy link
Member

@CSchulz see #878

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants