방법의 응집력이 부족한 이유 (LCOM)는 getters와 setter를 포함합니다.

StackOverflow https://stackoverflow.com/questions/6013032

  •  14-11-2019
  •  | 
  •  

문제

여기에 표시된 것처럼 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; }
}
.

각 getter와 setter는 '다른 모든 인스턴스 필드'에 액세스하지 않기 때문에 0.94의 나쁜 점수를 얻습니다.

이렇게 계산됩니다.

accessAverage - methodCount / 1 - methodCount

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

이 메트릭을 이해하지 못하고, 왜 Getters와 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"클래스를 만들 수 있으며 데이터 필드를 속성으로 노출시키는 "고객"클래스를 생성 할 수 있습니다.

// 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
}
.

그러나이 게임을하는 경우, IT를 불가능한 예제에 적용 할 수 있습니다 :

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top