In order to improve the clarity, quality and development time it is worth considering the following principles whenever possible:
- DRY (Don't Repeat Yourself)
- KISS (Keep It Simple, Stupid)
- SoC (Separation of Concerns)
- Single responsibility principle
- Law of Demeter
Variables: Variables should be lowercase words separated by _.
const field_name = '...';Functions: Functions should be camelCase. This is to easily distinguish between variables and functions.
const myFunction = () => { ... };Capitalize "magic" strings and numbers:
const WARNING_MSG = 'This is a warning message';
const TRADE_TYPES = [{
code: 'NOTOUCH',
name: 'NoTouch',
}];Modules: Module names and classes should be PascalCase.
const MyModule = (() => { ... })();
jQuery variables: jQuery variables should have a $ in the beginning to mark them.
const $test = $('#test');
JavaScript elements: JavaScript elements start with el_ for a similar effect.
const el_test = document.getElementById('test');
Boolean: Those variables which store a boolean value, should start with is_, has_, ...
const is_updated = true;
const has_crypto = false;Form elements: Consider prefixes for form elements to make it more obvious what type of field they are, such as:
const fields = {
txt_name : { id: '#txt_name' },
chk_tnc : { id: '#chk_tnc' },
ddl_agents: { id: '#ddl_agents' },
};- Always try to explain yourself in code first, comments should be seen as a "necessary evil".
- Don't add obvious noise.
- Don't comment out code. Just remove.
- Use as explanation of intent.
- Use as clarification of code.
- Use as warning of consequences.
Explanations: Feel free to add comments to explain any code that is confusing.
To do: Use TODO: ... comments anywhere that needs consideration or attention in the future.
Alphabetical ordering: The order is important; it should be sorted alphabetically according to path:
momentcomes first as it's not a relative path.sis beforeuso./storagecomes before./utility.- Both
applyToAllElementsandcreateElementare from the same file, butais beforec - Unassigned
requiregoes to the end
Combining require: When there are many functions being imported from the same file, consider combining it into one import line.
const Utility = require('./utility');
...
Utility.handleHash();
Utility.createElement('div');
...