HOME

TheInfoList



OR:

In aspect and
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 ...
, advice describes a class of functions which modify other functions when the latter are run; it is a certain function, method or procedure that is to be applied at a given join point of a program.


Use

The practical use of advice functions is generally to modify or otherwise extend the behavior of functions which cannot or should not be readily modified or extended. For instance, the Emacspeak
Emacs Emacs (), originally named EMACS (an acronym for "Editor Macros"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, s ...
-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. For a simple Emacs example: suppose after a user corrected a mis-spelled word using the Emacs ispell module, they wanted to re-spellcheck the entire buffer. ispell-word offers no such functionality, even if the corrected word appears frequently in the buffer. The user ''could'' track down the definition of ispell-word, copy it into their personal Emacs files, and add the desired functionality there, but this is tedious and, worse,
brittle A material is brittle if, when subjected to stress, it fractures with little elastic deformation and without significant plastic deformation. Brittle materials absorb relatively little energy prior to fracture, even those of high strength. ...
(the user's version is now out of sync with the core Emacs implementation, if it even works without further refactoring). What the user wants is quite simple — just to run another command any time ispell-word runs. Using advice, it can be done as simply as this: (advice-add #'ispell-word :after #'flyspell-buffer) While this example is obviously trivial, the strength of advice, especially when compared to similar facilities such as 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
evaluation In common usage, evaluation is a systematic determination and assessment of a subject's merit, worth and significance, using criteria governed by a set of Standardization, standards. It can assist an organization, program, design, project or any o ...
throughout the lifetime of a piece of code (cf. code staging) in Lisp allows advice to be 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
spaghettification In astrophysics, spaghettification (sometimes referred to as the noodle effect) is the vertical stretching and horizontal compression of objects into long thin shapes (rather like spaghetti) in a very strong, non- homogeneous gravitational fi ...
attendant to the extensive use of GOTOs. In practice, however, such issues rarely present themselves. 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 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

A form of advices were part of C with Classes in the late 1970s and early 1980s, namely functions called call and return defined in a class, which were called before (respectively, after) member functions of the class. However, these were dropped from C++. Advices are part of the Common Lisp Object System (CLOS), as :before, :after, and :around methods, which are combined with the primary method under "standard method combination". Common Lisp implementations provide advice functionality (in addition to the standard method combination for CLOS) as extensions. LispWorks supports advising functions, macros and CLOS methods. EmacsLisp added advice-related code in versio
19.28
1994.


History

The following is taken from a discussion at the mailing lis

Pascal Costanza contributed the following: The term "advice" goes back to the term ''advising'' as introduced by Warren Teitelman in his PhD thesis in 1966. Here is a quote from Chapter 3 of his thesis: :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 a 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. It also found its way to
Flavors Flavour or flavor is either the sensory perception of taste or smell, or a flavoring in food that produces such perception. Flavour or flavor may also refer to: Science * Flavors (programming language), an early object-oriented extension to L ...
, the first object-oriented extension to
Lisp Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
developed at MIT. They were subsumed under the notion of method combination. Since method combination and macros are closely related, it's also interesting to note that the first macro system was described in 1963, three years before Warren Teitelman's PhD thesis.See AIM-57 at https://web.archive.org/web/20060913001624/http://www.ai.mit.edu/research/publications/browse/0000browse.shtml


See also

* Function decorator (w.r.t. Python) * Aspect-oriented software development#Advice bodies


Notes

Gregor Kiczales comments the above as follows:


References


External links


Teitelman's PhD thesis, PILOT: A Step Toward Man-Computer Symbiosis
(AITR-221)
Interlisp reference manual
from 1974

{{aosd Aspect-oriented programming Lisp (programming language) Programming constructs Articles with example Lisp (programming language) code