Advice (programming)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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.

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. ==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-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 (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 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. ==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 version 19.28, 1994. ==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 "Origin of Advice"

Text derived from https://en.wikipedia.org/w/index.php?oldid=1305515157 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001305515157 · captured 1755000554.0 · version history

Cite this page as: encyclopegia.org, “Advice (programming)”, snapshot enwiki-001305515157. https://encyclopegia.org/a/advice-programming