Advice (programming): Difference between revisions

Content deleted Content added
 
(15 intermediate revisions by 10 users not shown)
Line 2:
 
==Use==
The practical use of advice functions is generally to modify or otherwise extend the behavior of functions which cannot or should not be easilyreadily modified or extended. TheFor instance, the [[Emacspeak]] [[Emacs]]-addon makes extensive use of advice: it must modify thousands of existing Emacs modules and functions such that it can produce audio output for the blind corresponding to the visual presentation, but it would be infeasible to copy all of them and redefine them to produce audio output in addition to their normal outputs; so, the Emacspeak programmers define advice functions which run before and after.
 
AnotherFor a simple Emacs example;: suppose after onea user corrected a misspelledmis-spelled word throughusing the Emacs [[ispell]] module, onethey wanted to re-spellcheck the entire buffer. <code>ispell-word</code> offers no such functionality, even if the spellcheckedcorrected word isappears usedfrequently ain thousandthe timesbuffer. OneThe user ''could'' track down the definition of <code>ispell-word</code>, copy it into one'stheir personal Emacs files, and writeadd the additionaldesired functionality there, but this is tedious and, proneworse, to[[Software broken-nessbrittleness|brittle]] (the Emacsuser's version willis getnow out of sync with the actualcore Ispell ElispEmacs moduleimplementation, if it even works out ofwithout itsfurther homerefactoring). What onethe user wants is fairlyquite simple: just to run another command afterany time <code>ispell-word</code> runs. Using advice functions, it can be done as simply as this:
 
<syntaxhighlight lang="elisp">
(defadviceadvice-add #'ispell (after advice)-word
:after (#'flyspell-buffer))
(ad-activate 'ispell t)
</syntaxhighlight>
 
While this example is obviously trivial, the strength of advice, especially when compared to similar facilities such as [[Python_syntax_and_semantics#Decorators|Python decorators]] and [[Java annotations]], lies in the fact that not only do the advised functions / methods not need to be designed to accept advice, but also the advice themselves need not be designed to be usable as advice - they're just normal functions. The availability of [[eval|evaluation]] throughout the lifetime of a piece of code (cf. [[Multi-stage programming|code staging]]) in Lisp allows advice to be [[Inline expansion|inlined]] automatically into any other code in a variety of ways. Any piece of code can be advised to carry out any other computation before, after, around, or instead of its original definition.
 
=== Readability ===
Advice has the potential to introduce confusion, as a piece of advice applied to a function is not apparent to a user who tracks down the function's source definition to learn about it. In such cases, advice acts almost like a [[COMEFROM]], a joke facility added to [[INTERCAL]] to spoof the [[Spaghetti code|spaghettification]] attendant to the extensive use of [[GOTO]]s. In practice, however, such issues rarely present themselves. [[Upstream (software development)|Upstream]] developers and maintainers of Lisp packages and modules never use advice, since there is no advantage to be gained by advising functions when their original source definitions can be freely rewritten to include the desired features. Advice is only useful in that it enables [[Downstream (software development)|downstream]] users to subsequently modify default behaviour in a way that does not require propagation of such modifications into the core implementation's source definition.
 
==Implementations==
Line 27 ⟶ 31:
 
:Advising is the basic innovation in the model, and in the PILOT system. Advising consists of inserting new procedures at any or all of the entry or exit points to a particular procedure (or class of procedures). The procedures inserted are called "advice procedures" or simply "advice".
:
 
:Since each piece of advice is itself a procedure, it has its own entries and exits. In particular, this means that the execution of advice can cause the procedure that it modifies to be bypassed completely, e.g., by specifying as an exit from the advice one of the exits from the original procedure; or the advice may change essential variables and continue with the computation so that the original procedure is executed, but with modified variables. Finally, the advice may not alter the execution or affect the original procedure at all, e.g., it may merely perform some additional computation such as printing a message or recording history. Since advice can be conditional, the decision as to what is to be done can depend on the results of the computation up to that point.
:
 
:The principal advantage of advising is that the user need not be concerned about the details of the actual changes in his program, nor the internal representation of advice. He can treat the procedure to be advised _as''as a unit_unit'', a single block, and make changes to it without concern for the particulars of this block. This may be contrasted with editing in which the programmer must be cognizant of the internal structure of the procedure.
 
"Advising" found its way into [[BBN Lisp]] and later into [[Xerox PARC]]'s [[Interlisp]].
Line 53 ⟶ 57:
 
==External links==
* [http://www.ai.mit.edu/research/publications/browse/0200browse.shtml Teitelman's PhD thesis, PILOT: A Step Toward Man-Computer Symbiosis] (AITR-221)
* [http://bitsavers.informatik.uni-stuttgart.de/pdf/xerox/interlisp/Interlisp_Reference_Manual_1974.pdf Interlisp reference manual] from 1974
* [httphttps://p-cos.blogspot.com/2007/12/origin-of-advice.html "Origin of Advice"]
 
{{aosd}}