为什么方法缺乏内聚 (LCOM) 包括 Getter 和 Setter

发布于 2024-11-07 10:14:55 字数 1007 浏览 5 评论 0原文

我正在查看 LCOM 指标,如下所示,

http://www.ndepend.com/Metrics.aspx< /a>

所以我们要说几件事,

1) 如果类的所有方法都使用其所有实例字段,则该类是完全内聚的
2) 静态方法和实例方法都计算在内,还包括构造函数、属性 getter/setter、事件添加/删除方法

如果我查看这样的类,

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 始终只能访问一个实例字段。

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; }
}

It gets a bad score of 0.94 because each getter and setter doesn't access 'all of the other instance fields'.

It is calculated like this,

accessAverage - methodCount / 1 - methodCount

(2 - 17) / (1 - 17) = 0.94 (rounded)

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.

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

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

发布评论

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

评论(1

≈。彩虹 2024-11-14 10:14:55

这表明,如果你盲目地走向极端,那么每个软件指标都是有缺陷的。

当你看到一个类时,你就知道这是一个“不连贯”的类。例如:

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;
}

真的,我认为最好了解什么是凝聚力,以及为什么它是一个有价值的目标,但也要了解软件工具无法正确衡量它。

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.

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; }
} 

The metric they came up with certainly detects incohesion, but also comes up with false positives.

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.

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