Naming Conventions
The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used.
Meaningful and understandable variables' name helps anyone to understand the reason for using it.
- Local variables should be named using camel case lettering starting with a small letter e.g. localData
- Global variables names should start with a capital letter e.g. GlobalData
- Constant names should be formed using capital letters only e.g. CONSDATA
- Variable names should be nouns.
- Use pronounceable variable names,if you can't pronounce it, you can't discuss it without sounding weird
- It is better to avoid the use of digits in variable names
- The name of the class/interface/Enums should be written in Pascal Case form
NumberOfDonuts = 34
FavePhrase = "Hello World"
- The names of the function, local variable should be written in camelCase starting with small letters
numberOfDonuts = 34
favePhrase = "Hello World"
- The name of the function must describe the reason of using the function clearly and briefly
- Use camelCase for class members, interface members, Enum members, method parameters
Coding Standards
- There should be one space between the right parenthesis and the left curly brace that begins the statement body for anonymous functions
- The right curly brace is aligned with the line containing the beginning of the declaration of the function
- A keyword followed by left parenthesis should be separated by a space
-
Each semicolon in the control part of a for statement should be followed with a space e.g.
(i=0; i<z; i+=1) - Whitespace should follow every comma.
- Strings should be wrapped in single quotes
- Separate logical parts of code with blank lines
- A do statement always end with a semicolon
- Each simple statement and assignment must end with a semicolon
- Consider placing JavaScript at the bottom
- There should be no space between the name of a function and the left parenthesis of its parameter list
- There should be no space between the name of a function and the left parenthesis of its parameter list e.g. function helloWorld() {}
- There should be one space between the right parenthesis and the left curly brace that begins the statement body for anonymous functions
- The right curly brace is aligned with the line containing the beginning of the declaration of the function
- The opening brace of a block is placed on the same line as its corresponding statement or declaration, do not omit curly brackets
- Always use const or let to declare variables
- Use const by default, unless a variable needs to be reassigned
- Don't use negative names for boolean variables
//Good
const isEnabled = false;
// Bad
const isNotEnabled = true;
- A prefix like is, are, or has helps every developer to distinguish a boolean from another variable by just looking at it.
- Do not use _ as a prefix for private properties.
- Limit the length of code to 500–600 lines in a file.
- Use try...catch to handle the error and exceptions, make sure there is no unhandled exception in your code.
Handling Errors
Always log the errors and relevant information.This helps in debugging the code in case of errors. The following log levels can be used to add logging in your code:
- Add TODOs for pending code blocks
- Reuse the code as much as possible and remove unnecessary code blocks
- Use undefined do not use null except where external libraries require it
- Don't use blank lines unnecessarily e.g. the first line of a function not should be a blank line
- Always explicitly define a return type for methods that are more than one line. This can help TypeScript validate that you are always returning something that matches the correct type
Loading Scripts
When loading a script, the browser cannot continue until the entire file has been loaded.
If we have JavaScript files in order to add functionality, we should place those files at the bottom, just before the closing body tag.
This is a good performance practice, and the results are quite noticeable.