Code Patterns & Conventions
This document outlines the coding patterns, conventions, and best practices used in development at Innox Technologies.
Table of Contents
General Principles
TypeScript Conventions
React Patterns
Next.js Patterns
Server Actions
Component Patterns
Styling Conventions
Error Handling
File Naming
Import Conventions
Testing Patterns
Documentation
General Principles
1. Clarity Over Cleverness
// ❌ Bad - Clever but unclear
const u = users?.find(u => u.id === id)?.n ?? 'Unknown';
// ✅ Good - Clear and explicit
const user = users?.find(user => user.id === currentUserId);
const userName = user?.name ?? 'Unknown';2. Explicit Over Implicit
3. Early Returns
TypeScript Conventions
Type Definitions
File Location: src/types/index.ts
Pattern: Centralized type definitions
Interface vs Type:
Type Imports
Pattern: Use type keyword for type-only imports
Strict Typing
Avoid any
anyReact Patterns
Server Components (Default)
Pattern: Use Server Components by default
Client Components (When Needed)
Pattern: Only use Client Components for interactivity
When to use Client Components:
Event handlers (
onClick,onChange)React hooks (
useState,useEffect,useContext)Browser APIs (
window,localStorage,navigator)Third-party libraries that require hooks
Component Structure
Pattern: Consistent component structure
Props Destructuring
Pattern: Destructure props with defaults
Custom Hooks
Pattern: Extract reusable logic into custom hooks
Next.js Patterns
Server Components for Data Fetching
Pattern: Fetch data in Server Components, pass to Client Components
Parallel Data Fetching
Pattern: Fetch independent data in parallel
Dynamic Imports
Pattern: Lazy load heavy components
Server Actions
Standard Server Action Pattern
Pattern: Consistent structure for all Server Actions
ActionResult Pattern
Type: Always return ActionResult<T>
Revalidation
Pattern: Revalidate cache after mutations
Component Patterns
Compound Components
Pattern: Components that work together
Render Props
Pattern: Pass rendering logic as prop
Controlled vs Uncontrolled
Pattern: Prefer controlled components
Styling Conventions
Chakra UI v3 Patterns
Use Semantic Tokens:
Responsive Patterns:
Custom Styling
Pattern: Use Chakra props, avoid inline styles
Error Handling
Try-Catch Pattern
Pattern: Consistent error handling with logging
Error Boundaries
Pattern: Client-side error boundaries
File Naming
Conventions
Folder Structure
Import Conventions
Order
Pattern: Consistent import order
Absolute Imports
Pattern: Use @/ for project root
Type Imports
Pattern: Use type keyword for type-only imports
Testing Patterns
Test Structure
Pattern: AAA pattern (Arrange, Act, Assert)
Test File Naming
Documentation
JSDoc Comments
Pattern: Document exported functions and types
Component Documentation
Pattern: Document component purpose and props
Inline Comments
Pattern: Explain WHY, not WHAT
Best Practices Summary
Do's ✅
Use Server Components by default
Use TypeScript strictly
Use absolute imports with
@/Destructure props with defaults
Handle errors consistently with
ActionResultDocument exported functions and components
Use semantic tokens for styling
Fetch data in parallel when possible
Implement permission checks in Server Actions
Follow consistent naming conventions
Don'ts ❌
Don't use Client Components unnecessarily
Don't use
anytypeDon't use relative imports for deep paths
Don't ignore TypeScript errors
Don't skip error handling
Don't hardcode colors
Don't use client-side fetch for data
Don't skip permission checks
Don't commit commented code
Don't mix UI logic with business logic
For architecture details, see ARCHITECTURE.md. For troubleshooting, see TROUBLESHOOTING.md.
Last updated
