Coding Standards

入门 Starter 参考型 Reference ⚡ Claude Code 专属 ⚡ Claude Code Optimized
3 min read · 166 lines

Cross-language coding standards emphasizing immutability, small files, and error handling

Original files: rules/common/coding-style.md + rules/typescript/coding-style.md + rules/python/coding-style.md + rules/golang/coding-style.md

Coding Standards

Overview

Cross-language coding style rules emphasizing immutability, small file organization, comprehensive error handling, and input validation. Includes language-specific supplements for TypeScript/JavaScript, Python, and Go.

TypeScript/JavaScript

Applies to: *.ts, *.tsx, *.js, *.jsx

Immutable Updates

// WRONG: Mutation
function updateUser(user, name) {
  user.name = name  // MUTATION!
  return user
}

// CORRECT: Spread operator
function updateUser(user, name) {
  return { ...user, name }
}

Error Handling

try {
  const result = await riskyOperation()
  return result
} catch (error) {
  console.error('Operation failed:', error)
  throw new Error('Detailed user-friendly message')
}

Input Validation with Zod

import { z } from 'zod'

const schema = z.object({
  email: z.string().email(),
  age: z.number().int().min(0).max(150)
})

const validated = schema.parse(input)

Console.log Policy

  • No console.log in production code
  • Use proper logging libraries instead

Python

Applies to: *.py, *.pyi

  • Follow PEP 8 conventions
  • Use type annotations on all function signatures
  • Format with black, sort imports with isort, lint with ruff

Immutable Data Structures

from dataclasses import dataclass

@dataclass(frozen=True)
class User:
    name: str
    email: str

from typing import NamedTuple

class Point(NamedTuple):
    x: float
    y: float

Go

Applies to: *.go, go.mod, go.sum

  • gofmt and goimports are mandatory
  • Accept interfaces, return structs
  • Keep interfaces small (1-3 methods)

Error Wrapping

if err != nil {
    return fmt.Errorf("failed to create user: %w", err)
}

Summary

Principle Requirement
Immutability Always create new objects, never mutate
File size 200-400 lines typical, 800 max
Function size Under 50 lines
Nesting depth Max 4 levels
Error handling Explicit at every level, never swallow
Input validation Always validate at system boundaries
Hardcoded values Use constants or config instead

相关技能 Related Skills