Only comment things that have business logic complexity.
Comments are an apology, not a requirement. Good code mostly documents itself.
Bad:
javascript
function hashIt(data) { // The hash let hash = 0; // Length of string const length = data.length; // Loop through every character in data for (let i = 0; i < length; i++) { // Get character code. const char = data.charCodeAt(i); // Make the hash hash = (hash << 5) - hash + char; // Convert to 32-bit integer hash &= hash; }}
Good:
javascript
function hashIt(data) { let hash = 0; const length = data.length; for (let i = 0; i < length; i++) { const char = data.charCodeAt(i); hash = (hash << 5) - hash + char; // Convert to 32-bit integer hash &= hash; }}
They usually just add noise. Let the functions and variable names along with the proper indentation and formatting give the visual structure to your code.
Comments
Only comment things that have business logic complexity.
Comments are an apology, not a requirement. Good code mostly documents itself.
Bad:
Good:
Don't leave commented out code in your codebase
Version control exists for a reason. Leave old code in your history.
Bad:
Good:
Don't have journal comments
Remember, use version control! There's no need for dead code, commented code, and especially journal comments. Use
git logto get history!Bad:
Good:
Avoid positional markers
They usually just add noise. Let the functions and variable names along with the proper indentation and formatting give the visual structure to your code.
Bad:
Good: