| 1 |
-*- mode: text; coding: utf-8 -*- |
| 2 |
--------------------------------- |
| 3 |
|
| 4 |
CLISP sources coding style |
| 5 |
========================== |
| 6 |
|
| 7 |
Lisp developers are individuals, and every individual tends to have his |
| 8 |
own preferred coding style. Nevertheless, on projects like CLISP where |
| 9 |
several people come together to hack the same source, it is necessary to |
| 10 |
adhere to a common coding style, so that other developers enjoy reading |
| 11 |
your code. |
| 12 |
|
| 13 |
A common coding style is a compromise for all participants, and it is also |
| 14 |
an exercise in tolerance. When I contribute code to Linux, I adhere to the |
| 15 |
Linux coding style (/usr/src/linux/Documentation/CodingStyle - very |
| 16 |
recommended reading). When I contribute to GCC, I follow the GNU coding style |
| 17 |
("info standards"), although I get nauseous when I look at the result. |
| 18 |
When I write for XFree86, I follow their style; it's a somewhat neutral one. |
| 19 |
|
| 20 |
As a general rule, don't reformat entire files other people have written; |
| 21 |
that will make the other developers spit. Instead, adhere to the particular |
| 22 |
file's predominant style (even if it is not the same as the general style |
| 23 |
used in the project). |
| 24 |
|
| 25 |
A lot of good advice about naming conventions and placement and contents of |
| 26 |
comments can be found in other places; therefore I skip these issues here. |
| 27 |
I'll mention only one rule about |
| 28 |
|
| 29 |
Special variables |
| 30 |
----------------- |
| 31 |
|
| 32 |
All variables which are subject to 'defvar', 'defparameter' or 'declare |
| 33 |
special' (even if only locally in a function) must be surrounded by asterisks. |
| 34 |
I had once in a program a special variable called 'line', and it caused me |
| 35 |
an hour of debugging time at least. I will not make this mistake again... |
| 36 |
|
| 37 |
Tabs |
| 38 |
---- |
| 39 |
|
| 40 |
In CLISP source, except for Makefiles and ChangeLog files, we don't use tabs. |
| 41 |
This is because they have no functional advantage, and make "diff" output |
| 42 |
harder to read and understand. And also, because some editors on Windows have |
| 43 |
a default tab width of 4, whereas on Unix, the common consensus is a tab width |
| 44 |
of 8. If you are using Emacs and want to contribute to CLISP, the best way to |
| 45 |
ensure that you don't send us tabs by mistake is to add the following lines |
| 46 |
to your <$HOME/.emacs> file: |
| 47 |
|
| 48 |
;; Tabs are anachronistic. |
| 49 |
(setq-default indent-tabs-mode nil) |
| 50 |
|
| 51 |
Trailing whitespace |
| 52 |
------------------- |
| 53 |
|
| 54 |
Trailing whitespace pollutes diffs and should be avoided. |
| 55 |
Emacs will highlight it if you do |
| 56 |
(setq-default show-trailing-whitespace t) |
| 57 |
and remove it if you do |
| 58 |
(add-hook 'write-file-functions 'delete-trailing-whitespace) |
| 59 |
|
| 60 |
Lisp indentation and parentheses placement |
| 61 |
------------------------------------------ |
| 62 |
|
| 63 |
We try to keep things in 79 columns, because that's the default width of |
| 64 |
Emacs windows. We urge you to use the Emacs 21 lisp-mode to indent |
| 65 |
your lisp code, see file <clisp/emacs/clisp-indent.el>. |
| 66 |
VIM users should put <clisp/emacs/lisp.vim> into <$HOME/.vim/after/syntax/>. |
| 67 |
|
| 68 |
Parentheses should be set in the mainstream style (used in CLtL1, CLtL2 |
| 69 |
and the ANSI CL spec), with the closing parens at the end of the last |
| 70 |
line, like this: |
| 71 |
|
| 72 |
(defun get-funname-symbol (funname) |
| 73 |
(if (atom funname) |
| 74 |
funname |
| 75 |
(get-setf-symbol (second funname)))) |
| 76 |
|
| 77 |
Elements of a list are indented by 2 spaces relative to the list, except |
| 78 |
- for arguments of function calls, which often can be aligned to the first |
| 79 |
argument, |
| 80 |
- for elements of COND-clauses, which are usually indented by 1 space. |
| 81 |
|
| 82 |
Some of the CLISP code still uses the "Bruno style". Its characteristic |
| 83 |
is that closing parentheses are located either on the same line as the |
| 84 |
corresponding opening paren, or on extra lines containing only closing |
| 85 |
parentheses: |
| 86 |
|
| 87 |
(defun get-funname-symbol (funname) |
| 88 |
(if (atom funname) |
| 89 |
funname |
| 90 |
(get-setf-symbol (second funname)) |
| 91 |
) ) |
| 92 |
|
| 93 |
Please avoid adding code in this style. |
| 94 |
|
| 95 |
C style |
| 96 |
------- |
| 97 |
|
| 98 |
Most of the C code of CLISP is stored in files with extension '.d'. It _is_ |
| 99 |
C code, but the file will be preprocessed a bit. |
| 100 |
|
| 101 |
A comment until end of line is introduced by '# '. Other '#' signs, not |
| 102 |
followed by a space, have the usual C meaning. Please avoid adding new |
| 103 |
comments in this style, use the normal /* C comments */. |
| 104 |
|
| 105 |
Preprocessor commands can start at any column, not necessarily at the first |
| 106 |
column of a line. |
| 107 |
|
| 108 |
These 'var' symbols that you encounter in every function introduce variable |
| 109 |
declarations. We use one variable per declaration; the C syntax rules make |
| 110 |
declarations of multiple variables on the same line hard to read and hard |
| 111 |
to edit. 'var' is a preprocessor symbol which expands to nothing. |
| 112 |
Writing 'var' not only makes it easier for a human reader to understand |
| 113 |
the code; it also allows you to mix declarations and statements freely, |
| 114 |
without the need for additional braces (see utils/varbrace.c). |
| 115 |
|
| 116 |
C indentation |
| 117 |
------------- |
| 118 |
|
| 119 |
The general style is similar to K&R style or Linux kernel style, with a |
| 120 |
tab width of 2 spaces. A typical 'if' or 'while' statement looks like this: |
| 121 |
|
| 122 |
while (consp(keylistr)) { |
| 123 |
if (eq(item,Car(keylistr))) |
| 124 |
goto found; |
| 125 |
keylistr = Cdr(keylistr); |
| 126 |
} |
| 127 |
|
| 128 |
When statements have to span lines, split them before the operator; this |
| 129 |
is what the GNU standards recommend: |
| 130 |
|
| 131 |
if (mconsp(Car(alist)) && simple_string_p(Car(Car(alist))) |
| 132 |
&& string_equal(line,Car(Car(alist)))) { |
| 133 |
|
| 134 |
Use <clisp/emacs/d-mode.el> to make Emacs indent and fontify the *.d files. |
| 135 |
VIM users should put <clisp/emacs/d.vim> into <$HOME/.vim/syntax/>. |
| 136 |
|
| 137 |
Comments |
| 138 |
-------- |
| 139 |
|
| 140 |
Comments in front of a function definition describe the function's |
| 141 |
specification, i.e. all a caller of the function needs to know. |
| 142 |
Describe the function's behavior in a sentence with implicit subject |
| 143 |
"this function", not as a command. |
| 144 |
For algorithmic functions with non obvious complexity, the (worst-case) |
| 145 |
complexity (= running time) is part of the function's specification. |
| 146 |
Please follow the common format (UP stands for "Unterprogram"): |
| 147 |
/* UP: brief description |
| 148 |
> 1st input parameter |
| 149 |
> 2nd input parameter |
| 150 |
< return value */ |
| 151 |
Do not forget to list global variables accessed and modified, including STACK. |
| 152 |
|
| 153 |
Comments about the inner working of a function belong inside the function. |
| 154 |
|
| 155 |
Example: |
| 156 |
|
| 157 |
;; (isqrt x) returns the largest integer <= x^(1/2). |
| 158 |
;; x must be an integer >= 0. |
| 159 |
;; Complexity: O(M(n) log n) if x has n digits. |
| 160 |
(defun isqrt (x) |
| 161 |
;; Use Newton's algorithm. |
| 162 |
(let ((y (ash 1 (ash (integer-length x) -1)))) |
| 163 |
(loop (when (= y (setq y (ash (+ y (floor x y)) -1))) (return))) |
| 164 |
y)) |
| 165 |
|
| 166 |
Comments about a variable belong in front of the variable. |
| 167 |
|
| 168 |
German comments |
| 169 |
--------------- |
| 170 |
|
| 171 |
When CLISP was written, it was not anticipated to have wide distribution. |
| 172 |
We are sorry about the German comments. If you are a native German |
| 173 |
speaker, you are welcome to faithfully translate them to English. |
| 174 |
If not, please leave them alone and wait for someone to translate them |
| 175 |
to English. Many files have been already translated by Stefan Kain |
| 176 |
and Reini Urban; you may ask them (on <clisp-list>) to translate |
| 177 |
the file you need. |
| 178 |
|
| 179 |
German variable and function names are probably even more disturbing |
| 180 |
than the comments, and much harder to handle. Use babelfish. |
| 181 |
Remember: "fehler" == "error"; "zustand" == "state" |
| 182 |
(this is where the 'z' suffix often comes from). |
| 183 |
|
| 184 |
Please avoid "clever" tricks with Unicode in comments. |
| 185 |
E.g., using #\SUBSET_OF_OR_EQUAL_TO (Unicode #x2286) instead of the |
| 186 |
plain words "subclass of" or #\FULLWIDTH_SOLIDUS (Unicode #xff0f) & |
| 187 |
#\FULLWIDTH_ASTERISK (Unicode #xff0a) to emulate nested C comments |
| 188 |
breaks compilation in some language environments: |
| 189 |
$ LANG=ja_JP.eucJP make clisp.h |
| 190 |
... |
| 191 |
../src/lispbibl.d:1028: error: stray '\217' in program |
| 192 |
... |
| 193 |
Remember, ASCII rules! |
| 194 |
|
| 195 |
|
| 196 |
Variable names |
| 197 |
-------------- |
| 198 |
|
| 199 |
Use descriptive variable names. Don't use names like "ll", "tem", "stri", |
| 200 |
"comp"; if you want to participate in an obfuscated Lisp code contest, you |
| 201 |
may, but don't put the resulting code into CLISP. |
| 202 |
|
| 203 |
Variable initializations |
| 204 |
------------------------ |
| 205 |
|
| 206 |
If you want to access a variable's value, first initialize it. |
| 207 |
|
| 208 |
In Lisp, to initialize a list to be empty, write |
| 209 |
|
| 210 |
(let ((l '())) |
| 211 |
not |
| 212 |
(let ((l nil)) ; this doesn't tell whether l is a list, boolean etc. |
| 213 |
or |
| 214 |
(let (l) ; worse - looks like an uninitialized variable. |
| 215 |
|
| 216 |
In Lisp, if keyword arguments have a non-obvious default, write it down: |
| 217 |
|
| 218 |
(defun f (sequence &key (start 0) (end nil)) |
| 219 |
not |
| 220 |
(defun f (sequence &key (start 0) end) |
| 221 |
|
| 222 |
In C, to initialize a static variable to 0, write |
| 223 |
|
| 224 |
static int flag = 0; |
| 225 |
not |
| 226 |
static int flag; |
| 227 |
|
| 228 |
To many people this advice seems obvious; I am sorry that I have to repeat |
| 229 |
it here. |
| 230 |
|
| 231 |
Macros and ABI |
| 232 |
-------------- |
| 233 |
|
| 234 |
Macro expanders must not perform side effects; only the execution of the |
| 235 |
macro expansion may perform side effects. |
| 236 |
|
| 237 |
If a macro expansion needs to perform complex actions, try to move as much |
| 238 |
of it as possible into functions that are then called from the macro |
| 239 |
expansion. In particular, don't put literal strings into macro expansions. |
| 240 |
This helps 1. keeping the size of compiled .fas files small, 2. reducing |
| 241 |
the probability that O(version) needs to be bumped when you change the |
| 242 |
effect of the macro. |
| 243 |
|
| 244 |
Macro expansions can refer to documented symbols and functions from public |
| 245 |
packages (COMMON-LISP, CLOS, EXT, FFI), as well as undocumented, unexported |
| 246 |
functions. Both kinds of functions, together with their calling conventions, |
| 247 |
form the ABI (= Application Binary Interface) of .fas files. When the ABI |
| 248 |
changes, O(version) needs to be bumped, thus declaring previously compiled |
| 249 |
.fas files as invalid. The second kind of function should be marked as |
| 250 |
"; ABI" in the *.lisp source code or "/* ABI */" in constsym.d, to make the |
| 251 |
developers aware. |
| 252 |
|
| 253 |
Newline Output |
| 254 |
-------------- |
| 255 |
|
| 256 |
All output by the system must start with a fresh-line and must be terminated |
| 257 |
with an elastic newline. See doc/Newline-Convention.txt for the rationale. |
| 258 |
|
| 259 |
Change Management |
| 260 |
----------------- |
| 261 |
|
| 262 |
Even though CLISP is kept under the CVS, all functional changes require |
| 263 |
a ChangeLog entry. If there is no ChangeLog file in the current |
| 264 |
directory, the entry goes into <clisp/src/ChangeLog>; you should refer |
| 265 |
to the recent entries there for good examples of ChangeLog entries. |
| 266 |
One should always use a high-level overview of the change (1-2 lines) |
| 267 |
in the beginning of the entry, and use it as the CVS log message. |
| 268 |
The file name should include a path relative to one of 3 "roots": |
| 269 |
clisp/, clisp/src/ or clisp/modules/. E.g., <clisp/utils/clispload.lsp> |
| 270 |
is recorded as <utils/clispload.lsp>, <clisp/modules/pcre/cpcre.c> as |
| 271 |
<pcre/cpcre.c> and <clisp/src/lispbibl.d> as <lispbibl.d>. |
| 272 |
The main requirement is that the record must be unambiguous. |
| 273 |
|
| 274 |
We highly recommend using Emacs change-log mode with font-lock, as the |
| 275 |
highlighting will give many valuable suggestions about the right format. |
| 276 |
Use M-x add-change-log-entry RET to add an entry, and some information |
| 277 |
will be put there for you automagically. |
| 278 |
|
| 279 |
Never mix functional changes with formatting/translation changes! |
| 280 |
If you are fixing indentation or translating comments, make a separate |
| 281 |
CVS commit without a ChangeLog entry, then check in your functional |
| 282 |
changes and add the ChangeLog entry. |
| 283 |
|
| 284 |
You are encouraged to add a test case to a file in <clisp/tests/> |
| 285 |
whenever you add a feature or fix a bug, the more tests, the better. |
| 286 |
One requirement must be satisfied after every CVS commit: |
| 287 |
$ cvs up -C; ./configure --cbc build-dir |
| 288 |
must pass all tests, the old and the new alike. |
| 289 |
|
| 290 |
Documentation and comment changes do not need a ChangeLog entry. |
| 291 |
Do not abbreviate function, variable and file names |
| 292 |
(e.g., using regular expressions). |
| 293 |
|
| 294 |
All functionality should be documented. |
| 295 |
When adding a new symbol, document it in the Implementation Notes and |
| 296 |
add an appropriate mapping from the symbol to its XML ID in the impnotes |
| 297 |
to the file doc/Symbol-Table.text (in the order of the impnotes' flow). |
| 298 |
|
| 299 |
|
| 300 |
Source File Encoding |
| 301 |
-------------------- |
| 302 |
|
| 303 |
All files are to be encoded with utf-8 and Unix line endings (LF, ASCII 10). |
| 304 |
Please do make sure that you do not break non-ASCII characters |
| 305 |
and do not replace line endings with the dos CR/LF. |