Page 1 of 1

Style guide

PostPosted: Fri Jul 10, 2009 11:15 am
by Unne
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...

Code: Select all
(with-some-resource foo
  (bar foo))  ;; nice

(some-long-macro-name foo
                      (bar foo))  ;; ouch


Which of these is good?

Code: Select all
(if (foo)
    (bar)
    (baz))

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


Code: Select all
(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

PostPosted: Fri Jul 10, 2009 2:54 pm
by Paul Donnelly
Code: Select all
(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.
Code: Select all
(dotimes (i 100)
  (foo i))

Re: Style guide

PostPosted: Fri Jul 10, 2009 6:05 pm
by Harleqin
There is a small subchapter in Practical Common Lisp about formatting Lisp code (at the end of Chapter 4, "Syntax and Semantics").

Re: Style guide

PostPosted: Fri Jul 10, 2009 8:54 pm
by nuntius
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

PostPosted: Sat Jul 11, 2009 11:32 am
by Jasper
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.
Code: Select all
(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.

Re: Style guide

PostPosted: Mon Jul 13, 2009 9:27 am
by lnostdal
CLiki has a page that talks about (name) style: http://www.cliki.net/Naming%20conventions