Style guide

Discussion of Common Lisp
Post Reply
Unne
Posts: 32
Joined: Sat Jun 28, 2008 6:10 pm
Location: Oregon
Contact:

Style guide

Post by Unne » Fri Jul 10, 2009 11:15 am

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.

Paul Donnelly
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Style guide

Post by Paul Donnelly » Fri Jul 10, 2009 2:54 pm

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))

Harleqin
Posts: 71
Joined: Wed Dec 17, 2008 5:18 am
Location: Bonn, Germany

Re: Style guide

Post by Harleqin » Fri Jul 10, 2009 6:05 pm

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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Style guide

Post by nuntius » Fri Jul 10, 2009 8:54 pm

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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Style guide

Post by Jasper » Sat Jul 11, 2009 11:32 am

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.

lnostdal
Posts: 20
Joined: Thu Jul 03, 2008 2:01 pm
Location: Skien, Norway
Contact:

Re: Style guide

Post by lnostdal » Mon Jul 13, 2009 9:27 am

CLiki has a page that talks about (name) style: http://www.cliki.net/Naming%20conventions

Post Reply