Coding Standards
Naming Conventions
Variables
Use descriptive, meaningful variable names in camelCase. Avoid single-letter or ambiguous names.
// Good
let userName = 'John Doe';
let invoiceTotal = 1500;
// Bad
let u = 'John Doe';
let x = 1500;
Functions
Function names should be in camelCase and clearly describe their purpose. Use verbs for actions.
// Good
function getUserName() {
return 'John Doe';
}
function calculateInvoiceTotal(items) {
// ...
}
// Bad
function getuser() {
return 'John Doe';
}
function calc() {
// ...
}
Classes
Class names should use PascalCase and be singular, representing a single entity or concept.
// Good
class UserManager {
// class content
}
class Invoice {
// ...
}
// Bad
class user_manager {
// class content
}
class invoices {
// ...
}
Constants
Use UPPER_CASE for constants.
// Good
const MAX_USER_COUNT = 100;
// Bad
const maxusercount = 100;
Rationale
Clear, consistent naming improves readability, maintainability, and reduces bugs. Use camelCase for variables/functions, PascalCase for classes, and UPPER_CASE for constants to distinguish their roles.
Edge Cases & Advanced Examples
// Good: Descriptive destructuring
const { userName, invoiceTotal } = userData;
// Bad: Ambiguous destructuring
const { u, x } = userData;
// Good: Arrow function naming
const calculateTotal = (items) => items.reduce((sum, item) => sum + item.price, 0);
// Bad: Unclear arrow function
const calc = (x) => x.reduce((a, b) => a + b, 0);
File & Folder Naming
- Use kebab-case for file names:
user-manager.js,invoice-list.js - Use plural for folders containing multiple items:
components/,utils/ - Avoid spaces and special characters in names.
Formatting
Indentation
Use 2 spaces per indentation level. Do not use tabs. Indent consistently for readability.
Line Length
Keep lines under 80 characters. Break long statements into multiple lines.
Blank Lines
Use blank lines to separate logical sections of code, such as between functions or class methods.
Comments
Use comments to explain non-obvious code. Write comments above the code they describe, not inline. Avoid redundant comments.
// Good
// This function returns the user's name
function getUserName() {
return 'John Doe';
}
// Explain complex logic
// Calculate the total invoice amount including tax
function calculateInvoiceTotal(items) {
// ...
}
// Bad
function getUserName() {
return 'John Doe'; // returns name
}
Rationale
Consistent formatting makes code easier to read, review, and maintain. It also helps avoid merge conflicts and bugs due to misaligned code.
Advanced Formatting Examples
// Good: Break long statements
const longString =
'This is a long string that is split across multiple lines to keep each line under 80 characters.';
// Good: Blank lines between logical sections
function firstFunction() {
// ...
}
function secondFunction() {
// ...
}
Comments
Rationale
Comments should explain why code exists, not what it does. Avoid redundant comments and keep them up-to-date.
Advanced Comment Examples
// Good: Explain complex logic
// Calculate the invoice total, including discounts and taxes
function calculateInvoiceTotal(items, discount, taxRate) {
// ...
}
// Bad: Redundant or outdated comments
function calculateInvoiceTotal(items) {
// Calculates total
// ...
}
Comprehensive Example: Class & Module
// user-manager.js
/**
* Manages user operations such as registration and authentication.
*/
export class UserManager {
/**
* Registers a new user.
* @param {Object} data - User data
* @returns {User}
*/
register(data) {
// ... registration logic ...
}
/**
* Authenticates a user.
* @param {string} email
* @param {string} password
* @returns {boolean}
*/
authenticate(email, password) {
// ... authentication logic ...
}
}
Additional Best Practices
- Use ES6+ syntax and features for clarity and maintainability.
- Group related functions and classes together in files and folders.
- Avoid magic numbers and strings; use named constants instead.
- Refactor duplicated code into reusable functions or modules.
- Write self-documenting code: code should be clear enough that comments are only needed for complex logic.
- Use module imports/exports for code organization.
- Prefer arrow functions for callbacks and concise logic.
- Use destructuring for objects/arrays to improve clarity.
- Document public interfaces and expected inputs/outputs.
- Use linters (ESLint) and formatters (Prettier) to enforce standards.