Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript : underscore convention for members [closed]

Tags:

I have a class Email

class Email {   private _from: string;   private _to: Array<string>;   private _subject: string; } 

It'll create an email object something like:

{   _from:'',   _to:'',   _subject:'' } 

This seems a little weird to me since I cannot directly use this object to send to a function . Instead I'll have to transform the object so that it doesn't have underscores . So how do I use the underscore convention or do I have to transform the object .

EDIT : If I do drop the '_'

How do I name the getters and setters if we name the private variables without underscore? A VSCode plugin called Typescript toolbox creates them something like this

public get $subject(): string {    return this.subject; } 

Is $ a good convention ?

like image 773
Jeson Dias Avatar asked Dec 11 '17 06:12

Jeson Dias


People also ask

What does _ do in TypeScript?

If you use _ , it explicitly states that the function will be passed one argument, but that you don't care about it. The function's . length will be 1, which might matter in some frameworks.

Should private variables have underscore?

In C++, an underscore usually indicates a private member variable. In C#, I usually see it used only when defining the underlying private member variable for a public property. Other private member variables would not have an underscore.

What is underscore variable in TypeScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

Why put an underscore in front of a variable?

The underscore prefix is meant as a hint to another programmer that a variable or method starting with a single underscore is intended for internal use. This convention is defined in PEP 8. This isn't enforced by Python. Python does not have strong distinctions between “private” and “public” variables like Java does.


1 Answers

Those who say, must not use the "_", for them, here is some code from TypeScript site:

class Employee {     private _fullName: string;      get fullName(): string {         return this._fullName;     }      this._fullName = ...... } 

Same question on Stackoverflow, you should have a look on it, especially the answer.

For the time being, if accepted, we should not use _, then what are other better ways?

Let's take your example of email, if we will not use _ then, we will come something like this:

member: to,      get/set: emailTo member: from     get/set: emailFrom  

might be you can think some better name, but every time you need to think, which is not very common in the developer world!

Using _ and the same name for a property is easy to check the code otherwise we will be keep mapping which property to which member.

BUT: If you have been forced by your lead at your company then you can use $ for member and property without it; not a rule but easy way:

class Employee {     private fullName$: string;      get fullName(): string {         return this.fullName$;     }      this.fullName$ = ...... } 

The Choice Is Yours!!!

like image 50
Ali Adravi Avatar answered Oct 07 '22 18:10

Ali Adravi