This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Naming Variables
Keonwoo Kim edited this page May 3, 2019
·
1 revision
The following are basic rules for naming variables:
- Use
camelCase
for a variable and a function,PascalCase
for a class, andUPPER_CASE
for a constant.
const plusOneMap = new Map([[1, 2], [2, 3]]);
const duplicateString = (x: string) => `${x} and ${x}`;
function triplicateString(x: string) {
return `${x}, ${x} and ${x}`;
}
class Rectangle {
static get type() {
return 'rectangle';
}
constructor(height: number, width: number) {
this.height = height;
this.width = width;
}
}
const API_KEY = 'SW4D5m7zTnc5byJsWco4JGERCa4bnkUkvFRfH6Yi';
- Use verb for the name of a function, and noun for the name of a variable.
async function getUserDataAsync(name: string) {
const response = await fetch(`https://api.github.com/users/${name}`);
const userData = await response.json();
return data;
}
- Long name is usually better than short name.
// Do
const addressListSortedByName = addressList.concat().sort((a, b) => a.localeCompare(b));
// Don't
const addrlsbynm = addrls.concat().sort((a, b) => a.localeCompare(b));