Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript private setter public getter convention [closed]

So I want to have an immutable Vector class. For this I need to have a public getter for the x and y coordinates and a private setter so that I can actually initialize those values in the constructor.

I have a few options at my disposal so I'm wondering which one is per convention.

I could do it like this:

class Vector {
    constructor(private _x: number, private _y: number) { }
    public get x() {
        return this._x;
    }

    public get y() {
        return this._y;
    }
}

But I don't know if using an underscore is a common thing to do. This might be an issue since that name will be visible in the intellisense.

The second option could be

class Vector {
    constructor(private x: number, private y: number) { }
    public get X() {
        return this.x;
    }

    public get Y() {
        return this.y;
    }
}

As far as I know, only classes start with capitals in JS so this might also be a bad idea.

What is the preferred way to handle this?

like image 213
Luka Horvat Avatar asked Feb 10 '14 16:02

Luka Horvat


People also ask

Can we use getter and setter in TypeScript?

In TypeScript, there are two supported methods getter and setter to access and set the class members. In this very short article, I'm going to show you Typescript Accessor which includes getters/setters method. Actually, getters and setters are nothing but a way for you to provide access to the properties of an object.

Should getters and setters be public or private?

Usually you want setters/getters to be public, because that's what they are for: giving access to data, you don't want to give others direct access to because you don't want them to mess with your implementation dependent details - that's what encapsulation is about.

What is protected in TypeScript?

protected implies that the method or property is accessible only internally within the class or any class that extends it but not externally. Finally, readonly will cause the TypeScript compiler to throw an error if the value of the property is changed after its initial assignment in the class constructor.

Are setter methods private?

The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.


1 Answers

The general consensus so far has been to use the underscore. Private variables are not shown in auto-completion outside of the class - so you wouldn't see _x or _y in auto-completion on an instance of Vector. The only place you will see them is when calling the constructor and if that really offends, you can avoid auto-mapping (although I'd rather have the auto-mapping).

class Vector {
    private _x: number;
    private _y: number;

    constructor(x: number, y: number) { 
        this._x = x;
        this._y = y;
    }

    public get x() {
        return this._x;
    }

    public get y() {
        return this._y;
    }
}

var vector = new Vector(1, 2);

There is no official standard on this yet - but it is best to follow JavaScript style naming conventions if there is any chance whatsoever of interacting with external code as it will avoid switching conventions back and forth depending on whether you are calling a "Function We Made" or a "Function They Made".

Avoiding differences only in case is also a recommendation in Code Complete by Steve McConnell - which is another bad point against x and X along with the naming convention point you mentioned in the question.

like image 76
Fenton Avatar answered Oct 11 '22 00:10

Fenton