- Coding conventions are style guidelines for programming.
- Use the guidelines to improve readability and makes code maintenance easier. Coding convention has nothing to do with the performance.
- Use camelCase for identifier names (variables and functions).
- Use CamelCase with first letter as capital for class names
- All names start with a letter.
firstName = "John";
lastName = "Doe";
price = 19.90;
tax = 0.20;
fullPrice = price + (price * tax);
function getSum(num1, num2) {
return num1 + num2;
}- Always put spaces around operators ( = + - * / ), and after commas. This makes code much more readable.
var x = y + z;
var cars = ["Volvo", "Saab", "Fiat"];- Use 2 spaces for indentation of code blocks instead of tabs. If you are using use tslint for linting purposes. Use .editorconfig to configure the IDE or code editor that you are using.
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}- For simple statements, always end it with a semicolon. Semicolon is forced by typescript and in JS it is not forced, but for old browser like IE, it has some consequences of not having a semicolon at the end of the statement.
let values = ["Volvo", "Saab", "Fiat"];
let person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};- For compound statements,
- Put the opening bracket at the end of the first line.
- Use one space before the opening bracket.
- Put the closing bracket on a new line, without leading spaces.
- Do not end a complex statement with a semicolon.
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}
for (i = 0; i < 5; i++) {
x += i;
} -
General rules for object definitions:
- Place the opening bracket on the same line as the object name.
- Use colon plus one space between each property and its value.
- Use quotes around string values, not around numeric values.
- Do not add a comma after the last property-value pair.
- Place the closing bracket on a new line, without leading spaces.
- Always end an object definition with a semicolon.
let person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};- Short objects can be written compressed, on one line, using spaces only between properties:
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};- Your line of code should not be longer than 80 characters.
-
Always use the same naming convention for all your code. For example:
- Variable and function names written as camelCase
- Global variables written in UPPERCASE (We don't, but it's quite common)
- Constants (like PI) written in UPPERCASE
Use simple syntax for loading external scripts (the type attribute is not necessary):
<script src="myscript.js"></script>- Always use lower case letters to name your file.
- Filename extension ends with .js