software-design
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSoftware Design Philosophies
软件设计理念
Opinionated guidance on classic design principles and architectural patterns, with concrete code examples and trade-off analysis.
带有明确立场的经典设计原则与架构模式指导,包含具体代码示例与权衡分析。
When Applying Design Guidance
何时应用设计指导
- Diagnose first — Identify the specific design tension (coupling, cohesion, complexity, rigidity)
- Name the principle — State which principle applies and why
- Show the trade-off — Every principle has a cost; state it explicitly
- Demonstrate with code — Before/after examples grounded in the user's codebase context
- 先诊断 — 识别具体的设计矛盾(耦合、内聚、复杂度、僵化性)
- 明确原则 — 说明适用的原则及原因
- 展示权衡 — 每个原则都有成本,需明确指出
- 用代码演示 — 基于用户代码库上下文的前后对比示例
Core Stance: Pragmatism Over Dogma
核心立场:实用主义优于教条主义
- Principles are heuristics, not laws. Apply them when they reduce complexity; skip them when they add it.
- Prefer boring, obvious code over clever, abstract code.
- The best design is the simplest one that handles current requirements. Speculative generality is a code smell.
- Three concrete duplications are better than one wrong abstraction (Rule of Three).
- 原则是启发式方法,而非定律。当它们能降低复杂度时应用;若会增加复杂度则跳过。
- 优先选择乏味、直白的代码,而非精巧、抽象的代码。
- 最佳设计是能满足当前需求的最简单方案。投机性的通用设计是一种代码异味。
- 三次明确的重复比一次错误的抽象更好(三次原则)。
Classic Principles Quick Reference
经典原则速查
| Principle | One-liner | Apply when... | Skip when... |
|---|---|---|---|
| SRP | One reason to change | A module mixes unrelated concerns | Splitting creates indirection with no clarity gain |
| OCP | Extend, don't modify | You need plugin-style variation | You're still discovering requirements |
| LSP | Subtypes must be substitutable | Building type hierarchies | Using composition instead |
| ISP | Small, focused interfaces | Clients depend on methods they don't use | Interface has natural cohesion |
| DIP | Depend on abstractions | You need testability or swappable implementations | Only one implementation exists or will ever exist |
| DRY | Single source of truth | Identical logic with identical reasons to change | Similar-looking code with different reasons to change |
| KISS | Simplest solution that works | Always the default | Never skip this |
| YAGNI | Don't build it until you need it | Tempted to add "just in case" features | Building foundational APIs with known extension points |
For detailed explanations, examples, and anti-patterns for each principle, see references/principles.md.
| 原则 | 一句话总结 | 适用场景 | 避免场景 |
|---|---|---|---|
| SRP | 单一变更原因 | 模块混杂了不相关的职责 | 拆分后会增加间接性且无清晰度提升 |
| OCP | 扩展而非修改 | 需要插件式变体的场景 | 仍在探索需求的阶段 |
| LSP | 子类型必须可替换 | 构建类型层级结构时 | 改用组合方式实现时 |
| ISP | 小而聚焦的接口 | 客户端依赖了未使用的方法时 | 接口具有自然内聚性时 |
| DIP | 依赖抽象而非具体实现 | 需要可测试性或可替换实现的场景 | 仅存在一种实现且未来也不会有其他实现时 |
| DRY | 单一事实来源 | 逻辑完全相同且变更原因一致的场景 | 外观相似但变更原因不同的代码 |
| KISS | 最简单的可行方案 | 始终作为默认选择 | 绝不跳过此原则 |
| YAGNI | 无需则不构建 | 想要添加“以防万一”的功能时 | 构建已知有扩展点的基础API时 |
如需每个原则的详细解释、示例及反模式,请查看references/principles.md。
Architectural Patterns Quick Reference
架构模式速查
| Pattern | Best for | Avoid when |
|---|---|---|
| Clean/Hexagonal | Long-lived systems with complex domains | Simple CRUD apps, prototypes |
| DDD | Complex business logic with domain experts | Technical/infrastructure-heavy systems |
| Event-Driven | Decoupled workflows, audit trails | Simple request/response flows |
| CQRS | Read/write asymmetry, complex queries | Uniform read/write patterns |
| Monolith-first | New projects, small teams | Already proven need for independent deployment |
| Microservices | Independent team deployment at scale | Small team, shared database, tight coupling |
For detailed guidance on each pattern including structure, trade-offs, and code examples, see references/architecture.md.
| 模式 | 最佳适用场景 | 避免场景 |
|---|---|---|
| Clean/Hexagonal | 生命周期长、领域复杂的系统 | 简单CRUD应用、原型 |
| DDD | 业务逻辑复杂且有领域专家参与的系统 | 偏重技术/基础设施的系统 |
| Event-Driven | 解耦的工作流、审计追踪 | 简单的请求/响应流程 |
| CQRS | 读写不对称、复杂查询 | 读写模式统一的场景 |
| Monolith-first | 新项目、小团队 | 已明确需要独立部署的场景 |
| Microservices | 大规模下团队独立部署的场景 | 小团队、共享数据库、高耦合的系统 |
如需每个模式的详细指导,包括结构、权衡及代码示例,请查看references/architecture.md。
Decision Framework
决策框架
When advising on design, follow this priority order:
- Does it work correctly? — Correctness over elegance
- Can someone else read it? — Clarity over cleverness
- Can it be tested? — Testability over convenience
- Can it change safely? — Isolation of change over DRY
- Does it perform adequately? — Performance only when measured
提供设计建议时,请遵循以下优先级顺序:
- 是否能正确运行? — 正确性优于优雅性
- 其他人能否读懂? — 清晰度优于精巧性
- 是否可测试? — 可测试性优于便利性
- 能否安全变更? — 变更隔离优于DRY原则
- 性能是否足够? — 仅在有性能指标要求时才考虑性能
Common Design Smells and Remedies
常见设计异味与解决方法
| Smell | Likely violation | Remedy |
|---|---|---|
| God class / function | SRP | Extract cohesive responsibilities |
| Shotgun surgery (one change touches many files) | Low cohesion | Colocate related logic |
| Feature envy (method uses another object's data more than its own) | Misplaced responsibility | Move method to the data owner |
| Primitive obsession | Missing domain concept | Introduce a value type |
| Deep inheritance trees | Favoring inheritance over composition | Flatten with composition/interfaces |
| Boolean parameters | SRP, OCP | Split into separate functions |
| Speculative generality | YAGNI | Delete unused abstractions |
| 设计异味 | 可能违反的原则 | 解决方法 |
|---|---|---|
| 上帝类/上帝函数 | SRP | 提取具有内聚性的职责 |
| 霰弹式修改(一次变更涉及多个文件) | 低内聚 | 将相关逻辑集中放置 |
| 特性羡慕(方法更多使用其他对象的数据而非自身数据) | 职责错位 | 将方法移至数据所属对象 |
| 原始类型痴迷 | 缺失领域概念 | 引入值类型 |
| 深层继承树 | 偏好继承而非组合 | 用组合/接口扁平化结构 |
| 布尔参数 | SRP、OCP | 拆分为独立函数 |
| 投机性通用设计 | YAGNI | 删除未使用的抽象 |
Offering Guidance
提供指导的要点
When reviewing or advising:
- Reference the specific principle by name with a one-sentence rationale
- Show a minimal before/after code example in the user's language
- State the trade-off honestly ("this adds an interface but isolates the database dependency")
- If multiple principles conflict, state the tension and recommend based on context
- Load references/principles.md for deep-dive principle discussions
- Load references/architecture.md for architectural pattern guidance
进行评审或提供建议时:
- 引用具体原则的名称并附上一句话理由
- 展示用户所用语言的极简前后代码对比示例
- 坦诚说明权衡点(例如:“这会增加一个接口,但能隔离数据库依赖”)
- 若多个原则存在冲突,说明矛盾点并结合上下文给出建议
- 加载references/principles.md获取原则的深度讨论
- 加载references/architecture.md获取架构模式的指导内容