Action at a distance is an
anti-pattern
An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
in
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
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 is a proper design, which avoids
global variable
In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global ...
s and alters data only in a controlled and
local
Local may refer to:
Geography and transportation
* Local (train), a train serving local traffic demand
* Local, Missouri, a community in the United States
Arts, entertainment, and media
* ''Local'' (comics), a limited series comic book by Bria ...
manner, or usage of a
pure functional programming style with
referential transparency
In analytic philosophy and computer science, referential transparency and referential opacity are properties of linguistic constructions, and by extension of languages. A linguistic construction is called ''referentially transparent'' when for an ...
.
The term is based on the concept of
action at a distance
Action at a distance is the concept in physics that an object's motion (physics), motion can be affected by another object without the two being in Contact mechanics, physical contact; that is, it is the concept of the non-local interaction of ob ...
in physics, which may refer to a process that allows objects to interact without a mediator particle such as the
gluon
A gluon ( ) is a type of Massless particle, massless elementary particle that mediates the strong interaction between quarks, acting as the exchange particle for the interaction. Gluons are massless vector bosons, thereby having a Spin (physi ...
. In particular,
Albert Einstein
Albert Einstein (14 March 187918 April 1955) was a German-born theoretical physicist who is best known for developing the theory of relativity. Einstein also made important contributions to quantum mechanics. His mass–energy equivalence f ...
referred to
quantum nonlocality as "spooky action at a distance".
Software bug
A software bug is a design defect ( bug) in computer software. A computer program with many or serious bugs may be described as ''buggy''.
The effects of a software bug range from minor (such as a misspelled word in the user interface) to sev ...
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 effect
In medicine, a side effect is an effect of the use of a medicinal drug or other treatment, usually adverse but sometimes beneficial, that is unintended. Herbal and traditional medicines also have side effects.
A drug or procedure usually use ...
s 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
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
programming language, demonstrates an especially serious case of action at a distance (note the
$ variable was Deprecation">deprecated
Deprecation is the discouragement of use of something human-made, such as a term, feature, design, or practice. Typically something is deprecated because it is claimed to be inferior compared to other options available.
Something may be deprec ...
in later versions of Perl):
Action at a distance across objects
Proper object-oriented programming involves design principles that avoid 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
In object-oriented programming, a god object (sometimes also called an omniscient or all-knowing object) is an object that references a large number of distinct types, has too many unrelated or uncategorized methods, or some combination of both ...
, not implementing true objects, or failing to heed the Law of Demeter.
One of the advantages of functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
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
* COMEFROM
In computer programming, COMEFROM (or COME FROM) is an obscure control flow structure used in some programming languages, originally as a joke. COMEFROM is the inverse of GOTO in that it can take the execution state from any arbitrary point in cod ...
* 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 ...
* State pattern
The state pattern is a Behavioral pattern, behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines. The state pattern can be inter ...
References
{{reflist, refs=
[{{cite web , url=https://perldoc.perl.org/5.30.0/perlvar.html , title=Perl documentation of the ]$[
variable
[{{cite web , url=http://www.perl.com/pub/a/1999/11/sins.html , title=Sins of Perl Revisited , author-first=Mark Jason , author-last=Dominus , author-link=Mark Jason Dominus , date=1999]
Anti-patterns