This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Style guide

6 posts · 3050 views

Is there an authoritative or definitive style guide (mainly concerned about proper indentation) for CL? I've found this but it's Scheme-centric and doesn't seem complete, and has some odd suggestions.

Up to now my style guide has been "whatever Emacs lisp-mode does" but this has its limitations...
(with-some-resource foo
  (bar foo))  ;; nice

(some-long-macro-name foo
                      (bar foo))  ;; ouch
Which of these is good?
(if (foo)
    (bar)
    (baz))

(if (foo)
  (bar)
  (baz))
(foo (bar) (baz)
     (quux) (blarg)
     (something) (else))

(foo (bar)       (baz)
     (quux)      (blarg)
     (something) (else))
An so on. I realize this is subjective but I think a consistent style can help a lot to foster a community that can comfortably read and edit other people's code.

Re: Style guide

(with-some-resource foo
  (bar foo))

(if (foo)
    (bar)
    (baz))

(foo (bar) (baz)
     (quux) (blarg)
     (something) (else))
IMO. In general, lisp-mode has the right idea. When you make macros that take a body parameter, make sure to use &body so Emacs knows how to indent that portion. The last one, I think, is a matter of taste, but my position is never to attempt vertical alignment with weird spacing.

EDIT: You can look at simple vertical alignment (as in the “if” or “foo” call) as the standard, and divergences as a sign that “something special is happening here”. For example, in the following, “i” isn't just going to get evaluated, and the strange indentation implies special evaluation rules somewhere in that form. The same for “let” forms.
(dotimes (i 100)
  (foo i))

Re: Style guide

There is a small subchapter in Practical Common Lisp about formatting Lisp code (at the end of Chapter 4, "Syntax and Semantics").
"Just throw more hardware at it" is the root of all evil.
Svante

Re: Style guide

Code first, worry about style later. Learn style by coding and reading other people's code. Style is nothing without substance.

That said, here's a couple good resources.
http://norvig.com/luv-slides.ps
http://www.lispniks.com/faq/

As for indentation, do whatever emacs (or your other lisp-aware editor) does automatically.

Re: Style guide

As long as the code itself is nice, without too many nesting and such, i don't care much. Only not to put ))) at a new line. (If you need to, it is probably too nested.)

I also prefer the 2-space IF, the first argument really is different from the other two.

As for functions in general, i think you can do a first few arguments after it and then do others, especially if the first arguments are different somehow(then also for macros), the multiple statements on the single line would be like producing a functions an that is called.
(a-function input-really-revealing-what-it-is-used-for
  thing-one thing-two)
I also do this for keyword arguments.

Another way to save horizontal space is to put the arguments on the next line altogether.