Why Does Lack of Cohesion Of Methods (LCOM) Include Getters and Setters

后端 未结 1 634
一生所求
一生所求 2021-02-04 02:54

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 utte         


        
相关标签:
1条回答
  • 2021-02-04 03:13

    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.

    0 讨论(0)
提交回复
热议问题