bbatsov / emacs-lisp-style-guide

A community-driven Emacs Lisp style guide
1.08k stars 53 forks source link

Proposal: Don't use anonymous functions as hooks #14

Closed shosti closed 10 years ago

shosti commented 10 years ago

i.e. instead of this:

(add-hook 'foo-mode-hook (lambda () (do-something 10)))

use this:

(defun my-foo-mode-hook ()
  (do-something 10))

(add-hook 'foo-mode-hook 'my-foo-mode-hook)

(These probably aren't great name examples). The advantage of the latter is that if the hook function is changed everything will work out, whereas re-doing add-hook with a changed anonymous function will result in both the new and old anonymous functions being run.

bbatsov commented 10 years ago

Definitely. You can add a whole section on hooks. Btw, functions used in hooks shouldn't have hook in their names as this is pretty confusing.