3

I been reading a blog post from Chris Wanstrath from 2010 where he write about coffee-mode. He proposed there this functions hook:

(defun coffee-custom ()
  "coffee-mode-hook"

  ;; Emacs key binding
  (define-key coffee-mode-map [(meta r)] 'coffee-compile-buffer))

(add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))

In coffee-mode.el you can see similar hook call

"... Add `'(lambda () (coffee-cos-mode t))' to `coffee-mode-hook' to turn
it on by default."

I don't understand the use of lambda calling another function in order to execute it. Why not simply:

(add-hook 'coffee-mode-hook 'my:coffee-custom)

Resources: http://ozmm.org/posts/coffee_mode.html

5

(add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))

Definitely not recommended.

  1. Quoting lambdas like that is not good practice.
  2. As you say, (add-hook 'coffee-mode-hook 'coffee-custom) would be better (for many reasons).

In coffee-mode.el you can see similar hook call

"... Add `'(lambda () (coffee-cos-mode t))' to `coffee-mode-hook'

This one at least had a purpose, as it's passing an argument; however:

  1. Again with the quoted lambda.
  2. I'd still recommend using a named function instead of a lambda, even if you have to define it yourself.
  3. If coffee-cos-mode is a minor mode defined with the standard macro then the argument will be redundant in this scenario (since Emacs 24.1), in which case you could just add 'coffee-cos-mode to the hook, in the same fashion as the previous scenario.
  • You mean it is no a good idea because that cause redundant function call or is there some other reason? – siery Jun 30 '18 at 13:28
  • Having function code (possibly in byte-compiled form) in a hook variable is just messy. Inspecting the variable becomes harder to understand; removing the function from the hook becomes harder; updating the function in the hook becomes harder. With a named function symbol, all those things are nice and easy. – phils Jun 30 '18 at 13:40
  • n.b. One of my links is ostensibly about key bindings, but relevant here due to the issue of the (possibly-compiled) function code being just as hard to read in a hook value as it is in describe-key output. – phils Jun 30 '18 at 13:43
  • Yes, readability was my drive as well. Thank you! – siery Jun 30 '18 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.