Advice in aspect-oriented programming
Encyclopedia
In aspect
and functional programming
, 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.
The following is taken from a discussion at the mailing list aosd-discuss. 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" found its way into BBN Lisp
and later into Xerox PARC
's Interlisp
.
It also found its way to Flavors, the first object-oriented
extension to Lisp developed at MIT. They were subsumed under the notion of method combination. See, for example, AIM-602 at http://www.ai.mit.edu/research/publications/browse/0600browse.shtml 1
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 http://www.ai.mit.edu/research/publications/browse/0000browse.shtml 2
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 obviously 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.
Another Emacs example; suppose after one corrected a misspelled word through ispell
, one wanted to re-spellcheck the entire buffer.
Aspect-oriented programming
In computing, aspect-oriented programming is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns...
and functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...
, 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
Join point
In computer science, a join point is a point in the control flow of a program. In aspect-oriented programming a set of join points is described as a pointcut...
of a program.
The following is taken from a discussion at the mailing list aosd-discuss. Pascal Costanza
Pascal Costanza
Pascal Costanza is a researcher at the Programming Technology Lab, Artificial Intelligence Lab, Vrije Universiteit Brussel. He is known in the field of functional programming in LISP as well as in the aspect-oriented programming community for contributions to this field by applying AOP through...
contributed the following:
The term advice goes back to the term advising as introduced by Warren Teitelman
Warren Teitelman
Warren Teitelman is a computer scientist since 1960 to date, who contributed to and invented many technologies like Interlisp.- Early career and ARPANET :...
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
BBN LISP
BBN LISP was a dialect of the Lisp programming language by Bolt, Beranek and Newman Inc. in Cambridge, Massachusetts. It was based on L. Peter Deutsch's implementation of Lisp for the PDP-1 , which was developed from 1960 to 1964...
and later into Xerox PARC
Xerox PARC
PARC , formerly Xerox PARC, is a research and co-development company in Palo Alto, California, with a distinguished reputation for its contributions to information technology and hardware systems....
's Interlisp
Interlisp
Interlisp was a programming environment built around a version of the Lisp programming language. Interlisp development began in 1967 at Bolt, Beranek and Newman in Cambridge, Massachusetts as BBN LISP, which ran on PDP-10 machines running the TENEX operating system...
.
It also found its way to Flavors, the first object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
extension to Lisp developed at MIT. They were subsumed under the notion of method combination. See, for example, AIM-602 at http://www.ai.mit.edu/research/publications/browse/0600browse.shtml 1
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 http://www.ai.mit.edu/research/publications/browse/0000browse.shtml 2
Use
The practical use of advice functions is generally to modify or otherwise extend the behavior of functions which cannot be easily modified or extended. The EmacspeakEmacspeak
Emacspeak is a free computer application, a speech interface and an audio desktop employing Emacs, which is written in C, Emacs Lisp and Tcl and developed principally by T. V. Raman and first released May 1995; it is portable to all POSIX-compatible OSs...
Emacs
Emacs
Emacs is a class of text editors, usually characterized by their extensibility. GNU Emacs has over 1,000 commands. It also allows the user to combine these commands into macros to automate work.Development began in the mid-1970s and continues actively...
-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 obviously 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.
Another Emacs example; suppose after one corrected a misspelled word through ispell
Ispell
Ispell is a spelling checker for Unix that supports most Western languages. It offers several interfaces, including a programmatic interface for use by editors such as emacs...
, one wanted to re-spellcheck the entire buffer.
ispell-word
offers no such functionality, even if the spellchecked word is used a thousand times. One could track down the definition of ispell-word
, copy it into one's .emacs, and write the additional functionality, but this is tedious, prone to broken-ness (the .emacs version will get out of sync with the actual Ispell Elisp module, if it even works out of its home). What one wants is fairly simple: just to run another command after ispell-word
runs. Using advice functions, it can be done as simply as this:
(defadvice ispell (after advice)
(flyspell-buffer))
(ad-activate 'ispell t)
See also
- Function decorator (w.r.t. PythonPython (programming language)Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
) - Aspect-oriented software development#Advice bodies