Action at a distance (computer science)
Encyclopedia
Action at a distance is an anti-pattern
(a recognized common error) in which behavior in one part of a program
varies wildly based on difficult or impossible to identify operations in another part of the program.
The way to avoid the problems associated with action at a distance are a proper design which avoids global variable
s and alters data in a controlled and local manner, or usage of a pure
functional programming
style with referential transparency.
The term is based on the concept of action at a distance in physics
, referring to a process that allows objects to interact instantaneously without a mediator like a photon
. In particular, Albert Einstein
referred to effects in quantum mechanics
as "spooky action at a distance".
Bugs
due to "action at a distance" may arise because a program component is doing something at the wrong time, or affecting something it should not. It is very difficult, however, to track down which component is responsible. Side effects from innocent actions can put the program in an unknown state, so local data is not necessarily local. The solution in this particular scenario is to define which components should be interacting with which others. A proper design that accurately defines the interface between parts of a program, and that avoids shared states, can largely eliminate problems caused by action at a distance.
programming language, demonstrates an especially serious case of action at a distance:
The Law of Demeter
states that an object should only interact with other objects near itself. Should action in a distant part of the system be required then it should be implemented by propagating a message. Proper design severely limits occurrences of action at a distance, contributing to maintainable programs. Pressure to create an object orgy
results from poor interface design, perhaps taking the form of a God object
, not implementing true objects, or failing to heed the Law of Demeter.
One of the advantages of functional programming
is that action at a distance is de-emphasised, sometimes to the point of being impossible to express at all in the source language.
Being aware of the danger of allowing action at a distance into a design, and being able to recognize the presence of action at a distance, is useful in developing programs that are correct, reliable and maintainable. Given that the majority of the expense of a program may be in the maintenance phase, and that action at a distance makes maintenance difficult, expensive and error prone, it is worth effort during design to avoid.
Anti-pattern
In software engineering, an anti-pattern is a pattern that may be commonly used but is ineffective and/or counterproductive in practice.The term was coined in 1995 by Andrew Koenig,...
(a recognized common error) in which behavior in one part of a program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
varies wildly based on difficult or impossible to identify operations in another part of the program.
The way to avoid the problems associated with action at a distance are a proper design which avoids global variable
Global variable
In computer programming, a global variable is a variable that is accessible in every scope . Interaction mechanisms with global variables are called global environment mechanisms...
s and alters data in a controlled and local manner, or usage of a pure
Purely functional
Purely functional is a term in computing used to describe algorithms, data structures or programming languages that exclude destructive modifications...
functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...
style with referential transparency.
The term is based on the concept of action at a distance in physics
Action at a distance (physics)
In physics, action at a distance is the interaction of two objects which are separated in space with no known mediator of the interaction. This term was used most often in the context of early theories of gravity and electromagnetism to describe how an object responds to the influence of distant...
, referring to a process that allows objects to interact instantaneously without a mediator like a photon
Photon
In physics, a photon is an elementary particle, the quantum of the electromagnetic interaction and the basic unit of light and all other forms of electromagnetic radiation. It is also the force carrier for the electromagnetic force...
. In particular, Albert Einstein
Albert Einstein
Albert Einstein was a German-born theoretical physicist who developed the theory of general relativity, effecting a revolution in physics. For this achievement, Einstein is often regarded as the father of modern physics and one of the most prolific intellects in human history...
referred to effects in quantum mechanics
Quantum mechanics
Quantum mechanics, also known as quantum physics or quantum theory, is a branch of physics providing a mathematical description of much of the dual particle-like and wave-like behavior and interactions of energy and matter. It departs from classical mechanics primarily at the atomic and subatomic...
as "spooky action at a distance".
Bugs
Software bug
A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program's...
due to "action at a distance" may arise because a program component is doing something at the wrong time, or affecting something it should not. It is very difficult, however, to track down which component is responsible. Side effects from innocent actions can put the program in an unknown state, so local data is not necessarily local. The solution in this particular scenario is to define which components should be interacting with which others. A proper design that accurately defines the interface between parts of a program, and that avoids shared states, can largely eliminate problems caused by action at a distance.
Example
This example, from the PerlPerl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
programming language, demonstrates an especially serious case of action at a distance:
Action at a distance across objects
Proper object oriented programming involves design principles that avoid action at a distance.The Law of Demeter
Law of Demeter
The Law of Demeter or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling...
states that an object should only interact with other objects near itself. Should action in a distant part of the system be required then it should be implemented by propagating a message. Proper design severely limits occurrences of action at a distance, contributing to maintainable programs. Pressure to create an object orgy
Object orgy
In computer programming, object orgy is a term, common in the Perl programming community, describing a common failure in object-oriented design or programming...
results from poor interface design, perhaps taking the form of a God object
God object
In object-oriented programming, a God object is an object that knows too much or does too much. The God object is an example of an anti-pattern....
, not implementing true objects, or failing to heed the Law of Demeter.
One of the advantages of functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...
is that action at a distance is de-emphasised, sometimes to the point of being impossible to express at all in the source language.
Being aware of the danger of allowing action at a distance into a design, and being able to recognize the presence of action at a distance, is useful in developing programs that are correct, reliable and maintainable. Given that the majority of the expense of a program may be in the maintenance phase, and that action at a distance makes maintenance difficult, expensive and error prone, it is worth effort during design to avoid.
See also
- Law of DemeterLaw of DemeterThe Law of Demeter or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling...
- Object orgyObject orgyIn computer programming, object orgy is a term, common in the Perl programming community, describing a common failure in object-oriented design or programming...
- God objectGod objectIn object-oriented programming, a God object is an object that knows too much or does too much. The God object is an example of an anti-pattern....
- Loose couplingLoose couplingIn computing and systems design a loosely coupled system is one where each of its components has, or makes use of, little or no knowledge of the definitions of other separate components. The notion was introduced into organizational studies by Karl Weick...
- State patternState patternThe state pattern, which closely resembles Strategy Pattern, is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to represent the state of an object. This is a clean way for an object to partially change its type at...
- COME FROMCOMEFROMIn computer programming, COMEFROM is an obscure control flow structure used in some programming languages, originally as a joke....