CK Metrics

Research
Introduced by
Shyam Chidamber and Chris Kemerer
Year
1994
Type
Design quality metric suite
Family
Object-Oriented Metrics

What does it do?

CK metrics are a suite of six object-oriented design metrics proposed by Chidamber and Kemerer in 1994.

They measure class-level properties such as method complexity, inheritance depth, coupling to other classes, and cohesion among methods. In a validation study of eight information management systems built in C++, Basili, Briand and Melo (1996) found that several of the metrics were useful predictors of fault-prone classes.

The intuition behind the method

A class that depends on many other classes is harder to change safely, because a modification can propagate to its collaborators. A class whose methods use disjoint sets of attributes may be doing two jobs and could be split. A deep inheritance chain makes a class's behaviour depend on many ancestors. CK metrics assign numbers to these properties so that design problems can be located in the code. The values are signals for review, not proof that a fault exists.

History

Chidamber and Kemerer (1994) proposed the six metrics in response to criticisms that earlier software metrics lacked a theoretical base. They grounded the suite in Bunge's ontology, evaluated it against Weyuker's principles, and tested an automated collection tool at two field sites. Basili, Briand and Melo (1996) empirically validated the suite as predictors of fault-prone classes.

When to use it

Use CK metrics on object-oriented code, where classes, inheritance and object collaboration are the units of design. They suit design review, refactoring prioritisation, and comparison of class designs, particularly before complexity has accumulated across many classes. Do not use them as the main measure for procedural or functional code, where classes and inheritance do not structure the design. Their interpretation is also weaker for designs built largely on mixins or traits, because DIT and NOC assume class-based inheritance.

Limitations and considerations

  • Basili et al. (1996) studied eight medium-sized information management systems built from identical requirements, using C++ and a sequential life cycle. Transfer of the results to other languages or to industrial settings should be checked rather than assumed.
  • Several CK metrics can be correlated with class size, so a high value on one metric may largely reflect another metric or the size of the class.
  • LCOM has more than one definition in the literature, and tools may compute it differently, so values are comparable only within one definition and tool.
  • A deep DIT can reflect a deliberate framework design. The metric alone does not separate intended depth from accidental complexity.
  • The metrics describe structure. They do not measure the correctness of method bodies or the skill of the developers.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • The metrics are defined on class structure (inheritance, attribute use and collaborations), which size-based or procedural counts do not capture.
  • Basili et al. (1996) reported that several CK metrics appeared useful for predicting fault-prone classes early in the life cycle, and that on their data set they were better predictors than traditional code metrics, which can only be collected at a later phase.
  • The suite was built on a stated theoretical basis, Bunge's ontology, and was evaluated analytically against Weyuker's measurement principles (Chidamber and Kemerer, 1994).
  • Each metric points to a concrete design action: a high CBO suggests reducing dependencies, and a high LCOM suggests that a class may combine unrelated responsibilities.

Applications

  • Design review of classes during implementation, using the metrics to flag classes whose structure needs attention.
  • Prioritising classes for testing or inspection, since Basili et al. (1996) reported that several CK metrics appeared useful for predicting fault-prone classes.
  • Refactoring candidates: classes with high LCOM may be split, and classes with high CBO may have their dependencies reduced.
  • Inheritance review: examining deep or wide hierarchies to check whether the depth is intentional.
  • Comparing design alternatives early in the life cycle, since Basili et al. (1996) reported that several CK metrics are useful in early phases, before the implementation is complete.

How it works

How it works

1

Parse the source code and identify, for each class, its methods, attributes, parent classes, and references to other classes. All six metrics are derived from these structural facts, so errors in extraction carry directly into the values.

2

Compute the six metrics for each class. WMC sums the complexity of the class's methods, typically measured per method as cyclomatic complexity; DIT is the length of the path from the class to the root of its inheritance tree; NOC counts its immediate subclasses; CBO counts the other classes it is coupled to through method calls or attribute use; RFC counts the methods that can be executed in response to a message received by an object of the class; LCOM compares method pairs that use disjoint attribute sets with pairs that share an attribute, and is floored at zero.

3

Read the values as design signals rather than verdicts. High WMC, CBO, RFC or LCOM marks classes that may warrant closer review, and a very deep inheritance tree calls for a check that the hierarchy is clear and intended. No universal cut-off values apply, so build a reference point first: compute each metric's distribution across the project or across earlier releases, flag classes in the upper tail for review, and record the tool and the LCOM variant used so that later comparisons remain valid. Check flagged classes against the code before acting on them.

4

Check whether the metrics track outcomes of interest in the system under study, for example by comparing high-value classes with defect reports or change history. Basili et al. (1996) performed this kind of comparison on their own data set, and results from one system or language do not transfer automatically.

Common pitfalls

  • Applying a fixed cut-off as a pass or fail rule. Acceptable ranges depend on the domain and the design style of the system.
  • Pushing LCOM towards zero by splitting classes into very small pieces, which can add coupling and obscure the design.
  • Reading a deep inheritance tree as a defect without checking whether it is an intended framework extension.
  • Treating correlation as causation. A class with high CBO carries more risk factors, but a defect is not guaranteed.

Frequently asked questions

What is WMC (Weighted Methods per Class) and how should I read it?

WMC is the sum of the complexity of the methods in a class. Cyclomatic complexity is a common choice for the per-method measure, so WMC grows with both the number of methods and their individual complexity. A high WMC suggests the class may carry more than one responsibility. A very low value may indicate a trivial class, so check whether its responsibility justifies a separate class. There is no universal cut-off, so compare values with the project's own distribution.

What is CBO (Coupling Between Objects) and how do I reduce it?

CBO is the number of other classes to which a class is coupled, for example through method calls or use of their attributes. A change in any of those classes can affect this class. To reduce coupling, one can introduce interfaces, inject dependencies rather than constructing collaborators directly, or split responsibilities so that each class depends on fewer others. Counting conventions can differ between tools, so compare values only within one tool.

What is LCOM (Lack of Cohesion of Methods) and why does it matter?

In the Chidamber and Kemerer definition, LCOM counts method pairs that use disjoint sets of attributes, subtracts the pairs that share at least one attribute, and floors the result at zero. A value of zero means that the number of method pairs sharing at least one attribute is at least the number of pairs with disjoint attribute sets. A high value means many method pairs work on separate data, which may indicate that the class holds unrelated responsibilities. Some classes with independent methods, such as utility classes, are acceptable, and variants of LCOM exist, so the definition in use should be stated.

Should I follow metric values strictly?

No. The metrics are signals that point to classes worth reviewing. A class with a large WMC that performs one coherent task may be better designed than a smaller class that mixes unrelated responsibilities. Combine the values with code review, test quality and knowledge of the domain.

Related methods

Sources

  1. Chidamber, S. R., & Kemerer, C. F. (1994). A metrics suite for object-oriented design. IEEE Transactions on Software Engineering, 20(6), 476–493.

    Open DOI reference
  2. Basili, V. R., Briand, L. C., & Melo, W. L. (1996). A validation of object-oriented design metrics as quality indicators. IEEE Transactions on Software Engineering, 22(10), 751–761.

    Open DOI reference
  3. Lanza, M., & Marinescu, R. (2007). Object-Oriented Metrics in Practice. Springer.

    Open publication reference