使用一个方法做不止一件事是否违反了单一职责原则?

发布于 2024-08-24 05:16:26 字数 173 浏览 14 评论 0原文

出于我的目的,我需要在 xml 文件中搜索特定节点,如果找到,请将其删除。我应该将搜索功能提取到其自己的方法中,并将删除功能提取到其自己的方法中吗?这样做似乎更昂贵,因为我将搜索 xml 文件一次以查看它是否存在,然后再次搜索以将其删除。如果我将这两个功能合并到一个方法中,我可以在找到它时立即将其删除。我在这里理解的SRP正确吗?

For my purposes, I need to search for a specific node in an xml file and, if found, delete it. Should I pull search functionality out into its own method and delete functionality out into its own method? It seems more expensive to do it this way because I'll be searching the xml file once to see if it exists and searching it again to delete it. If I combine these two functionalities into a single method I can delete it right when I find it. Am I understanding SRP correctly here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

友欢 2024-08-31 05:16:26

您是否还有其他原因/情况需要搜索 xml 文件?一般来说,在任何级别将不同的工作分开是一件好事,无论遵守或违反某人的规则(这是我的规则;-))。分离这些函数可能(?)还会使您的代码更易于理解,这可能比性能上的微小提升更重要。

Do you have any other reasons/situations in which you are searching the xml file? In general it is a Good Thing to separate distinct jobs at any level, regardless of adhering to or violating someone's rule (that's my rule ;-) ). Separating these functions might (?) also make your code more understandable, which may turn out to be more important than a trivial gain in performance.

夏尔 2024-08-31 05:16:26

您的普通 XML 解析器将创建知道其父级的节点,因此您可以执行以下操作:

XmlNode node = this.FindNode(filter);
node.ParentNode.DeleteChild(node);

这样您就可以拆分两个函数,但没有开销。

关于您的问题的核心:是的,在一种方法中搜索和删除违反了单一责任,但性能和 SRP 在许多情况下并不能很好地混合,因此您必须决定什么更重要。

PS:
示例与任何真实语言都没有(故意)相关。

Your average XML Parser will create Nodes which know their Parents so you can do something like:

XmlNode node = this.FindNode(filter);
node.ParentNode.DeleteChild(node);

This way you have split both functions but no overhead.

Regarding the core of your Question: Yes, searching and deleting in one Method violates the single responsibility but performance and SRP don't mix that well in many cases so you have to decide whats more important.

PS:
Example is not (knowingly) related to any real language out there.

春夜浅 2024-08-31 05:16:26

不,单一职责原则与如何编写代码的细节无关。它是关于如何将程序的功能划分为类。它说,如果一个类可能由于多个原因而发生变化,那么它应该是两个类。一个典型的例子是构造和格式化报告的类;报告的内容和报告的格式可能在不同的时间发生变化,因此该类是重构为两个的良好候选者。

您没有说明您的类的功能职责是什么,但是,从您的类应该完成的任何工作的角度来看,搜索和删除 XML 节点只是该单一工作的一部分,并且执行它们在同一类中且在一次操作中不违反 SRP。

(另一方面,如果您的类有大量域逻辑以及大量有关操作 XML 的具体细节,则会违反 SRP。)

No, the Single Responsibility Principle isn't about the details of how code is written. It's about how to divide up the functionality of a program into classes. It says that if a class is likely to change for more than one reason, it should be two classes. A classic example is a class that constructs and formats a report; the contents of the report and the format of the report are likely to change at different times, so that class is a good candidate for refactoring into two.

You don't say what the functional responsibility of your class is, but, from the point of view of whatever job your class is supposed to get done, searching for and deleting the XML node are just parts of that single job, and doing them in the same class and in one operation does not violate the SRP.

(On the other hand, if your class had a lot of domain logic and also a lot of nuts and bolts about manipulating XML, it would violate the SRP.)

贵在坚持 2024-08-31 05:16:26

它确实违反了命令查询分离原则,我认为这是相辅相成的-手与SRP。搜索和删除是两个可以改变的事情,因此它们也可以被定义为两个单独的职责。它们可以单独进行单元测试,您可能在查找节点的方式上有错误,但在删除方面则不然。您可能还想模拟删除部分。它还为您提供了查找和删除之间的中间点(这又回到了单元测试和调试)。

总而言之,我想说命令查询分离有很多好处,因此我会尽可能遵循它。

不要过早优化你的代码!以最可维护的方式/最好的设计来编写它,如果它是一个瓶颈,你可以调整它。

It does violate Command Query Separation Principle which I feel goes hand-in-hand with SRP. Searching and deleting are two things can change so those could also be defined as two separate responsibilities. They can be unit tested separately, you may have a bug in how you find the node, but not in the deletion. You may also want to mock out the deletion part. It also gives you an intermediate point in between the finding and deleting (again this goes back to unit testing and debugging).

All in all I'd say there are lots of benefits to command query separation so I try to follow it whenever possible.

Don't prematurely optimize your code! Write it in the most maintainable way / best design you can, then IF it is a bottle neck you can tweak it.

夏有森光若流苏 2024-08-31 05:16:26

也许也应该有一个职责封装原则

因此,想要执行某些职责的调用者必须调用单个方法才能执行它......例如......

void MoveMoney(int amount){
     _myBank.ReduceBy(amount);
     _theirBank.IncreaseBy(amount);
}

而不是将其留给调用者必须自己调用许多方法来履行职责……

 _myBank.ReduceBy(amount);
 _theirBank.IncreaseBy(amount);

我想首先确定所有“职责”无疑是做好这件​​事的关键。我只是在这里大声思考。

与避免两阶段初始化反模式类似:
避免两阶段初始化
(这基本上是将创建类实例的责任分解为多个构造函数/方法调用)

Maybe there should be a Responsibility Encapsulation Principle too

So a caller wanting to perform some responsibility, has to call a single method in order to to perform it... for example...

void MoveMoney(int amount){
     _myBank.ReduceBy(amount);
     _theirBank.IncreaseBy(amount);
}

rather than leaving it to the caller to carry out the responsibility by having to call numerous methods itself...

 _myBank.ReduceBy(amount);
 _theirBank.IncreaseBy(amount);

I guess identifying all the "reponsibilities" up front in the first place would no doubt be key to doing this well. I'm kind of just thinking out loud here.

Similar to avoiding the two phase initialisation antipattern:
Avoiding two phase initialization
(which is basically bleeding the responsibility of creating the instance of a class into multiple contructor/method calls)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文