AgentSkillsCN

Pattern Recognition

模式识别

SKILL.md

Pattern Recognition Skill

Standards for identifying and applying existing codebase patterns to maintain consistency.

When to Apply

  • Before writing new code
  • When implementing similar features
  • Code review for pattern consistency
  • Refactoring decisions

Pattern Detection Process

Step 1: Scan Existing Code

ActionPurpose
Find similar componentsMatch structure and naming
Find similar hooksMatch return types and patterns
Find similar servicesMatch error handling and API patterns
Check types locationEnsure types/index.ts usage

Step 2: Extract Patterns

ElementWhat to Look For
Component structureProps, hooks order, JSX structure
State managementZustand vs useState decisions
Error handlingTry/catch style, error messages
Naming conventionsFiles, functions, types
File organizationDirectory structure

Step 3: Apply Consistently

RuleDescription
Match existing styleNew code follows established patterns
Document deviationsIf pattern changes, document why
Refactor if neededUpdate old code to match new pattern

Naming Conventions

File Naming

TypeConventionExample
ComponentPascalCaseAnnotationCard.tsx
HookcamelCase with useuseVisualization.ts
ServicecamelCaseapiService.ts
TypescamelCase or indextypes/index.ts
StorecamelCase with StoreprojectStore.ts
UtilitycamelCaseformatters.ts

Code Naming

TypeConventionExample
ComponentPascalCaseAnnotationCard
FunctioncamelCase verbfetchProjects, handleClick
HookcamelCase with useuseVisualization
Type/InterfacePascalCaseProjectType, ButtonProps
ConstantUPPER_SNAKEAPI_BASE_URL, MAX_SIZE
VariablecamelCaseisLoading, userName

Component Patterns

Standard Component Structure

SectionOrderRequired
Imports1stYes
Props interface2ndYes
Component function3rdYes
Hooks declarationsInside, topYes
Event handlersInside, after hooksAs needed
Return JSXInside, lastYes

Component Organization by Type

TypeLocationPurpose
Page componentspages/Route entry points
Feature componentscomponents/[feature]/Feature-specific UI
Common componentscomponents/common/Reusable across features
Layout componentscomponents/layout/Page structure

Hook Patterns

Custom Hook Standards

ElementRequirement
Nameuse prefix + descriptive name
ReturnObject with named values
Statedata, loading, error pattern
DependenciesAll external values in dependency array

Hook Return Pattern

Return TypeUse Case
{ data, loading, error }Data fetching hooks
{ value, setValue, reset }Form/input hooks
{ isOpen, open, close, toggle }Toggle hooks

Service Patterns

API Service Standards

ElementRequirement
Async/awaitAll API calls use async/await
Error handlingTry/catch with console.error
Error format[ServiceName] Error description:
Return typePromise with typed response

Error Handling Pattern

ElementStandard
Log formatconsole.error('[Context] Message:', error)
User messageGeneric, no technical details
RethrowAfter logging for upstream handling

Type Patterns

Type Location Rules

RuleDescription
CentralizedAll shared types in types/index.ts
Import styleUse import type for type-only imports
Export styleUse export type for type exports
No interfacesPrefer type over interface for consistency

Type Naming

CategoryPatternExample
Entity[Entity]TypeProjectType, UserType
Props[Component]PropsButtonProps, CardProps
State[Domain]StateProjectState, UIState
API Response[Endpoint]ResponseProjectsResponse

Anti-Patterns

Anti-PatternProblemSolution
Props drillingHard to maintainUse Zustand or Context
Large componentsHard to test/readExtract sub-components
Inline stylesInconsistentUse MUI sx prop
any typeLoses type safetyDefine proper types
Barrel exportsCircular dependenciesUse direct imports
Mixed conventionsConfusingFollow established patterns

Pattern Compliance Checklist

Before Submitting Code

  • Follows existing component structure
  • Uses established naming conventions
  • Types defined in types/index.ts
  • Error handling matches project style
  • Uses import type where appropriate
  • File location matches pattern
  • Hooks follow return pattern
  • Services follow error handling pattern

Pattern Documentation

When to Document New Pattern

SituationAction
New architectural decisionDocument in ADR
Repeated pattern emergesAdd to skill documentation
Pattern deviation neededDocument reason in code comment
Breaking changeUpdate all related documentation