Skill站外收录 @skilllib/skill-2117939930272417276
modular-code
倡导将 Python 代码按功能域、抽象层级和 I/O 边界划分为职责清晰的小型模块,单文件控制在 150–500 行以兼顾可读性与 AI 工具处理效率,避免过大文件导致上下文截断或维护困难。
软件开发 AI 技能
Skills.MD
# Modular Code Organization
Write modular Python code with files sized for maintainability and AI-assisted development.
## File Size Guidelines
| Lines | Status | Action |
|-------|--------|--------|
| 150-500 | Optimal | Sweet spot for AI code editors and human comprehension |
| 500-1000 | Large | Look for natural split points |
| 1000-2000 | Too large | Refactor into focused modules |
| 2000+ | Critical | Must 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
- [The Hitchhiker's Guide to Python](https://docs.python-guide.org/writing/structure/)
- [Python Project Best Practices - Dagster](https://dagster.io/blog/python-project-best-practices)
- [Right-Sizing Python Files for AI Editors](https://medium.com/@eamonn.faherty_58176/right-sizing-your-python-files-the-150-500-line-sweet-spot-for-ai-code-editors-340d550dcea4)
- [PEP 8 Style Guide](https://peps.python.org/pep-0008/)
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「modular-code」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次