HOME

TheInfoList



OR:

The Law of Demeter (LoD) or principle of least knowledge is a design guideline for developing
software Software is a set of computer programs and associated documentation and data. This is in contrast to hardware, from which the system is built and which actually performs the work. At the lowest programming level, executable code consists ...
, particularly object-oriented programs. In its general form, the LoD is a specific case of
loose coupling In computing and systems design, a loosely coupled system is one # in which components are weakly associated (have breakable relationships) with each other, and thus changes in one component least affect existence or performance of another comp ...
. The guideline was proposed by Ian Holland at Northeastern University towards the end of 1987, and the following three recommendations serve as a succinct summary: * Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. * Each unit should only talk to its friends; don't talk to strangers. * Only talk to your immediate friends. The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents), in accordance with the principle of "
information hiding In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
". It may be viewed as a corollary to the
principle of least privilege In information security, computer science, and other fields, the principle of least privilege (PoLP), also known as the principle of minimal privilege (PoMP) or the principle of least authority (PoLA), requires that in a particular abstraction la ...
, which dictates that a module possess only the information and resources necessary for its legitimate purpose. It is so named for its origin in the Demeter Project, an adaptive programming and
aspect-oriented programming In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying t ...
effort. The project was named in honor of
Demeter In ancient Greek religion and mythology, Demeter (; Attic: ''Dēmḗtēr'' ; Doric: ''Dāmā́tēr'') is the Olympian goddess of the harvest and agriculture, presiding over crops, grains, food, and the fertility of the earth. Although s ...
, “distribution-mother” and the Greek goddess of
agriculture Agriculture or farming is the practice of cultivating plants and livestock. Agriculture was the key development in the rise of sedentary human civilization, whereby farming of domesticated species created food surpluses that enabled people to ...
, to signify a bottom-up philosophy of programming which is also embodied in the law itself.


History

The law dates back to 1987 when it was first proposed by Ian Holland, who was working on the Demeter Project. The Demeter Project was the birthplace of a lot of AOP (Aspect Oriented Programming) principles. A quote in one of the remainders of the project seems to clarify the origins of the name:


In object-oriented programming

An object a can request a service (call a method) of an object instance b, but object a should not "reach through" object b to access yet another object, c, to request its services. Doing so would mean that object a implicitly requires greater knowledge of object b's internal structure. Instead, b's interface should be modified if necessary so it can directly serve object a's request, propagating it to any relevant subcomponents. Alternatively, a might have a direct reference to object c and make the request directly to that. If the law is followed, only object b knows its own internal structure. More formally, the Law of Demeter for functions requires that a method m of an object a may only invoke the methods of the following kinds of objects: * a itself; * m's parameters; * any objects instantiated within m; * a's attributes; * global variables accessible by a in the scope of m. In particular, an object should avoid invoking methods of an object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code a.m().n() breaks the law where a.m() does not. As an
analogy Analogy (from Greek ''analogia'', "proportion", from ''ana-'' "upon, according to" lso "against", "anew"+ ''logos'' "ratio" lso "word, speech, reckoning" is a cognitive process of transferring information or meaning from a particular subject ( ...
, when one wants a dog to walk, one does not command the dog's legs to walk directly; instead one commands the dog which then commands its own legs.


Advantages

The advantage of following the Law of Demeter is that the resulting software tends to be more maintainable and adaptable. Since objects are less dependent on the internal structure of other objects, object implementation can be changed without reworking their callers. Basili et al. published experimental results in 1996 suggesting that a lower ''Response For a Class'' (RFC, the number of methods potentially invoked in response to calling a method of that class) can reduce the probability of
software bug A software bug is an error, flaw or fault in the design, development, or operation of computer software that causes it to produce an incorrect or unexpected result, or to behave in unintended ways. The process of finding and correcting bugs i ...
s. Following the Law of Demeter can result in a lower RFC. However, the results also suggest that an increase in ''Weighted Methods per Class'' (WMC, the number of methods defined in each class) can increase the probability of software bugs. Following the Law of Demeter can also result in a higher WMC. A
multilayered architecture In software engineering, multitier architecture (often referred to as ''n''-tier architecture) is a client–server architecture in which presentation, application processing and data management functions are physically separated. The most wide ...
can be considered to be a systematic mechanism for implementing the Law of Demeter in a software system. In a layered architecture, code within each
layer Layer or layered may refer to: Arts, entertainment, and media * ''Layers'' (Kungs album) * ''Layers'' (Les McCann album) * ''Layers'' (Royce da 5'9" album) *"Layers", the title track of Royce da 5'9"'s sixth studio album * Layer, a female Maveri ...
can only make calls to code within the layer and code within the next layer down. "Layer skipping" would violate the layered architecture.


Disadvantages

Although the LoD increases the adaptiveness of a software system, it may result in having to write many
wrapper method A wrapper function is a function (another word for a ''subroutine'') in a software library or a computer program whose main purpose is to call a second subroutine or a system call with little or no additional computation. Wrapper functions are us ...
s to propagate calls to components; in some cases, this can add noticeable time and space overhead. At the method level, the LoD leads to narrow interfaces, giving access to only as much information as it needs to do its job, as each method needs to know about a small set of methods of closely related objects. On the other hand, at the class level, if the LoD is not used correctly, wide (i.e. enlarged) interfaces may be developed that require introducing many auxiliary methods. This is due to poor design rather than a consequence of the LoD per se. If a wrapper method is being used, it means that the object being called through the wrapper should have been a dependency in the calling class. One proposed solution to the problem of enlarged class interfaces is the aspect-oriented approach, where the behavior of the method is specified as an aspect at a high level of abstraction. The wide interfaces are managed through a language that specifies implementations. Both the traversal strategy and the adaptive visitor use only a minimal set of classes that participate in the operation, and the information about the connections between these classes is abstracted out.


See also

*
Single-responsibility principle The single-responsibility principle (SRP) is a computer programming principle that states that "A module should be responsible to one, and only one, actor." The term actor refers to a group (consisting of one or more stakeholders or users) that ...
*
Principle of least astonishment The principle of least astonishment (POLA), aka principle of least surprise (alternatively a law or rule), applies to user interface and software design. It proposes that a component of a system should behave in a way that most users will expect it ...
*
Facade pattern The facade pattern (also spelled ''façade'') is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex und ...


References


Further reading

* * * * (from this book, "Law of Demeter" is also known as "Don't talk to strangers") * * {{refend


External links


Law of Demeter (LoD)"Object-Oriented Programming: An Objective Sense of Style" (OOPSLA '88 Proceedings) (PDF)The Paperboy, The Wallet,and The Law Of Demeter (PDF)Phil Haack: "The Law of Demeter is not a Dot Counting Exercise"
Object-oriented programming Programming principles