Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Advice (programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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. <code>ispell-word</code> offers no such functionality, even if the corrected word appears frequently in the buffer. The user ''could'' track down the definition of <code>ispell-word</code>, copy it into their personal Emacs files, and add the desired functionality there, but this is tedious and, worse, [[Software brittleness|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 <code>ispell-word</code> runs. Using advice, it can be done as simply as this: <syntaxhighlight lang="elisp"> (advice-add #'ispell-word :after #'flyspell-buffer) </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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)