A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/bbatsov/emacs-lisp-style-guide/ below:

bbatsov/emacs-lisp-style-guide: A community-driven Emacs Lisp style guide

The Emacs Lisp Style Guide

Role models are important.

– Officer Alex J. Murphy / RoboCop

This Emacs Lisp style guide recommends best practices so that real-world Emacs Lisp programmers can write code that can be maintained by other real-world Emacs Lisp programmers. A style guide that reflects real-world usage gets used, and a style guide that holds to an ideal that has been rejected by the people it is supposed to help risks not getting used at all — no matter how good it is.

The guide is separated into several sections of related rules. I’ve tried to add the rationale behind the rules (if it’s omitted, I’ve assumed that it’s pretty obvious).

I didn’t come up with all the rules out of nowhere; they are mostly based on my extensive experience of using Emacs and creating/maintaining Emacs packages, feedback and suggestions from members of the Emacs Lisp community, and various highly regarded Emacs Lisp programming resources, such as “GNU Emacs Lisp Reference Manual”.

The guide is still a work in progress; some sections are missing, others are incomplete, some rules are lacking examples, some rules don’t have examples that illustrate them clearly enough. In due time these issues will be addressed — just keep them in mind for now.

Please note, that the Emacs developers maintain a list of coding conventions and tips too.

You can generate a PDF or an HTML copy of this guide using Pandoc.

Source Code Layout & Organization

Nearly everybody is convinced that every style but their own is ugly and unreadable. Leave out the “but their own” and they’re probably right…

– Jerry Coffin (on indentation)

In practical terms this means you should add the following to your Emacs config:

(setq-default indent-tabs-mode nil)

An even better idea would be to force the use of spaces using .dir-locals.el in each of your Emacs Lisp projects.

((emacs-lisp-mode
  (indent-tabs-mode nil)))
;; good
(format "%s %d"
        something
        something-else)

;; bad
(format "%s %d"
  something
  something-else)
;; good
(format
 "%s %d"
 something
 something-else)

;; bad
(format
  "%s %d"
  something
  something-else)
;; good
(if something
    if-clause
  (something)
  (something-else))

;; bad
(if something
    if-clause
  (progn
    (something)
    (something-else)))
;; good
(when pred
  (foo)
  (bar))

;; bad
(if pred
  (progn
    (foo)
    (bar)))
;; good
(unless pred
  (foo)
  (bar))

;; bad
(when (not pred)
  (foo)
  (bar))
;; good
(if (null lst) ...)

(if (or (not foo) something) ...)

;; bad
(if (not lst))

(if (and (null foo) bar) ...)
;; Preferred
(< 5 x 10)

;; Old
(and (> x 5) (< x 10))
;; good
(cond
  ((< n 0) "negative")
  ((> n 0) "positive")
  (t "zero"))

;; bad
(cond
  ((< n 0) "negative")
  ((> n 0) "positive")
  (:else "zero"))
;; good
(with-eval-after-load "foo"
  (bar)
  (baz))


;; bad
(eval-after-load "foo"
  '(progn
     (bar)
     (baz)))

The only real difficulties in programming are cache invalidation and naming things.

– Phil Karlton

;; good
(defvar some-var ...)
(defun some-fun ...)

;; bad
(defvar someVar ...)
(defun somefun ...)
(defvar some_fun ...)
;; good
(defun projectile-project-root ...)

;; bad
(defun project-root ...)
;; good
(lambda (x _y) x)

;; bad
(lambda (x y) x)
;; good
(defun palindromep ...)
(defun only-one-p ...)

;; bad
(defun palindrome? ...) ; Scheme style
(defun is-palindrome ...) ; Java style
;; good
(defface widget-inactive ...)

;; bad
(defface widget-inactive-face ...)
;;; Good
(mapcar (lambda (x) (or (car x) "")) some-list)
(let ((predicate (lambda (x) (and (numberp x) (evenp x)))))
  (funcall predicate 1000))

;;; Bad - Define real functions for these.
(defcustom my-predicate (lambda (x) (and (numberp x) (evenp x)))
  ...)
(define-key my-keymap (kbd "C-f")
  (lambda () (interactive) (forward-char 1)))
(add-hook 'my-hook (lambda () (save-some-buffers)))
;;; Good
(lambda (x) (car x))

;;; Ok, but redundant.
#'(lambda (x) (car x))

;;; Bad
'(lambda (x) (car x))
;; good
(cl-remove-if-not #'evenp numbers)

;; bad
(cl-remove-if-not (lambda (x) (evenp x)) numbers)
;; good
(cl-remove-if-not #'evenp numbers)
(global-set-key (kbd "C-l C-l") #'redraw-display)
(cl-labels ((butterfly () (message "42")))
  (funcall #'butterfly))

;; bad
(cl-remove-if-not 'evenp numbers)
(global-set-key (kbd "C-l C-l") 'redraw-display)
(cl-labels ((butterfly () (message "42")))
  (funcall 'butterfly))
(defmacro define-widget (name &rest forms)
  "Description"
  (declare (debug (sexp body))
           (indent defun))
  ...)
(provide 'foo)

;;; foo.el ends here
;;; good
;;;###autoload
(define-derived-mode foo-mode ...)

;;;###autoload
(define-minor-mode foo-minor-mode ...)

;;;###autoload
(defun foo-setup () ...)

;;; bad
;;;###autoload
(defun foo--internal () ...)

;;;###autoload
(defvar foo-option)
;;; good
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.foo\\'" . foo-mode))

;;; bad
;;;###autoload
(foo-setup)
;;; good
(dolist (hook '(prog-mode-hook text-mode-hook))
  (add-hook hook 'turn-on-column-number-mode)
  (add-hook hook 'turn-off-line-number-mode)
  (add-hook hook 'linum-mode))

;;; bad
(add-hook 'prog-mode-hook 'turn-on-column-number-mode)
(add-hook 'prog-mode-hook 'turn-off-line-number-mode)
(add-hook 'prog-mode-hook 'linum-mode)
(add-hook 'text-mode-hook 'turn-on-column-number-mode)
(add-hook 'text-mode-hook 'turn-off-line-number-mode)
(add-hook 'text-mode-hook 'linum-mode)
;;; good
(font-lock-add-keywords nil (mapcar 'downcase list-of-crazy-cased-words))
(seq-do 'load list-of-files-to-load)

;;; bad
(mapcar 'load list-of-files-to-load)
;;; good
(dolist (map (list c-mode-map c++-mode-map))
  (define-key map "\C-c\C-c" 'compile))

;;; bad
(mapc
  (lambda (map) (define-key map "\C-c\C-c" 'compile))
  (list c-mode-map c++-mode-map))

Good code is its own best documentation. As you’re about to add a comment, ask yourself, “How can I improve the code so that this comment isn’t needed?” Improve the code and then document it to make it even clearer. – Steve McConnell

;;; Frob Grovel
;; This is where Frob grovels and where Grovel frobs.

;; This section of code has some important implications:
;;   1. Foo.
;;   2. Bar.
;;   3. Baz.

(defun fnord (zarquon)
  ;; If zob, then veeblefitz.
  (quux zot
        mumble             ; Zibblefrotz.
        frotz))
;; bad
(1+ counter) ; increments counter by one

Good code is like a good joke - it needs no explanation. – Russ Olsen

Do, or do not. There is no try. – Yoda

(defun some-fun ()
  ;; FIXME: This has crashed occasionally since v1.2.3. It may
  ;;        be related to the BarBazUtil upgrade. (xz 13-1-31)
  (baz))
(defun bar ()
  (sleep 100)) ; OPTIMIZE

Emacs is famous for the breadth, depth, and ubiquity of its documentation. By taking the time to write docstrings in your package, you are helping to continue that tradition!

;; good
(defun goto-line (line &optional buffer)
  "Go to LINE, counting from line 1 at beginning of buffer.
If called interactively, a numeric prefix argument specifies
LINE; without a numeric prefix argument, read LINE from the
minibuffer..."
...)

;; bad
(defun goto-line (line &optional buffer)
  "Go to LINE, counting from line 1 at beginning of buffer.
   If called interactively, a numeric prefix argument specifies
   LINE; without a numeric prefix argument, read LINE from the
   minibuffer..."
  ...)

;; also bad
(defun goto-line (line &optional buffer)
  "Go to LINE, counting from line 1 at beginning of buffer.
   If called interactively, a numeric prefix argument specifies
 LINE; without a numeric prefix argument, read LINE from the
 minibuffer..."
  ...)

Nothing written in this guide is set in stone. It’s my desire to work together with everyone interested in Emacs Lisp coding style, so that we could ultimately create a resource that will be beneficial to the entire Emacs community.

Feel free to open tickets or send pull requests with improvements. Thanks in advance for your help!

This work is licensed under a Creative Commons Attribution 3.0 Unported License

A community-driven style guide is of little use to a community that doesn’t know about its existence. Tweet about the guide, share it with your friends and colleagues. Every comment, suggestion or opinion we get makes the guide just a little bit better. And we want to have the best possible guide, don’t we?

Cheers,
Bozhidar


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4