0

关于横切关注点和面向方面的编程,您经常阅读有关代码缠结的信息。本文1将代码纠缠描述为:

软件系统中的模块可以同时与多个需求交互。例如,开发人员经常同时考虑业务逻辑、性能、同步、日志记录和安全性。如此众多的需求导致同时存在来自每个关注点的实现的元素,从而导致代码纠结。

这不和低凝聚力完全一样吗?高缠结和低内聚之间有什么区别,还是两个不同的词描述的是同一件事?

4

2 回答 2

2

Accourding to wikipedia:

The implementation of a concern is tangled if its code is intermixed with code that implements other concerns. The module in which tangling occurs is not cohesive.

Cohesion is decreased if:
- The functionalities embedded in a class, accessed through its methods, have little in common.
- Methods carry out many varied activities, often using coarsely-grained or unrelated sets of data.

So.. when the code is tangled, it would violate SOLID principles such Single Responsibility Principle, Open Closed Priciple etc.

All these principles most often go together and violation of one principle/best practice lead to another.

But tangling doesn't necessarily mean that the code is not cohesive. For example we could have a class called SecurityChecker, which does the authentication of a user log all authentication related activties. Clearly this would be handling multiple concerns which are Authentication and Logging. Therefroe it would be a tangled class. On the other hand both these conerns would be operating on the same set of data which in this case could be user data, times of logon , number of login attempts etc. Therefore cohesion could still be high.

Genrally most of these principles/guidlines/best practices look at the same issue from different perspectives, and the end goal is to manage dependencies between different components/classes etc so that the overall design would be more maintainable , efficient and elegant in the long run.

于 2012-07-10T14:25:21.607 回答
0

非常相似是的。

Cohesion is used to indicate the degree to which a class has a single, well-focused purpose.

因此,如果您有一个具有单一目标的班级,那么它不会因为尝试做不止一件事而“纠结”。

于 2012-07-10T13:05:10.663 回答