AgentSkillsCN

coding-standards

运行 OpenAI Codex CLI,实现代码分析、重构与自动化编辑,并可自定义沙箱权限与推理强度。

SKILL.md
--- frontmatter
name: coding-standards
description: Core coding principles and design aphorisms for writing maintainable code. Use when writing, reviewing, or refactoring code. Triggers on discussions about null handling, error handling, encapsulation, data structures, dependencies, state management, method signatures, or general code quality.
license: MIT
metadata:
  author: latitude
  version: "1.0.0"

Coding Standards

Principles for writing maintainable, debuggable code that stands the test of time.

When to Apply

Reference these guidelines when:

  • Writing new code or reviewing pull requests
  • Debugging issues caused by unclear data flow
  • Designing APIs, method signatures, or data structures
  • Refactoring code for maintainability
  • Deciding how to handle errors and edge cases
  • Managing state in applications

Principles at a Glance

PrincipleCore IdeaKey Rule
Nil ValuesNull means absence, nothing elseNever give semantic meaning to null
Grep TestCode must be findableIf you can't grep it, it's too clever
Data FirstStructure determines codeDesign data structures before algorithms
Stable DependenciesDepend on what's stableDepend on abstractions, not implementations
Loud FailuresHidden bugs are worseIf it can fail, make it fail loudly
Tell, Don't AskObjects own their behaviorDon't query state to make external decisions
Avoid DichotomiesReality is nuancedReject false binary choices
Single Source of TruthState lives in one placeNever derive state from state
Explicit ParametersClarity over clevernessAvoid booleans and hashes as arguments
Task ParametersFresh data in async codePass IDs to tasks, not models

Quick Reference

Null Handling

Bad: Using null to signal a semantic condition

typescript
const role = ROLES[user.role] // Returns undefined if not found
processRole(role) // Explodes somewhere far away

Good: Validate at boundaries, use explicit types

typescript
const role = ROLES[user.role]
if (!role) throw new Error(`Unknown role: ${user.role}`)
processRole(role)

Findable Code

Bad: Dynamic method dispatch

typescript
const method = `handle${eventType}`
this[method](data) // Good luck finding handleUserCreated

Good: Explicit mapping

typescript
const handlers = {
  userCreated: this.handleUserCreated,
  userDeleted: this.handleUserDeleted,
}
handlers[eventType]?.(data)

Dependency Direction

Bad: Concrete depends on concrete

typescript
class PaymentService {
  private stripe = new StripeClient() // Locked to Stripe forever
}

Good: Depend on abstractions

typescript
class PaymentService {
  constructor(private gateway: PaymentGateway) {} // Any gateway works
}

Error Handling

Bad: Silent failures

typescript
try {
  await saveUser(user)
} catch (e) {
  // Swallowed silently
}

Good: Fail loudly or handle explicitly

typescript
const result = await saveUser(user)
if (result.error) {
  captureException(result.error)
  throw result.error
}

Tell, Don't Ask

Bad: Query state, then act

typescript
if (monitor.getValue() > monitor.getLimit()) {
  monitor.triggerAlarm()
}

Good: Let the object decide

typescript
monitor.setValue(newValue) // Triggers alarm internally if needed

State Management

Bad: Derived state

typescript
const [items, setItems] = useState(props.items) // Copied from props

Good: Single source of truth

typescript
const items = props.items // Always read from source

Method Signatures

Bad: Boolean parameter

typescript
viewController.present(other, true) // What does true mean?

Good: Explicit methods

typescript
viewController.animatePresentation(other)
viewController.immediatelyPresent(other)

Async Tasks

Bad: Passing models to queues

typescript
queue.add('processUser', { user }) // Stale by execution time

Good: Pass identifiers

typescript
queue.add('processUser', { userId: user.id }) // Fetch fresh data in job

Detailed Guide

Read the GUIDE.md file for comprehensive explanations, rationale, and additional examples for each principle.