« Wilbur mentioned in a C|Net article! | Main | April 1st: Functional vs. imperative style - confusion prevails? »

2005-03-30

On Common Lisp "style"

Whenever I see code written by other people - especially Common Lisp code - it makes me realize that I am quite obsessed about writing my own code in a very particular style. This style consists of lexical rules (ones dealing with how the code looks) and idioms (certain CL constructs are to be used in lieu of others in certain situations). Long time ago, when working at CMU, I actually wrote a guideline and distributed it to the other folks working in the same project - they thought I was pretty much insane. Oh well...

I will discuss the lexical rules first: Common Lisp is always written in lower case. Names with multiple parts are written with parts separated with hyphens (e.g., foo-bar). "Camel case" (e.g., FooBar) is not permitted. Names of predicates (i.e., functions yielding a boolean value) end in a "p". If a predicate has a multi-part name, then the "p" is separated with a hyphen. Some standard functions of Common Lisp represent exceptions to this rule (e.g., atom), mostly for historical reasons. Global variables (i.e., those introduced with defvar or defparameter) are named using asterisks at both ends. Global constants (i.e., those introduced with defconstant are named with hyphens at both ends. Macros that assign values to generalized variables have names that end in an "f".

Common Lisp programs are always written with a Decent Editor (such as gnu-emacs). Any Decent Editor knows how to indent Common Lisp programs. It is futile to try to defeat the editor's indentation rules and/or to indent by hand.

Comments are introduced using the semicolon character. The number of consecutive semicolons determines the (automatic) placement of the comment:

  • A single semicolon introduces a comment placed at the end of a line containing code.

  • Two semicolons introduce a comment indented at the indentation level of the code.

  • Three semicolons introduce a comment placed at the beginning of a line.

  • Four semicolons introduce a comment indicating the title of a file or a bigger code block.

Since a Decent Editor will observe these conventions and indent accordingly, it is again futile to try to defeat the indentation rules. For an in-depth discussion about commenting conventions, see CLtL2 pp.526-527.

At some later date, I will discuss the idioms.

Posted by ora at 20:41

Comments

All common wisdom and good advice except your convention for -constants- -- I only was familiar with +constant-variable+ (i.e. plus signs at both ends). Perhaps everyone develops his/her own style for constants because they are so rarely used in CL anyway.

When I was an egg, I was given Pitman and Norvig's style guide http://www.norvig.com/luv-slides.ps and always liked its advice.

Posted by: Rudi Schlatte at April 2, 2005 07:03 AM

I cannot remember the reason for the convention for -constants- ... I have used it for at least 18 years. Hmm... I am sure I had a good reason originally :-)

Posted by: Ora Lassila at April 3, 2005 08:51 AM