1---2name: komluk-scaffolding-pattern-recognition3description: Pattern Recognition Skill4---56# Pattern Recognition Skill78Standards for identifying and applying existing codebase patterns to maintain consistency.910## When to Apply1112- Before writing new code13- When implementing similar features14- Code review for pattern consistency15- Refactoring decisions1617---1819## Pattern Detection Process2021### Step 1: Scan Existing Code22| Action | Purpose |23|--------|---------|24| Find similar modules/components | Match structure and naming |25| Find similar functions/hooks | Match return types and patterns |26| Find similar services | Match error handling and API patterns |27| Check shared-types location | Match how the project centralizes types |2829### Step 2: Extract Patterns30| Element | What to Look For |31|---------|------------------|32| Module/component structure | Imports, signature, body order |33| State management | Local-vs-shared state decisions |34| Error handling | Try/catch style, error messages |35| Naming conventions | Files, functions, types |36| File organization | Directory structure |3738### Step 3: Apply Consistently39| Rule | Description |40|------|-------------|41| Match existing style | New code follows established patterns |42| Document deviations | If pattern changes, document why |43| Refactor if needed | Update old code to match new pattern |4445---4647## Example: React + TypeScript conventions (illustrative)4849> Illustrative — the naming, component, hook, service, and type conventions below50> are one team's React/TypeScript catalog shown as a concrete example. The51> reusable skill is the *process* above (scan → extract → apply existing52> conventions). Substitute your stack's actual conventions; the value of this53> skill is matching whatever your codebase already does, not adopting these54> specific rules.5556## Naming Conventions5758### File Naming59| Type | Convention | Example |60|------|------------|---------|61| Component | PascalCase | `AnnotationCard.tsx` |62| Hook | camelCase with use | `useVisualization.ts` |63| Service | camelCase | `apiService.ts` |64| Types | camelCase or index | `types/index.ts` |65| Store | camelCase with Store | `projectStore.ts` |66| Utility | camelCase | `formatters.ts` |6768### Code Naming69| Type | Convention | Example |70|------|------------|---------|71| Component | PascalCase | `AnnotationCard` |72| Function | camelCase verb | `fetchProjects`, `handleClick` |73| Hook | camelCase with use | `useVisualization` |74| Type/Interface | PascalCase | `ProjectType`, `ButtonProps` |75| Constant | UPPER_SNAKE | `API_BASE_URL`, `MAX_SIZE` |76| Variable | camelCase | `isLoading`, `userName` |7778---7980## Component Patterns8182### Standard Component Structure83| Section | Order | Required |84|---------|-------|----------|85| Imports | 1st | Yes |86| Props interface | 2nd | Yes |87| Component function | 3rd | Yes |88| Hooks declarations | Inside, top | Yes |89| Event handlers | Inside, after hooks | As needed |90| Return JSX | Inside, last | Yes |9192### Component Organization by Type93| Type | Location | Purpose |94|------|----------|---------|95| Page components | `pages/` | Route entry points |96| Feature components | `components/[feature]/` | Feature-specific UI |97| Common components | `components/common/` | Reusable across features |98| Layout components | `components/layout/` | Page structure |99100---101102## Hook Patterns103104### Custom Hook Standards105| Element | Requirement |106|---------|-------------|107| Name | `use` prefix + descriptive name |108| Return | Object with named values |109| State | `data`, `loading`, `error` pattern |110| Dependencies | All external values in dependency array |111112### Hook Return Pattern113| Return Type | Use Case |114|-------------|----------|115| `{ data, loading, error }` | Data fetching hooks |116| `{ value, setValue, reset }` | Form/input hooks |117| `{ isOpen, open, close, toggle }` | Toggle hooks |118119---120121## Service Patterns122123### API Service Standards124| Element | Requirement |125|---------|-------------|126| Async/await | All API calls use async/await |127| Error handling | Try/catch with console.error |128| Error format | `[ServiceName] Error description:` |129| Return type | Promise with typed response |130131### Error Handling Pattern132| Element | Standard |133|---------|----------|134| Log format | `console.error('[Context] Message:', error)` |135| User message | Generic, no technical details |136| Rethrow | After logging for upstream handling |137138---139140## Type Patterns141142### Type Location Rules143| Rule | Description |144|------|-------------|145| Centralized | All shared types in `types/index.ts` |146| Import style | Use `import type` for type-only imports |147| Export style | Use `export type` for type exports |148| No interfaces | Prefer `type` over `interface` for consistency |149150### Type Naming151| Category | Pattern | Example |152|----------|---------|---------|153| Entity | `[Entity]Type` | `ProjectType`, `UserType` |154| Props | `[Component]Props` | `ButtonProps`, `CardProps` |155| State | `[Domain]State` | `ProjectState`, `UIState` |156| API Response | `[Endpoint]Response` | `ProjectsResponse` |157158---159160## Anti-Patterns161162| Anti-Pattern | Problem | Solution |163|--------------|---------|----------|164| Props drilling | Hard to maintain | Use a shared store or context |165| Large components | Hard to test/read | Extract sub-components |166| Inline styles | Inconsistent | Use your styling system's tokens |167| `any` type | Loses type safety | Define proper types |168| Barrel exports | Circular dependencies | Use direct imports |169| Mixed conventions | Confusing | Follow established patterns |170171---172173## Code Reuse Protocol174175The universal rule: **before writing ANY new utility, grep the codebase for an176existing one.** Map your project's shared modules and reuse them instead of177creating parallel helpers, exception hierarchies, or clients.178179### Example: a backend `core/` layout (illustrative)180181> Illustrative — one team's shared-module layout. Substitute your project's182> actual shared modules. The reusable rule is "search before you create".183184| Module | Contains | Example |185|--------|----------|---------|186| `core/utils/` | datetime, validation, paths, formatters, file, language | `utc_now()`, `validate_uuid()`, `ensure_dir()` |187| `core/exceptions.py` | Base exceptions: AppError, NotFoundError, CreationError, GitHubError | Inherit, don't create parallel hierarchies |188| `core/http_client.py` | Singleton async HTTP client with connection pooling | `get_http_client()` for all HTTP calls |189| `core/config.py` | App configuration and settings | Centralized env var access |190| `*/service.py` | Domain service layer | Match existing service patterns |191| `*/schemas.py` | Validation models per domain | Follow existing schema structure |192193### Rules1941. **Grep before creating** - Search shared modules for an existing function before writing a new one1952. **Inherit base exceptions** - New domain errors must extend the project's base error type1963. **Use shared clients** - Reuse the project's shared HTTP/DB client, never spin up a new one ad hoc1974. **Follow service pattern** - New services should match the structure of existing services198199---200201## Pattern Compliance Checklist202203### Before Submitting Code204- [ ] Follows existing component structure205- [ ] Uses established naming conventions206- [ ] Types defined in types/index.ts207- [ ] Error handling matches project style208- [ ] Uses `import type` where appropriate209- [ ] File location matches pattern210- [ ] Hooks follow return pattern211- [ ] Services follow error handling pattern212213---214215## Pattern Documentation216217### When to Document New Pattern218| Situation | Action |219|-----------|--------|220| New architectural decision | Document in ADR |221| Repeated pattern emerges | Add to skill documentation |222| Pattern deviation needed | Document reason in code comment |223| Breaking change | Update all related documentation |224225---226> Source: [komluk/scaffolding](https://github.com/komluk/scaffolding) — distributed by [TomeVault](https://tomevault.io).227<!-- tomevault:4.0:skill_md:2026-06-15 -->