Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the standard naming convention for a property's backing field?

Background

I'm wanting to follow the most commonly-practised naming conventions for TypeScript. I've noticed that the official website shows code examples featuring Pascal-case for types and modules and camel-case for just about everything else.

Example

I'm currently implementing a property that encapsulates a backing value:

class SomeClass {
    get status() {
        return statusBackingField;
    }
    set status(newValue: Status) {
        statusBackingField = newValue;
        //Do other work here
    }

    statusBackingField: Status;
}

Problem

The name of the property is status. In C#, I would normally name the property Status and the backing value status. Since the convention is to use camel-case for properties, this doesn't work. I'm not sure which convention I should use for consistency with other TypeScript code in general.

Question

Other languages, such as C# and Java, seem to have official or de facto standard conventions. Is there any such authoritative or de facto standard convention for naming backing fields in TypeScript?

Notes

For the close-voters: please note that I'm not looking for opinions. I'm looking for objective information as requested in the summarised question above.

like image 594
Sam Avatar asked Jan 20 '14 21:01

Sam


People also ask

What are different naming conventions?

The standard naming conventions used in modern software development are as follows: Pascal case. camel case. snake case.

What is the convention for naming variables?

The choice of a variable name should be mnemonic — that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

What is the naming convention for constants?

The recommended naming and capitalization convention is to use PascalCasing for constants (Microsoft has a tool named StyleCop that documents all the preferred conventions and can check your source for compliance - though it is a little bit too anally retentive for many people's tastes).


1 Answers

There is no code convention standard for TypeScript. Since it is a superset of JavaScript, following JavaScript code conventions would probably be the correct approach. In such a case you would use an underscore-prefixed property _status. Idiomatically, this also matches the compiler’s use of an underscore-prefixed _this for compiled arrow functions and underscored-prefixed _super for superclasses.

like image 109
C Snover Avatar answered Sep 27 '22 20:09

C Snover