[英]Why Does Lack of Cohesion Of Methods (LCOM) Include Getters and Setters
我正在查看此處顯示的LCOM指標,
http://www.ndepend.com/Metrics.aspx
所以我們說了幾件事,
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
如果我看一下這樣的課,
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都不會訪問“所有其他實例字段”。
它是這樣計算的,
accessAverage - methodCount / 1 - methodCount
(2 - 17) / (1 - 17) = 0.94 (rounded)
我不理解這個指標,為什么它應該包括getter和setter? getter和setter將始終只訪問一個實例字段。
這表明,如果您盲目地將其發揮到極致,那么每個軟件指標都是有缺陷的。
當你看到一個時,你知道一個“不連貫”的課程。 例如:
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; } }
}
這顯然是一個不連貫的類,因為它包含兩個不需要彼此相關的數據。
但是雖然我們很明顯這個課程是不完整的,但你怎么能得到一個軟件程序來確定不連貫? 怎么會說上面的課是不連貫的,但這不是?
class Customer
{
public string FullName { get; set; }
public Address PostalAddress { get; set; }
}
他們提出的指標肯定會檢測到不連貫,但也會出現誤報。
如果您認為此指標很重要怎么辦? 您可以創建僅包含字段的“CustomerData”類,以及將數據字段公開為屬性的“Customer”類。
// 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
}
但如果我正在玩這個游戲,我也可以將它應用到這個不連貫的例子中:
class Hedgehog_And_AfricanCountry_Data
{
public Hedgehog _hedgehog;
public AfricanNation _africanNation;
}
class Hedgehog_And_AfricanCountry
{
private Hedgehog_And_AfricanCountry_Data _hedgehogAndAfricanCountryData;
// etc;
}
真的,我認為最好理解什么是凝聚力,為什么它是一個有價值的目標,但也要明白軟件工具無法正確測量它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.