Violations of Demeters Law (VOD)

Description:

Law of Demeter

Definition 1 (Client) Method M is a client of method f attached to class C, if inside M message f is sent to an object of class C, or to C. If f is specialized in one or more subclasses, then M is only a client of f attached to the highest class in the hierarchy. Method M is a client of some method attached to C.

Definition 2 (Supplier) If M is a client of class C then C is a supplier to M. In other words, a supplier class to a method is a class whose methods are called in the method.

Definition 3 (Acquaintance Class) A class C1 is an acquaintance class of method M attached to class C2, if C1 is a supplier to M and C1 is not one of the following:

  1. the same as C2;
  2. a class used in the declaration of an argument of M
  3. a class used in the declaration of an instance variable of C2

Definition 4 (Preferred-acquaintance Class) A preferred-acquaintance class of method M is either:

  1. a class of objects created directly in M, or
  2. a class used in the declaration of a global variable used in M.

Realization note. Direct creation means that a given object is created via operator new.

Definition 5 (Preferred-supplier class) Class B is called a preferred-supplier to method M (attached to class C) if B is a supplier to M and one of the following conditions holds:

  1. B is used in the declaration of an instance variable of C,
  2. B is used in the declaration of an argument of M, including C and its superclasses,
  3. B is a preferred acquaintance class of M.

The relation among the different types of supplier classes.

The class form of Demeters Law has two versions: a strict version and a minimization version. The strict form of the law states that every supplier class of a method must be a preferred supplier.

The minimization form is more permissive than the first version and requires only to minimize the number of acquaintance classes of each method.

Observations.

  1. The motivation behind the Law of Demeter is to ensure that the software is as modular as possible. The Law effectively reduces the occurrences of certain nested message sends and simplifies the methods.
  2. The definition of the Law makes a difference between the classes associated with the declaration of the method and the classes used in the body of the method, i.e. the classes associated with its implementation. The former includes the class where the method is attached, its superclasses, the classes used in the declarations of the instance variables and the classes used to declare the arguments of the method. In some sense, there are 'automatic' consequences of the method declaration. They can be easily derived from the code and shown by a browser. All other supplier classes to the methods are introduced in the body of the function, that means these couples were created at the time of concretely implementing the method. They can only be determined by a careful reading of the implementation.

Violations of Demeters Law - VOD

The definition of this metric is based on the minimization form of the Law of Demeter.

Based on the concepts defined there, and remembering that the minimization form of Demeters Law requires that the number of acquaintance classes should be kept low, we define the VOD metric.

Definition 6 (VOD Metric) Being given a class C and A the set of all its acquaintance classes,

VOD(C) = |A|

Informally, VOD is the number of acquaintance classes of a given class.

Keeping the VOD value for a class low offers a number of benefits, enumerated below:

  1. Coupling control. A project with a low VOD value is the sign of minimal "use" coupling between abstractions. That means that a reduced number of methods can be invoked. This makes the methods more reusable.
  2. Structure hiding. Reducing VOD represents in fact the reducing of the direct retrieval of subparts of the "part-of" hierarchy. In other words, public members should be used in a restricted way.
  3. Localization of information. A low VOD value also means that the class information is localized. This reduces the programming complexity.

Source: Ing. Radu Marinescu. An Object Oriented Metrics Suite on Coupling. Universitatea "Politehnica" Timisoara, Facultatea de Automatica si Calculatoare, Departamentul de Calculatoare si Inginerie Software. September, 1998.