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, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
in which behavior in one part of a
program varies wildly based on difficult or impossible to identify
operation
Operation or Operations may refer to:
Arts, entertainment and media
* ''Operation'' (game), a battery-operated board game that challenges dexterity
* Operation (music), a term used in musical set theory
* ''Operations'' (magazine), Multi-Man ...
s 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
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
* Local government, a form of public administration, usually the lowest tier of administrat ...
manner, or usage of a
pure functional programming style with
referential transparency
In computer science, referential transparency and referential opacity are properties of parts of computer programs. An expression is called ''referentially transparent'' if it can be replaced with its corresponding value (and vice-versa) witho ...
.
The term is based on the concept of
action at a distance
In physics, action at a distance is the concept that an object can be affected without being physically touched (as in mechanical contact) by another object. That is, it is the non-local interaction of objects that are separated in space.
Non- ...
in physics, which may refer to a process that allows objects to interact without a mediator particle such as the
gluon
A gluon ( ) is an elementary particle that acts as the exchange particle (or gauge boson) for the strong force between quarks. It is analogous to the exchange of photons in the electromagnetic force between two charged particles. Gluons bi ...
. In particular,
Albert Einstein
Albert Einstein ( ; ; 14 March 1879 – 18 April 1955) was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time. Einstein is best known for developing the theor ...
referred to
quantum nonlocality
In theoretical physics, quantum nonlocality refers to the phenomenon by which the measurement statistics of a multipartite quantum system do not admit an interpretation in terms of a local realistic theory. Quantum nonlocality has been experime ...
as "spooky action at a distance".
Software 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 effect
In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequence ...
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 family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
programming language, demonstrates an especially serious case of action at a distance
(note the
$
variable was deprecated in later versions of Perl
):
Action at a distance across objects
Proper object-oriented programming">/nowiki> variable was deprecated 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
In computer programming, an object orgy is a situation in which objects are insufficiently encapsulated via information hiding, allowing unrestricted access to their internals. This is a common failure (or anti-pattern) in object-oriented des ...
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 applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
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 code ...
*
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 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 interpreted as a strateg ...
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