software-design

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Software Design Philosophies

软件设计理念

Opinionated guidance on classic design principles and architectural patterns, with concrete code examples and trade-off analysis.
带有明确立场的经典设计原则与架构模式指导,包含具体代码示例与权衡分析。

When Applying Design Guidance

何时应用设计指导

  1. Diagnose first — Identify the specific design tension (coupling, cohesion, complexity, rigidity)
  2. Name the principle — State which principle applies and why
  3. Show the trade-off — Every principle has a cost; state it explicitly
  4. Demonstrate with code — Before/after examples grounded in the user's codebase context
  1. 先诊断 — 识别具体的设计矛盾(耦合、内聚、复杂度、僵化性)
  2. 明确原则 — 说明适用的原则及原因
  3. 展示权衡 — 每个原则都有成本,需明确指出
  4. 用代码演示 — 基于用户代码库上下文的前后对比示例

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

经典原则速查

PrincipleOne-linerApply when...Skip when...
SRPOne reason to changeA module mixes unrelated concernsSplitting creates indirection with no clarity gain
OCPExtend, don't modifyYou need plugin-style variationYou're still discovering requirements
LSPSubtypes must be substitutableBuilding type hierarchiesUsing composition instead
ISPSmall, focused interfacesClients depend on methods they don't useInterface has natural cohesion
DIPDepend on abstractionsYou need testability or swappable implementationsOnly one implementation exists or will ever exist
DRYSingle source of truthIdentical logic with identical reasons to changeSimilar-looking code with different reasons to change
KISSSimplest solution that worksAlways the defaultNever skip this
YAGNIDon't build it until you need itTempted to add "just in case" featuresBuilding 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

架构模式速查

PatternBest forAvoid when
Clean/HexagonalLong-lived systems with complex domainsSimple CRUD apps, prototypes
DDDComplex business logic with domain expertsTechnical/infrastructure-heavy systems
Event-DrivenDecoupled workflows, audit trailsSimple request/response flows
CQRSRead/write asymmetry, complex queriesUniform read/write patterns
Monolith-firstNew projects, small teamsAlready proven need for independent deployment
MicroservicesIndependent team deployment at scaleSmall 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:
  1. Does it work correctly? — Correctness over elegance
  2. Can someone else read it? — Clarity over cleverness
  3. Can it be tested? — Testability over convenience
  4. Can it change safely? — Isolation of change over DRY
  5. Does it perform adequately? — Performance only when measured
提供设计建议时,请遵循以下优先级顺序:
  1. 是否能正确运行? — 正确性优于优雅性
  2. 其他人能否读懂? — 清晰度优于精巧性
  3. 是否可测试? — 可测试性优于便利性
  4. 能否安全变更? — 变更隔离优于DRY原则
  5. 性能是否足够? — 仅在有性能指标要求时才考虑性能

Common Design Smells and Remedies

常见设计异味与解决方法

SmellLikely violationRemedy
God class / functionSRPExtract cohesive responsibilities
Shotgun surgery (one change touches many files)Low cohesionColocate related logic
Feature envy (method uses another object's data more than its own)Misplaced responsibilityMove method to the data owner
Primitive obsessionMissing domain conceptIntroduce a value type
Deep inheritance treesFavoring inheritance over compositionFlatten with composition/interfaces
Boolean parametersSRP, OCPSplit into separate functions
Speculative generalityYAGNIDelete 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获取架构模式的指导内容