action at distance (computer science)
   HOME

TheInfoList



OR:

Action at a distance is an anti-pattern 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 Applied science, practical discipli ...
in which behavior in one part of a
program Program, programme, programmer, or programming may refer to: Business and management * Program management, the process of managing several related projects * Time management * Program, a part of planning Arts and entertainment Audio * Progra ...
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-Ma ...
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 variables and alters data only in a controlled and local 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) withou ...
. The term is based on the concept of action at a distance 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 bind q ...
. 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 theory ...
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 experimen ...
as "spooky action at a distance".
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 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
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
programming language, demonstrates an especially serious case of action at a distance (note the $ ):


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 design ...
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 declar ...
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 * Loose coupling * State pattern


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