为什么缺乏凝聚力的方法(LCOM)中包括getter和setter [英] Why Does Lack of Cohesion Of Methods (LCOM) Include Getters and Setters
问题描述
我在看的LCOM指标如下所示,
I am looking at the LCOM metric as shown here,
http://www.ndepend.com/Metrics.aspx
因此,我们说了一些东西,
So we are saying a few things,
1) A class is utterly cohesive if all its methods use all its instance fields
2) Both static and instance methods are counted, it includes also constructors, properties getters/setters, events add/remove methods
如果我看的一类,如此,
If I look at a class such as this,
public class Assessment
{
public int StartMetres { get; set; }
public int EndMetres { get; set; }
public decimal? NumericResponse { get; set; }
public string FreeResponse { get; set; }
public string Responsetype { get; set; }
public string ItemResponseDescription { get; set; }
public string StartText { get; set; }
public decimal? SummaryWeight { get; set; }
}
它得到一个坏的评分为0.94,因为每个getter和setter不访问所有其他实例字段的
It gets a bad score of 0.94 because each getter and setter doesn't access 'all of the other instance fields'.
据计算这个样子,
accessAverage - methodCount / 1 - methodCount
(2 - 17) / (1 - 17) = 0.94 (rounded)
我不理解这个指标,为什么要包括getter和setter方法?一个getter和setter将永远只能访问一个单一的实例字段。
I am not understanding this metric, why should it include getters and setters? A getter and setter will always only access one single instance field.
推荐答案
这表明,每个软件度量是有缺陷的,如果你一味地把它发挥到极致。
This demonstrates that every software metric is flawed if you blindly take it to its extreme.
您知道一个无凝聚力级,当你看到一个。例如:
You know an "incohesive" class when you see one. For example:
class HedgeHog_And_AfricanCountry
{
private HedgeHog _hedgeHog;
private Nation _africanNation;
public ulong NumberOfQuills { get { return _hedgeHog.NumberOfQuills; } }
public int CountOfAntsEatenToday { get { return _hedgeHog.AntsEatenToday.Count(); } }
public decimal GrossDomesticProduct { get { return _africanNation.GDP; } }
public ulong Population { get { return _africanNation.Population; } }
}
这显然是一个无凝聚力类,因为它包含两个数据不需要与彼此
This is obviously an incohesive class, because it contains two pieces of data that don't need to be with one another.
不过,虽然这是明显地向我们这个类是无凝聚力,你怎么能得到一个软件程序来确定incohesion?如何将它告诉大家,上面的类是无凝聚力,但这不是?
But while it's obvious to us that this class is incohesive, how can you get a software program to determine incohesion? How would it tell that the above class is incohesive, but this isn't?
class Customer
{
public string FullName { get; set; }
public Address PostalAddress { get; set; }
}
他们想出了度量肯定检测incohesion,而且还附带了误报。
The metric they came up with certainly detects incohesion, but also comes up with false positives.
如果你确定这个指标是很重要的是什么?你可以创建一个CustomerData级只包含字段和公开的数据字段属性客户类。
What if you decided this metric was important? You could create a "CustomerData" class containing just fields, and a "Customer" class that exposes the data fields as properties.
// This has no methods or getters, so gets a good cohesion value.
class CustomerData
{
public string FullName;
public Address PostalAddress;
}
// All of the getters and methods are on the same object
class Customer
{
private CustomerData _customerData;
public string FullName { get { return _customerData.FullName; } }
// etc
}
但是,如果我在玩这个游戏,我可以把它应用到无凝聚力的例子还有:
But if I'm playing this game, I can apply it to the incohesive example as well:
class Hedgehog_And_AfricanCountry_Data
{
public Hedgehog _hedgehog;
public AfricanNation _africanNation;
}
class Hedgehog_And_AfricanCountry
{
private Hedgehog_And_AfricanCountry_Data _hedgehogAndAfricanCountryData;
// etc;
}
说真的,我认为这是最好理解的凝聚力是什么,以及为什么它是一个值得追求的目标,但也明白,一个软件工具不能正确测量。
Really, I think it's best to understand what cohesion is, and why it's a worthwhile goal, but also understand that a software tool can not properly measure it.
这篇关于为什么缺乏凝聚力的方法(LCOM)中包括getter和setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!