Jump to content

Action at a distance (computer programming): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Jayden54 (talk | contribs)
m Disambiguation, link to Operation
No edit summary
Tags: Mobile edit Mobile web edit
 
(48 intermediate revisions by 40 users not shown)
Line 1: Line 1:
{{Short description|Anti-pattern}}
{{For|the physics term|Action at a distance (physics)}}
{{Refimprove|date=May 2010}}
{{Use dmy dates|date=August 2022|cs1-dates=y}}
{{Use list-defined references|date=August 2022}}
{{For|the interaction between objects|Action at a distance (physics)}}
'''Action at a distance''' is an [[anti-pattern]] in [[computer science]] in which behavior in one part of a [[computer program|program]] varies wildly based on difficult or impossible to identify [[Instruction (computer science)|operation]]s 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]]s and alters data only in a controlled and [[Subprogram#Local variables, recursion and reentrancy|local]] manner, or usage of a [[pure functional programming]] style with [[referential transparency]].
'''Action at a distance''' is an [[anti-pattern]] (a recognized common error) in which behavior in one part of a [[computer program|program]] varies wildly based on difficult or impossible to identify [[Instruction (computer science)|operation]]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]]s and alters data in a controlled and [[Subprogram#Local variables, recursion and re-entrancy|local]] manner.


The term is based on the [[action at a distance (physics)|physics concept]] of the same name, where particles may be created that cancel each other out. Such particles instantaneously communicate information across space regardless of distance in what [[Albert Einstein]] called "spooky action at a distance".
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]]. In particular, [[Albert Einstein]] referred to [[quantum nonlocality]] as "spooky action at a distance".


[[software bug|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 avoid shared states, can largely eliminate problems caused by action at a distance.
[[Software bug]]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 (computer science)|Side effect]]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==
==Example==
This example, from the [[Perl]] programming language, demonstrates an especially serious case of action at a distance (note the <code><nowiki>$[</nowiki></code> variable was [[Deprecation|deprecated]] in later versions of Perl<ref name="Perl_Var"/>):
From [http://www.perl.com/pub/a/1999/11/sins.html Sins of Perl Revisited] by Mark-Jason Dominus:


{{Quotation|
:''Array indices normally begin at 0 because the value of <code>$[</code> is normally 0; if you set <code>$[</code> to 1, then arrays start at 1, which makes [[Fortran]] programmers happy, and so we see examples like this in the <code>perl3</code> man page:''
[[Array data type|Array]] indices normally begin at 0 because the value of <code><nowiki>$[</nowiki></code> is normally 0; if you set <code><nowiki>$[</nowiki></code> to 1, then arrays start at 1, which makes [[Fortran]] and [[Lua (programming language)|Lua]] programmers happy, and so we see examples like this in the <code>perl(3)</code> [[man page]]:


<syntaxhighlight lang="perl">
<pre>
foreach $num ($<nowiki>[</nowiki> .. $#entry) {
foreach $num ($[ .. $#entry) {
print " $num\t'",$entry[$num],"'\n";
print " $num\t'",$entry[$num],"'\n";
}
}
</syntaxhighlight>
</pre>


:''And of course you could set <code>$[</code> to 17 to have arrays start at some random number such as 17 or 4 instead of at 0 or 1. This was a great way to sabotage module authors.''
And of course you could set <code><nowiki>$[</nowiki></code> to 17 to have arrays start at some random number such as 17 or 4 instead of at 0 or 1. This was a great way to sabotage module authors.


:''Fortunately, sanity prevailed. These features are now recognized to have been mistakes. The perl5-porters mailing list now has a catchphrase for such features: they're called "action at a distance". The principle is that a declaration in one part of the program shouldn't drastically and invisibly alter the behavior of some other part of the program. Some of the old action-at-a-distance features have been reworked into safer versions. For example, In [[Perl]] 5, you are not supposed to use <code>$*</code>. Instead, you put <code>/m</code> on the end of the match operator to say that the meanings of <code>^</code> and <code>$</code> should be changed just for that one regex.''
Fortunately, sanity prevailed. These features are now recognized to have been mistakes. The perl5-porters mailing list now has a catchphrase for such features: they're called "action at a distance". The principle is that a declaration in one part of the program shouldn't drastically and invisibly alter the behavior of some other part of the program.
|[[Mark Jason Dominus]]|Sins of Perl Revisited<ref name="Perl_1999_Sins"/>}}


==Action at a distance across objects==
==Action at a distance across objects==
Proper [[object oriented programming]] involves design principles that avoid action at a distance.
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]], not implementing true objects, or failing to heed the Law of Demeter.
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.


Line 32: Line 39:


==See also==
==See also==
*[[Law of Demeter]]
* [[COMEFROM]]
*[[Object orgy]]
* [[Loose coupling]]
*[[God object]]
* [[State pattern]]
*[[Loose coupling]]
*[[State pattern]]
*[[Accumulate and fire]]
*[[COME FROM]]


==References==
==References==
{{reflist|refs=
*''[[Perl Design Patterns Book]]''
<ref name="Perl_Var">{{cite web |url=https://perldoc.perl.org/5.30.0/perlvar.html |title=Perl documentation of the <code><nowiki>$[</nowiki></code> variable}}</ref>
<ref name="Perl_1999_Sins">{{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}}</ref>
}}


[[Category:Anti-patterns]]
[[Category:Anti-patterns]]

[[it:Azione a distanza (informatica)]]

Latest revision as of 16:56, 13 May 2024

Action at a distance is an anti-pattern in computer 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 variables and alters data only 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, which may refer to a process that allows objects to interact without a mediator particle such as the gluon. In particular, Albert Einstein referred to quantum nonlocality 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 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

[edit]

This example, from the Perl programming language, demonstrates an especially serious case of action at a distance (note the $[ variable was deprecated in later versions of Perl[1]):

Array indices normally begin at 0 because the value of $[ is normally 0; if you set $[ to 1, then arrays start at 1, which makes Fortran and Lua programmers happy, and so we see examples like this in the perl(3) man page:

foreach $num ($[ .. $#entry) {
    print "  $num\t'",$entry[$num],"'\n";
}

And of course you could set $[ to 17 to have arrays start at some random number such as 17 or 4 instead of at 0 or 1. This was a great way to sabotage module authors.

Fortunately, sanity prevailed. These features are now recognized to have been mistakes. The perl5-porters mailing list now has a catchphrase for such features: they're called "action at a distance". The principle is that a declaration in one part of the program shouldn't drastically and invisibly alter the behavior of some other part of the program.

— Mark Jason Dominus, Sins of Perl Revisited[2]

Action at a distance across objects

[edit]

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, 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.

See also

[edit]

References

[edit]
  1. ^ "Perl documentation of the $[ variable".
  2. ^ Dominus, Mark Jason (1999). "Sins of Perl Revisited".