algorithm - LCOM4询问计算方式

标签 algorithm oop metrics code-metrics lcom

最近,我在计算 LCOM4 时遇到了一个语义问题,LCOM4 是一种用于查找类的方法和属性的内聚性的指标。

简介

LCOM4 是“计算缺乏内聚性的第四种方法”,Hitz 和 Montazeri (http://www.isys.uni-klu.ac.at/PDF/1995-0043-MHBM.pdf) 对此进行了描述,是目前定义一个类拥有多少职责的最佳方法。

我会尽量不使用特定的开发语言,因为我的问题是针对所有 OOP 语言。

让我基本上用默认算法向不知道的人解释它是如何工作的:

Class Foo {
    property a,b

    function f1() { this.a = 1 }
    function f2() { this.f1() }
    function f3() { this.b = 3 }
}

这个类有两个流程:

  • 属性a由f1()和f2()共享
  • 属性b由f3()共享

所以 Foo 的 LCOM4 是 2。

例如,让我们更改函数 f2() 以共享属性 b。

Class Foo {
    property a,b

    function f1() { this.a = 1 }
    function f2() { this.f1(); this.b = 1 }
    function f3() { this.b = 3 }
}

现在这个类只有一个流程:

  • 属性 a 和 b 由 f1()、f2() 和 f3() 共享。

这意味着 Foo 的 LCOM4 现在是 1。

LCOM4 = 0 或 LCOM4 = 1 意味着该类没有或只有 1 个责任,这是每个开发人员都必须希望他们的类,因为他们尊重 S< 的 S/strong>OLID 良好实践。

您可以在此处通过图表找到更多信息:http://www.aivosto.com/project/help/pm-oo-cohesion.html#LCOM4

我的问题

假设您编写了这样一个类:

Class Bar {
    property a

    static function build() { return new self }
    function Bar() { this.a = 5 }
}

...当然,在执行 new self 时,我使用声明的 Bar 方法创建了一个新的 Bar 实例。

根据 Hitz 和 Montazeri 的工作,我的类 Bar 的 LCOM4 是什么?

我使用的很多度量工具都说 LCOM4=2,但对我来说,该类只有 1 个责任,因此它的 LCOM4 必须为 1。 此外,即使不是很明确,方法 build()Bar() 也必须属于与 build() 相同的函数图> 正在调用 Bar()(嗯,我知道,它正在调用另一个实例,但即使它不是同一个对象,它也是同一个类)。

您对此有何看法?

有没有人知道如何处理这类类(class)? (我读了很多 Hitz 和 Montazeri 的文档,但我可能遗漏了一些)

如果没有答案,是否可以改进LCOM4的计算方式,使其更接近类的职责数?

顺便说一句,我的案例是在 PHP 中,但我认为这个问题也涉及所有其他 OOP 语言。

谢谢大家

最佳答案

根据您提供的文件:

In addition to this mere formal improvement of the definition of LCOM, we would like to get rid of a more semantic flaws in the definition of LCOM: Firstly, the not uncommon design principle to restrict accesses to instance variables to special purpose read/write methods introduces an anomaly of this measure: An otherwise cohesive class would yield very high LCOM-values, as all of the “real” methods would yield isolated nodes in the graph, as they do not directly share any instance variable anymore.

我将其解释为静态方法和实例方法是分开的,因此整体 LCOM4 = 2。定义支持该结果:

They define Lack of Cohesion in Methods (LCOM) as the number of pairs of methods operating on disjoint sets of instance variables, reduced by the number of method pairs acting on at least one shared instance variable.

在您的情况下,如上所述,LCOM4 = 1 + 1 - 0 = 2。

关于algorithm - LCOM4询问计算方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490962/

相关文章:

python - 值错误 : 'balanced_accuracy' is not a valid scoring value in scikit-learn

algorithm - Catch the Plane 问题来自 ACM ICPC 2018 世界总决赛

java - 测试井字游戏的获胜条件

java - NullPointerException 未引用任何对象

android - Flurry Session 数据对比 Google Analytics

eclipse - 无法在 eclipse neon 1 中安装度量插件

java - 使用在 java 中实现的中位数为快速选择选择枢轴?

c - 使用递归函数以相反顺序打印数组

oop - 在抽象工厂模式中,主要的抽象工厂类,它总是必须是抽象类吗?

php 工具包而不是框架