挑战单一职责原则
Challenging the Single-Responsibility Principle

原始链接: https://kiss-and-solid.com/blog/keep-it-simple

单一职责原则(SRP)虽然有价值,但常常被过度应用,导致系统过度碎片化和复杂化。不要无休止地解构代码,而是要专注于**最小化代码和最大化用例**——构建可重用组件以减少整体代码库大小并提高可维护性。 Quasar架构风格中的Siedersleben“血型法则”为此提供了一个有用的框架,它将组件分为: * **0型(通用):** 高度可重用,独立于业务或技术细节(例如Lodash)。可以安全地通用。 * **T型(技术):** 处理技术问题,可能可以开源(例如Knex)。 * **A型(领域):** 包含核心业务逻辑。持续重构以提取可重用的代码到0型或T型。 关键在于,要避免**AT型**——技术组件*使用*领域逻辑,因为这会阻碍重用性。目标是将尽可能多的功能转移到更可重用的0型和T型组中,从而获得更简洁、更具适应性的软件。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 挑战单一职责原则 (kiss-and-solid.com) 7 分,作者 WolfOliver 1小时前 | 隐藏 | 过去 | 收藏 | 讨论 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文

As part of the SOLID principles, the single-responsibility principle (SRP) is one of the most prominent software engineering principles. Unfortunately, it is often used as a killer argument to chop up a software system beyond all recognition.

The problem with this argument is that no matter how small a component already is, the single-responsibility principle can still be applied: every line of code can be assigned its own responsibility. I've seen many teams building distributed monoliths and spaghetti code in pursuit of the SRP.

Minimize code, maximize use cases

When it comes to deciding where to place which code, I found a particular mantra very helpful: minimize code and maximize use cases. The idea is to have as many reusable software components as possible and to minimize the overall code within an organization.

Even though you might not actually have a second project where you might want to reuse something, building for reusability leads to elegant and maintainable software architecture.

The blood group law

A very concrete method on how to approach reusability can be found in Siedersleben's blood group law. This principle is part of the "Quasar architecture style" (see references below). In contrast to other vague guidelines, I find this very easy to apply in practice.

According to this method, you can categorize components into different groups. The following explanations are partly mixed with my own interpretation.

Group 0: Generic components

This is the most generic group. This code is not polluted with technical details or with business logic. In the JavaScript world Lodash, bluebird, jsonpath, Luxon, etc. would be examples of group 0 components.

The big advantage of a group 0 component is that you can consume it within components from any other group (like blood type 0 can be received by any other type). Consuming a type 0 component from any other component will not reduce the reusability of the other component.

Group T: Technical components

These are technical components. Examples are: nconf, knex, metascraper, jsonwebtoken, passport, etc. These components also have value outside your company and are good candidates for open-sourcing.

Group A: Domain components

An A-component contains the domain logic. You should constantly scan your A components, try to generalize logic, and move code into Group 0 or Group T. Once you find such code, you can either replace it with an open-source solution or create an open-source solution. Most developers will be surprised how much 0/T code they find within A components. The goal should be to have as much code as possible in the 0/T group.

Group AT: The anti-pattern

This is a T component that calls or uses an A component. This type needs to be avoided.

References

联系我们 contact @ memedata.com