Naming Conventions
Consistent naming conventions are critical for maintaining readable, scalable, and maintainable code. This document outlines best practices for naming variables, functions, files, and other elements in a modern frontend project.
General Guidelines
- Be Descriptive: Use clear and meaningful names that describe the purpose of the element.
- Use CamelCase for JavaScript: Follow
camelCasefor variables, functions, and object properties. - Avoid Abbreviations: Write full, meaningful names instead of cryptic abbreviations.
- Stay Consistent: Use the same naming patterns across the entire codebase.
- Prefix Booleans: Start boolean variables with
is,has, orshould(e.g.,isVisible,hasAccess). - Avoid Reserved Words: Don’t use JavaScript reserved keywords as identifiers.
Naming Variables
Constants
- Use
UPPER_SNAKE_CASEfor constants:
Naming Functions
General Guidelines:
- Use
camelCase. - Start function names with verbs to indicate action.
function fetchUserData() {
// Fetch user data from API
}
function calculateTotalPrice(items) {
// Calculate the total price of items
}
Naming Files and Folders
-
Use kebab-case for file and folder names:
-
Group related files into folders:
-
Use index files for central exports:
Naming Components (React)
File Names
-
Use
PascalCasefor React component file names:
Component Names
-
Use
PascalCasefor component names:
Naming CSS Classes
Use BEM (Block-Element-Modifier) Convention
- Block: Represents the parent component.
- Element: Represents a child element of the block.
- Modifier: Represents a variant or state of the block/element.
Explanation:
button: Blockbutton__icon: Elementbutton--primary: Modifier
Naming State Variables (React)
Use Descriptive Names
Avoid Generic Names
API Endpoints
-
Use
snake_casefor API endpoint paths: -
Avoid mixing conventions:
Database and Backend Naming
-
Use
snake_casefor database tables and columns:
Key Takeaways
- Be consistent with naming conventions throughout the codebase.
- Prioritize readability and clarity.
- Use naming conventions that align with your project’s language and frameworks.