Naming design tokens, components, and variables is crucial for maintaining a clean, understandable, scalable codebase. Here are some best practices:
Design Tokens
Design tokens are named entities that store design decisions, such as colors, typography, spacing, etc. They bridge the gap between design and code.
- Consistency: Use a consistent naming convention throughout your tokens.
- Example: color-primary, font-size-small
- Descriptive and Clear: Names should describe the purpose or usage of the token.
- Example: color-background-primary, spacing-small
- Hierarchy: Reflect a clear hierarchy, especially for tokens used in multiple contexts.
- Example: button-color-primary, button-color-secondary
- Avoid hard coding: Do not tie names to specific values to allow flexibility.
- Example: color-primary instead of color-blue
- CamelCase or kebab-case: Stick to a casing style that fits your team’s conventions.
- Example: font size large (CamelCase) or font-size-large (kebab-case)
Components
Components are reusable building blocks of your application. Naming should reflect their purpose and context.
- Meaningful Names: Names should indicate the component’s function and scope.
- Example: UserCard, NavigationMenu
- PascalCase: Use PascalCase for component names.
- Example: ButtonPrimary, UserProfile
- Modular and Scoped: If components are nested or scoped, reflect this in the name.
- Example: HeaderLogo, FooterLinks
- Avoid Abbreviations: Full names are better than cryptic abbreviations for clarity.
- Example: SidebarMenu instead of SbMn
- BEM Methodology: For complex components with states or variations, consider BEM (Block Element Modifier).
- Example: Button–primary, Card__title
Variables
Variables hold data or references used in your code. Their names should be intuitive and descriptive of their content or purpose.
- Descriptive and Specific: Variable names should describe the content they hold or the role they play.
- Example: userAge, isAuthenticated
- LowerCamelCase: Use lowerCamelCase for variable names.
- Example: firstName, totalPrice
- Avoid Magic Numbers/Strings: Use variables instead of hard-coding values.
- Example: const maxRetries = 5; instead of const retry = 5;
- Contextual Naming: The name should make sense within the context of its use.
- Example: isUserLoggedIn in authentication context, buttonDisabled in UI context
- Short but Clear: While brevity is good, clarity should not be sacrificed.
- Example: totalAmount instead of ta
General Tips
- Consistency: Regardless of your naming, consistency across your project is key. Agree on a naming convention with your team and stick to it.
- Documentation: Maintain a naming convention guide in your project documentation.
- Review: Regularly review names for clarity and consistency during code reviews.
- Refactoring: Don’t hesitate to refactor names if they become misleading as the project evolves.