- Introduced a comprehensive set of general coding guidelines to enhance code readability and maintainability. - Established a localization rule to prevent hard-coded user-facing text, ensuring all strings are managed through the localization system. - Defined a naming convention for React components, mandating the use of PascalCase for consistency across the codebase. - Included examples and enforcement strategies for each rule to facilitate adherence during development and code reviews.
3.9 KiB
3.9 KiB
General Coding Guidelines
Rule Summary
Follow these rules when you write code:
-
Use Early Returns
- Prefer early returns and guard clauses to reduce nesting and improve readability, especially for error handling.
-
Tailwind for Styling
- Always use Tailwind CSS utility classes for styling HTML elements.
- Avoid writing custom CSS or using inline
styletags.
-
Class Tag Syntax
- Use
class:directive (e.g.,class:active={isActive}) instead of the ternary operator in class tags whenever possible.
- Use
-
Descriptive Naming
- Use clear, descriptive names for variables, functions, and constants.
- Use auxiliary verbs for booleans and state (e.g.,
isLoaded,hasError,shouldRender). - Event handler functions should be prefixed with
handle, e.g.,handleClickforonClick,handleKeyDownforonKeyDown.
-
Naming Conventions
- Directories: Use lowercase with dashes (e.g.,
components/auth-wizard). - Variables & Functions: Use
camelCase(e.g.,userList,fetchData). - Types & Interfaces: Use
PascalCase(e.g.,User,ButtonProps). - Exports: Favor named exports for components.
- No Unused Variables: Remove unused variables and imports.
- Directories: Use lowercase with dashes (e.g.,
-
File Layout
- Order: exported component → subcomponents → hooks/helpers → static content.
-
Props & Types
- Define props with TypeScript
interfaceortype, notprop-types. - Example:
interface ButtonProps { label: string; onClick?: () => void; } export function Button({ label, onClick }: ButtonProps) { return <button onClick={onClick}>{label}</button>; }
- Define props with TypeScript
-
Component Declaration
- Use the
functionkeyword for components, not arrow functions.
- Use the
-
Hooks Usage
- Call hooks (e.g.,
useState,useEffect) only at the top level of components. - Extract reusable logic into custom hooks (e.g.,
useAuth,useFormValidation).
- Call hooks (e.g.,
-
Memoization & Performance
- Use
React.memo,useCallback, anduseMemowhere appropriate. - Avoid inline functions in JSX—pull handlers out or wrap in
useCallback.
- Use
-
Composition
- Favor composition (render props,
children) over inheritance.
- Favor composition (render props,
-
Code Splitting
- Use
React.lazy+Suspensefor code splitting.
- Use
-
Refs
- Use refs only for direct DOM access.
-
Forms
- Prefer controlled components for forms.
-
Error Boundaries
- Implement an error boundary component for catching render errors.
-
Effect Cleanup
- Clean up effects in
useEffectto prevent memory leaks.
- Clean up effects in
-
Accessibility
- Apply appropriate ARIA attributes to interactive elements.
- For example, an
<a>tag should havetabindex="0",aria-label,onClick, andonKeyDownattributes as appropriate.
Examples
✅ Correct
// File: components/user-profile.tsx
interface UserProfileProps {
user: User;
isLoaded: boolean;
hasError: boolean;
}
export function UserProfile({ user, isLoaded, hasError }: UserProfileProps) {
if (!isLoaded) return <div>Loading...</div>;
if (hasError) return <div role="alert">Error loading user.</div>;
const handleClick = useCallback(() => {
// ...
}, [user]);
return (
<button
className="bg-blue-500 text-white"
aria-label="View user profile"
tabIndex={0}
onClick={handleClick}
onKeyDown={handleKeyDown}
>
{user.name}
</button>
);
}
❌ Incorrect
// File: components/UserProfile.jsx
function userprofile(props) {
if (props.isLoaded) {
// ...
}
}
return (
<button style={{ color: 'white' }} onClick={() => doSomething()}>
View
</button>
);
Enforcement
- All new code must follow these guidelines.
- Code reviews should reject code that does not comply with these rules.
- Refactor existing code to follow these guidelines when making changes.