Modular Code

作者 parcadeid07ff4b06b62無授權條款3.9K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫8 個月前更新

Modular Code Organization

AI 產生的概覽

指導將 Python 程式碼拆分為易於維護與 AI 輔助編輯的模組化檔案。

功能
提供將 Python 程式碼組織為模組的準則,包括檔案行數門檻、何時拆分檔案的判斷標準,以及依領域概念、抽象層、資料型別與 I/O 邊界等自然拆分點。也提出套件結構建議,以及模組命名與組織的注意事項。內容包含拆分現有大型檔案的重構流程,並列出程式碼庫中的候選檔案。
適用情境
適用於將 Python 程式碼組織或重構為模組、判斷檔案是否過大,或規劃如何拆分現有大型檔案時。目標是提升可維護性並支援 AI 輔助開發。
執行需求
無需工具、套件或憑證;僅為說明性指示,不附帶指令碼。

Modular Code Organization

Write modular Python code with files sized for maintainability and AI-assisted development.

File Size Guidelines

LinesStatusAction
150-500OptimalSweet spot for AI code editors and human comprehension
500-1000LargeLook for natural split points
1000-2000Too largeRefactor into focused modules
2000+CriticalMust split - causes tooling issues and cognitive overload

When to Split

Split when ANY of these apply:

  • File exceeds 500 lines
  • Multiple unrelated concerns in same file
  • Scroll fatigue finding functions
  • Tests for the file are hard to organize
  • AI tools truncate or miss context

How to Split

Natural Split Points

  1. By domain concept: auth.py → auth/login.py, auth/tokens.py, auth/permissions.py
  2. By abstraction layer: Separate interface from implementation
  3. By data type: Group operations on related data structures
  4. By I/O boundary: Isolate database, API, file operations

Package Structure

feature/├── __init__.py      # Keep minimal, just exports├── core.py          # Main logic (under 500 lines)├── models.py        # Data structures├── handlers.py      # I/O and side effects└── utils.py         # Pure helper functions

DO

  • Use meaningful module names (data_storage.py not utils2.py)
  • Keep __init__.py files minimal or empty
  • Group related functions together
  • Isolate pure functions from side effects
  • Use snake_case for module names

DON'T

  • Split files arbitrarily by line count alone
  • Create single-function modules
  • Over-modularize into "package hell"
  • Use dots or special characters in module names
  • Hide dependencies with "magic" imports

Refactoring Large Files

When splitting an existing large file:

  1. Identify clusters: Find groups of related functions
  2. Extract incrementally: Move one cluster at a time
  3. Update imports: Fix all import statements
  4. Run tests: Verify nothing broke after each move
  5. Document: Update any references to old locations

Current Codebase Candidates

Files over 2000 lines that need attention:

  • Math compute modules (scipy, mpmath, numpy) - domain-specific, may be acceptable
  • patterns.py - consider splitting by pattern type
  • memory_backfill.py - consider splitting by operation type

Sources

來源與署名

來源:parcadei/continuous-claude-v3位於.claude/skills/modular-code提交d07ff4b

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架