Naming conventions
A naming convention is a shared rule for naming variables, functions, files, classes, and other parts of a project. Clear names help you understand code without guessing.
What you will learn
- How to choose names that explain purpose
- How common casing styles differ
- Why consistency matters more than one universally correct style
Choose names that explain purpose
const userName = 'Yugien';
function getUserProfile(userId) {
// fetch and return the profile for userId
}userName and getUserProfile tell a reader what the value and function are for. Avoid names such as x, data2, or doThing when a more precise name is easy.
Common casing styles
- camelCase
- Starts lowercase and capitalizes later words. Common for JavaScript variables and functions:
userName. - PascalCase
- Capitalizes each word. Often used for classes or components:
UserProfile. - snake_case
- Uses underscores. Common in Python and some data formats:
user_name. - UPPER_SNAKE_CASE
- Often used for constants:
DEFAULT_TIMEOUT. - kebab-case
- Uses hyphens. Common in URLs, CSS, and data attributes:
user-profile.
Rules worth agreeing on
- Use the language and framework's normal style unless the project has a stronger rule.
- Use the same word for the same concept; do not alternate between
user,member, andaccountwithout a reason. - Prefer names that remain accurate when the implementation changes.
- Do not encode a type in every name just because an older convention did.
- Choose readable names for HTML IDs, CSS classes, URLs, and API fields too.