forked from google/styleguide
-
Notifications
You must be signed in to change notification settings - Fork 8
/
lispguide.xml
3887 lines (3806 loc) · 152 KB
/
lispguide.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="styleguide.xsl"?>
<GUIDE title="Google Common Lisp Style Guide">
<p align="right">
Revision 1.28
</p>
<address>
Robert Brown
</address>
<address>
<a HREF="mailto:[email protected]">François-René Rideau</a>
</address>
<address>
In memoriam Dan Weinreb
</address>
<p align="center">
<cite>Patterns mean "I have run out of language."</cite> — Rich Hickey
</p>
<OVERVIEW>
<CATEGORY title="Important Note">
<STYLEPOINT title="Note: Displaying Hidden Details in this Guide">
<SUMMARY>
This style guide contains many details
that are initially hidden from view.
They are marked by the triangle icon, which you see here on your left.
Click it now. You should see "Hooray" appear below.
</SUMMARY>
<BODY>
<p>
Hooray! Now you know you can expand points to get more details.
Alternatively, there's an "expand all" at the top of this document.
</p>
</BODY>
</STYLEPOINT>
</CATEGORY>
<CATEGORY title="Background">
<p>
Common Lisp is a powerful multiparadigm programming language.
With great power comes great responsibility.
</p>
<p>
This guide recommends formatting and stylistic choices
designed to make your code easier for other people to understand.
For those internal applications and free software libraries that
we develop at Google,
you should keep within these guidelines when making changes.
Note however that each project has its own rules and customs
that complement or override these general guidelines;
the speed-oriented QPX low fare search engine notably
has a very different style and feel from the QRes reservation system.
</p>
<p>
If you're writing Common Lisp code outside Google,
we invite you to consider these guidelines.
You may find some of them useful
where they don't conflict with other priorities you have.
We welcome remarks and constructive feedback
on how to improve our guide, and
on what alternate styles work for you and why.
</p>
<p>
This guide is not a Common Lisp tutorial.
For basic information about the language, please consult
<a HREF="http://www.gigamonkeys.com/book/">Practical Common Lisp</a>.
For a language reference, please consult the
<a HREF="http://www.lispworks.com/documentation/HyperSpec/Front/index.htm">Common Lisp HyperSpec</a>.
For more detailed style guidance, take (with a pinch of salt)
a look at Peter Norvig and Kent Pitman's
<a HREF="http://norvig.com/luv-slides.ps">style guide</a>.
</p>
</CATEGORY>
</OVERVIEW>
<CATEGORY title="Meta-Guide">
<STYLEPOINT title="Must, Should, May, or Not">
<SUMMARY>
Each guideline's level of importance is indicated
by use of the following keywords and phrases, adapted from
<a href="https://www.ietf.org/rfc/rfc2119.txt">RFC 2119</a>.
</SUMMARY>
<BODY>
<table>
<tr>
<th valign="top">MUST</th>
<td>
<p>
This word, or the terms "REQUIRED" or "SHALL",
means that the guideline is an absolute requirement.
You must ask permission to violate a MUST.
</p>
</td>
</tr>
<tr>
<th valign="top">MUST NOT</th>
<td>
<p>
This phrase, or the phrase "SHALL NOT",
means that the guideline is an absolute prohibition.
You must ask permission to violate a MUST NOT.
</p>
</td>
</tr>
<tr>
<th valign="top">SHOULD</th>
<td>
<p>
This word, or the adjective "RECOMMENDED", means that
there may exist valid reasons in particular circumstances
to ignore the demands of the guideline, but
the full implications must be understood and carefully weighed
before choosing a different course.
You must ask forgiveness for violating a SHOULD.
</p>
</td>
</tr>
<tr>
<th valign="top">SHOULD NOT</th>
<td>
<p>
This phrase, or the phrase "NOT RECOMMENDED", means that
there may exist valid reasons in particular circumstances
to ignore the prohibitions of this guideline, but
the full implications should be understood and carefully weighed
before choosing a different course.
You must ask forgiveness for violating a SHOULD NOT.
</p>
</td>
</tr>
<tr>
<th valign="top">MAY</th>
<td>
<p>
This word, or the adjective "OPTIONAL",
means that an item is truly optional.
</p>
</td>
</tr>
</table>
<p>
Unlike RFCs, we don't capitalize every instance of one of the above
keywords when it is used.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Permission and Forgiveness">
<SUMMARY>
There are cases where transgression of some of these rules
is useful or even necessary.
In some cases, you must seek permission or obtain forgiveness
from the proper people.
</SUMMARY>
<BODY>
<p>
Permission comes from the owners of your project.
</p>
<p>
Forgiveness is requested in a comment
near the point of guideline violation,
and is granted by your code reviewer.
The original comment should be signed by you, and
the reviewer should add a signed approval to the comment at review time.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Conventions">
<SUMMARY>
You MUST follow conventions. They are not optional.
</SUMMARY>
<BODY>
<p>
Some of these guidelines are motivated by universal principles of good programming.
Some guidelines are motivated by technical peculiarities of Common Lisp.
Some guidelines were once motivated by a technical reason,
but the guideline remained after the reason subsided.
Some guidelines, such those about as comments and indentation,
are based purely on convention, rather than on clear technical merit.
Whatever the case may be, you must still follow these guidelines,
as well as other conventional guidelines
that have not been formalized in this document.
</p>
<p>
You MUST follow conventions.
They are important for readability.
When conventions are followed by default,
violations of the convention are a signal
that something notable is happening and deserves attention.
When conventions are systematically violated,
violations of the convention are a distracting noise
that needs to be ignored.
</p>
<p>
Conventional guidelines <em>are</em> indoctrination.
Their purpose is to make you follow the mores of the community,
so you can more effectively cooperate with existing members.
It is still useful to distinguish the parts that are technically motivated
from the parts that are mere conventions,
so you know when best to defy conventions for good effect,
and when not to fall into the pitfalls that the conventions are there to help avoid.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Old Code">
<SUMMARY>
Fix old code as you go.
</SUMMARY>
<BODY>
<p>
A lot of our code was written before these guidelines existed.
You should fix violations as you encounter them
in the course of your normal coding.
</p>
<p>
You must not fix violations en masse
without warning other developers and coordinating with them,
so as not to make the merging of large branches
more difficult than it already is.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Future Topics">
<SUMMARY>
There are many topics for additional standardization
not covered by current version of this document,
but deferred to future versions.
</SUMMARY>
<BODY>
<ul>
<li>
File and directory structure
</li>
<li>
Packages and modularity
</li>
<li>
Threads and locking
</li>
<li>
How to add configurable components
</li>
<li>
CLOS style: initforms, slot and accessor names, etc.
</li>
<li>
Recommendations on max number of slots per class.
</li>
<li>
More concrete examples of good code:
<ul>
<li>
exceptions
</li>
<li>
transactions, with retry
</li>
<li>
XML
</li>
<li>
typing
</li>
<li>
encapsulation / abstraction
</li>
<li>
class and slot names
</li>
<li>
etc.
</li>
</ul>
</li>
<li>
When (not) to use conditional compilation:
<ul>
<li>
modifying the product
</li>
<li>
conditional debugging/console output/etc.
</li>
<li>
"temporarily" commenting-out blocks of code
</li>
<li>
etc.
</li>
</ul>
</li>
</ul>
</BODY>
</STYLEPOINT>
</CATEGORY>
<CATEGORY title="General Guidelines">
<STYLEPOINT title="Principles">
<SUMMARY>
There are some basic principles for team software development
that every developer must keep in mind.
Whenever the detailed guidelines are inadequate, confusing or contradictory,
refer back to these principles for guidance:
<ul>
<li>
Every developer's code must be easy for another developer
to read, understand, and modify
— even if the first developer isn't around to explain it.
(This is the "hit by a truck" principle.)
</li>
<li>
Everybody's code should look the same.
Ideally, there should be no way to look at lines of code
and recognize it as "Fred's code" by its style.
</li>
<li>
Be precise.
</li>
<li>
Be concise.
</li>
<li>
KISS — Keep It Simple, Stupid.
</li>
<li>
Use the smallest hammer for the job.
</li>
<li>
Use common sense.
</li>
<li>
Keep related code together.
Minimize the amount of jumping around
someone has to do to understand an area of code.
</li>
</ul>
</SUMMARY>
<BODY>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Priorities">
<SUMMARY>
<p>
When making decisions about how to write a given piece of
code, aim for the following -ilities in this priority order:
</p>
<ul>
<li>
Usability by the customer
</li>
<li>
Debuggability/Testability
</li>
<li>
Readability/Comprehensibility
</li>
<li>
Extensibility/Modifiability
</li>
<li>
Efficiency (of the Lisp code at runtime)
</li>
</ul>
</SUMMARY>
<BODY>
<p>
Most of these are obvious.
</p>
<p>
Usability by the customer means that the system has to do what the
customer requires; it has to handle the customer's transaction
volumes, uptime requirements; etc.
</p>
<p>
For the Lisp efficiency point,
given two options of equivalent complexity,
pick the one that performs better.
(This is often the same as the one that conses less,
i.e. allocates less storage from the heap.)
</p>
<p>
Given two options where one is more complex than the other,
pick the simpler option and revisit the decision only if
profiling shows it to be a performance bottleneck.
</p>
<p>
However, avoid premature optimization.
Don't add complexity to speed up something that runs rarely,
since in the long run, it matters less whether such code is fast.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Architecture">
<SUMMARY>
To build code that is robust and maintainable,
it matters a lot how the code is divided into components,
how these components communicate,
how changes propagate as they evolve,
and more importantly
how the programmers who develop these components communicate
as these components evolve.
</SUMMARY>
<BODY>
<p>
If your work affects other groups, might be reusable across groups,
adds new components, has an impact on other groups
(including QA or Ops), or otherwise isn't purely local,
you must write it up using at least a couple of paragraphs,
and get a design approval from the other parties involved
before starting to write code — or be ready to scratch what you have
when they object.
</p>
<p>
If you don't know or don't care about these issues,
ask someone who does.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Using Libraries">
<SUMMARY>
Often, the smallest hammer is to use an existing library.
Or one that doesn't exist yet.
In such cases, you are encouraged to use or develop such a library,
but you must take appropriate precautions.
</SUMMARY>
<BODY>
<ul>
<li>
You MUST NOT start a new library
unless you established that none is already available
that can be fixed or completed into becoming what you need.
That's a rule against the NIH syndrome ("Not Invented Here"),
which is particularly strong amongst Lisp hackers.
</li>
<li>
Whichever library, old or new, you pick, you MUST get permission
to incorporate third-party code into the code base.
You must discuss the use of such library
in the appropriate mailing-list,
and have your code reviewed by people knowledgeable in the domain
and/or the Lisp library ecosystem (if any).
Please be ready to argue why this particular solution makes sense
as compared to other available libraries.
</li>
<li>
Some libraries are distributed under licenses not compatible
with the software you're writing, and
must not be considered available for use.
Be aware of these issues, or consult with people who are.
</li>
</ul>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Open-Sourcing Code">
<SUMMARY>
<p>
If you write a general-purpose library,
or modify an existing open-source library,
you are encouraged to publish the result
separate from your main project and then
have your project import it like any other open-source library.
</p>
</SUMMARY>
<BODY>
<p>
Use your judgment to distinguish
general-purpose versus business-specific code,
and open-source the general-purpose parts,
while keeping the business-specific parts a trade secret.
</p>
<p>
Open-Sourcing code has many advantages,
including being able to leverage third parties for development,
letting the development of features be user-directed,
and keeping you honest with respect to code quality.
Whatever code you write, you will have to maintain anyway,
and make sure its quality is high enough to sustain use in production.
There should therefore be no additional burden to Open-Sourcing,
even of code that (at least initially)
is not directly usable by third parties.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Development Process">
<SUMMARY>
Development process is outside the scope of this document.
However, developers should remember at least these bits:
get reviewed, write tests, eliminate warnings, run tests, avoid mass-changes.
</SUMMARY>
<BODY>
<p>
</p>
<ul>
<li>
All code changes must be reviewed.
You should expect that your code will be reviewed by other hackers,
and that you will be assigned other hackers' code to review.
Part of the review criteria will be that code obeys
the coding standards in this document.
</li>
<li>
You must write and check-in tests for new code that you write and old bugs you fix.
There must be a unit test for every API function,
and any previously failing case.
Your work is not truly done until this activity is complete.
Estimating tasks must include the time it takes to produce such tests.
</li>
<li>
Your code must compile
without any compilation error or warning messages whatsoever.
If the compiler issues warnings that should be ignored,
muffle those warnings using the
<code>UIOP:WITH-MUFFLED-COMPILER-CONDITIONS</code> and
<code>UIOP:*UNINTERESTING-COMPILER-CONDITIONS*</code>
framework (part of <code>UIOP</code>, part of <code>ASDF 3</code>),
either around the entire project, or around individual files
(using <code>ASDF</code>'s <code>:around-compile</code> hooks).
</li>
<li>
All code should be checked in an appropriate source control system,
in a way that allows for complete reproducibility of
build, test and execution of
the code that is, has been or may be deployed.
</li>
<li>
You must run the "precheckin" tests, and each component must pass
its unit tests successfully before you commit any code.
</li>
<li>
You should incorporate code coverage into your testing process.
Tests are not sufficient
if they do not cover all new and updated code;
code that for whatever reason cannot be included in coverage results
should be clearly marked as such including the reason.
</li>
<li>
Many people develop on branches.
You must get permission to undertake mass-changes
(e.g. mass reindentations)
so that we can coordinate in advance,
and give branch residents time to get back on the mainline
</li>
</ul>
</BODY>
</STYLEPOINT>
</CATEGORY>
<CATEGORY title="Formatting">
<STYLEPOINT title="Spelling and Abbreviations">
<SUMMARY>
<p>
You must use correct spelling in your comments,
and most importantly in your identifiers.
</p>
<p>
When several correct spellings exist (including American vs English),
and there isn't a consensus amongst developers as which to use,
you should choose the shorter spelling.
</p>
<p>
You must use only common and domain-specific abbreviations, and
must be consistent with these abbreviations. You may abbreviate
lexical variables of limited scope in order to avoid overly-long
symbol names.
</p>
</SUMMARY>
<BODY>
<p>
If you're not sure, consult a dictionary,
Google for alternative spellings,
or ask a local expert.
</p>
<p>
Here are examples of choosing the correct spelling:
</p>
<ul>
<li>
Use "complimentary" in the sense of a meal or beverage
that is not paid for by the recipient, not "complementary".
</li>
<li>
Use "existent" and "nonexistent", not "existant".
Use "existence", not "existance".
</li>
<li>
Use "hierarchy" not "heirarchy".
</li>
<li>
Use "precede" not "preceed".
</li>
<li>
Use "weird", not "wierd".
</li>
</ul>
<p>
Here are examples of choosing the shorter spelling:
</p>
<ul>
<li>
Use "canceled", not "cancelled"
</li>
<li>
Use "queuing", not "queueing".
</li>
<li>
Use "signaled", not "signalled";
</li>
<li>
Use "traveled", not "travelled".
</li>
<li>
Use "aluminum", not "aluminium"
</li>
<li>
Use "oriented", not "orientated"
</li>
<li>
Use "color", not "colour"
</li>
<li>
Use "behavior", not "behaviour"
</li>
</ul>
<p>
Make appropriate exceptions for industry standard nomenclature/jargon,
including plain misspellings.
For instance:
</p>
<ul>
<li>
Use "referer", not "referrer", in the context of the HTTP protocol.
</li>
</ul>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Line length">
<SUMMARY>
You should format source code so that no line is longer than 100 characters.
</SUMMARY>
<BODY>
<p>
Some line length restriction is better than none at all.
While old text terminals used to make 80 columns the standard,
these days, allowing 100 columns seems better,
since good style encourages the use of
descriptive variables and function names.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Indentation">
<SUMMARY>
<p>
Indent your code the way a properly configured GNU Emacs does.
</p>
<p>
Maintain a consistent indentation style throughout a project.
</p>
<p>
Indent carefully to make the code easier to understand.
</p>
</SUMMARY>
<BODY>
<p>
Common Lisp indentation in Emacs is provided by the cl-indent library.
The latest version of cl-indent is packaged with
<a HREF="https://www.common-lisp.net/project/slime/">SLIME</a>
(under contrib/slime-cl-indent.el). After installing SLIME, set up Emacs
to load SLIME automatically using
<a HREF="https://www.common-lisp.net/project/slime/doc/html/Loading-Contribs.html">these instructions</a>, adding slime-indentation to the list of
contrib libraries to be loaded in the call to slime-setup.
</p>
<p>
Ideally, use the default indentation settings provided by
slime-indentation. If necessary, customize indentation parameters to
maintain a consistent indentation style throughout an existing project.
Parameters can be customized using the :variables setting in
define-common-lisp-style. Indentation of specific forms can be
customized using the :indentation setting of define-common-lisp-style.
This is particularly useful when creating forms that behave like macros
or special operators that are indented differently than standard
function calls (e.g. defun, labels, or let). Add a
<a HREF="https://www.gnu.org/software/emacs/manual/html_node/emacs/Hooks.html">hook</a> to 'lisp-mode-hook that calls common-lisp-set-style to set
the appropriate style automatically.
</p>
<p>
Use indentation to make complex function applications easier to read.
When an application does not fit on one line
or the function takes many arguments,
consider inserting newlines between the arguments
so that each one is on a separate line.
However, do not insert newlines in a way that makes it hard to tell
how many arguments the function takes
or where an argument form starts and ends.
</p>
<BAD_CODE_SNIPPET>
;; Bad
(do-something first-argument second-argument (lambda (x)
(frob x)) fourth-argument last-argument)
</BAD_CODE_SNIPPET>
<CODE_SNIPPET>
;; Better
(do-something first-argument
second-argument
#'(lambda (x) (frob x))
fourth-argument
last-argument)
</CODE_SNIPPET>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="File Header">
<SUMMARY>
<p>
You should include a description at the top of each source file.
</p>
<p>
You should include neither authorship nor copyright information in a source file.
</p>
</SUMMARY>
<BODY>
<p>
Every source file should begin with a brief description
of the contents of that file.
</p>
<p>
After that description, every file should start the code itself with an
<code>(in-package #:<em>package-name</em>)</code> form.
</p>
<p>
After that <code>in-package</code> form,
every file should follow with any file-specific
<code>(declaim (optimize ...))</code> declaration
that is not covered by an <code>ASDF</code> <code>:around-compile</code> hook.
</p>
<CODE_SNIPPET>
;;;; Variable length encoding for integers and floating point numbers.
(in-package #:varint)
(declaim #.*optimize-default*)
</CODE_SNIPPET>
<p>
You should not include authorship information at the top of a file:
better information is available from version control,
and such a mention will only cause confusion and grief.
Indeed, whoever was the main author at the time such a mention was included
might not be who eventually made the most significant contributions to the file,
and even less who is responsible for the file at the moment.
</p>
<p>
You should not include copyright information in individual source code files.
An exception is made for files meant to be disseminated as standalone.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Vertical white space">
<SUMMARY>
Vertical white space: one blank line between top-level forms.
</SUMMARY>
<BODY>
<p>
You should include one blank line between top-level forms,
such as function definitions.
Exceptionally, blank lines can be omitted
between simple, closely related defining forms of the same kind,
such as a group of related type declarations or constant definitions.
</p>
<CODE_SNIPPET>
(defconstant +mix32+ #x12b9b0a1 "pi, an arbitrary number")
(defconstant +mix64+ #x2b992ddfa23249d6 "more digits of pi")
(defconstant +golden-ratio32+ #x9e3779b9 "the golden ratio")
(defconstant +golden-ratio64+ #xe08c1d668b756f82 "more digits of the golden ratio")
(defmacro incf32 (x y)
"Like INCF, but for integers modulo 2**32"
`(setf ,x (logand (+ ,x ,y) #xffffffff)))
(defmacro incf64 (x y)
"Like INCF, but for integers modulo 2**64"
`(setf ,x (logand (+ ,x ,y) #xffffffffffffffff)))
</CODE_SNIPPET>
<p>
Blank lines can be used to separate parts of a complicated function.
Generally, however, you should break a large function into smaller ones
instead of trying to make it more readable by adding vertical space.
If you can't, you should document with a <code>;;</code> comment
what each of the separated parts of the function does.
</p>
<p>
You should strive to keep top-level forms,
including comments but excluding the documentation string, of
appropriate length; preferably short. Forms extending beyond a
single page should be rare and their use should be justified.
This applies to each of the forms in an <code>eval-when</code>,
rather than to the <code>eval-when</code> itself.
Additionally, <code>defpackage</code> forms may be longer,
since they may include long lists of symbols.
</p>
</BODY>
</STYLEPOINT>
<STYLEPOINT title="Horizontal white space">
<SUMMARY>
Horizontal white space: none around parentheses. No tabs.
</SUMMARY>
<BODY>
<p>
You must not include extra horizontal whitespace
before or after parentheses or around symbols.
</p>
<p>
You must not place right parentheses by themselves on a line.
A set of consecutive trailing parentheses must appear on the same line.
</p>
<BAD_CODE_SNIPPET>
;; Very Bad
( defun factorial ( limit )
( let (( product 1 ))
( loop for i from 1 upto limit
do (setf product ( * product i ) ) )
product
)
)
</BAD_CODE_SNIPPET>
<CODE_SNIPPET>
;; Better
(defun factorial (limit)
(let ((product 1))
(loop for i from 1 upto limit
do (setf product (* product i)))
product))
</CODE_SNIPPET>
<p>
You should use only one space between forms.
</p>
<p>
You should not use spaces to vertically align forms
in the middle of consecutive lines.
An exception is made when the code possesses
an important yet otherwise not visible symmetry
that you want to emphasize.
</p>
<BAD_CODE_SNIPPET>
;; Bad
(let* ((low 1)
(high 2)
(sum (+ (* low low) (* high high))))
...)
</BAD_CODE_SNIPPET>
<CODE_SNIPPET>
;; Better
(let* ((low 1)
(high 2)
(sum (+ (* low low) (* high high))))
...))
</CODE_SNIPPET>
<p>
You must align nested forms if they occur across more than one line.
</p>
<BAD_CODE_SNIPPET>
;; Bad
(defun munge (a b c)
(* (+ a b)
c))
</BAD_CODE_SNIPPET>
<CODE_SNIPPET>
;; Better
(defun munge (a b c)
(* (+ a b)
c))
</CODE_SNIPPET>
<p>
The convention is that the body of a binding form
is indented two spaces after the form.
Any binding data before the body is usually indented four spaces.
Arguments to a function call are aligned with the first argument;
if the first argument is on its own line,
it is aligned with the function name.
</p>
<CODE_SNIPPET>
(multiple-value-bind (a b c d)
(function-returning-four-values x y)
(declare (ignore c))
(something-using a)
(also-using b d))
</CODE_SNIPPET>
<p>
An exception to the rule against lonely parentheses
is made for an <code>eval-when</code> form around several definitions;
in this case, include a comment <code>; eval-when</code>
after the closing parenthesis.
</p>
<p>
You must set your editor to
avoid inserting tab characters in the files you edit.
Tabs cause confusion when editors disagree
on how many spaces they represent.
In Emacs, do <code>(setq-default indent-tabs-mode nil)</code>.
</p>
</BODY>
</STYLEPOINT>
</CATEGORY>
<CATEGORY title="Documentation">
<STYLEPOINT title="Document everything">
<SUMMARY>
You should use document strings on all visible functions
to explain how to use your code.
</SUMMARY>
<BODY>
<p>
Unless some bit of code is painfully self-explanatory,
document it with a documentation string (also known as docstring).
</p>
<p>
Documentation strings are destined to be read
by the programmers who use your code.
They can be extracted from functions, types, classes, variables and macros,
and displayed by programming tools, such as IDEs,
or by REPL queries such as <code>(describe 'foo)</code>;
web-based documentation or other reference works
can be created based on them.
Documentation strings are thus the perfect locus to document your API.
They should describe how to use the code
(including what pitfalls to avoid),
as opposed to how the code works (and where more work is needed),
which is what you'll put in comments.
</p>
<p>
Supply a documentation string when defining
top-level functions, types, classes, variables and macros.
Generally, add a documentation string wherever the language allows.
</p>
<p>
For functions, the docstring should describe the function's contract:
what the function does,
what the arguments mean,
what values are returned,
what conditions the function can signal.
It should be expressed at the appropriate level of abstraction,
explaining the intended meaning rather than, say, just the syntax.
In documentation strings, capitalize the names of Lisp symbols,
such as function arguments.
For example, "The value of LENGTH should be an integer."
</p>
<CODE_SNIPPET>
(defun small-prime-number-p (n)
"Return T if N, an integer, is a prime number. Otherwise, return NIL."
(cond ((or (< n 2))
nil)
((= n 2)
t)
((divisorp 2 n)
nil)
(t
(loop for i from 3 upto (sqrt n) by 2
never (divisorp i n)))))
</CODE_SNIPPET>
<CODE_SNIPPET>
(defgeneric table-clear (table)
(:documentation
"Like clrhash, empties the TABLE of all
associations, and returns the table itself."))
</CODE_SNIPPET>
<p>
A long docstring may usefully
begin with a short, single-sentence summary,
followed by the larger body of the docstring.
</p>
<p>
When the name of a type is used,
the symbol may be quoted by surrounding it with
a back quote at the beginning and a single quote at the end.
Emacs will highlight the type, and the highlighting serves
as a cue to the reader that <kbd>M-.</kbd>
will lead to the symbol's definition.
</p>
<CODE_SNIPPET>
(defun bag-tag-expected-itinerary (bag-tag)
"Return a list of `legacy-pnr-pax-segment' objects representing
the expected itinerary of the `bag-tag' object, BAG-TAG."
...)
</CODE_SNIPPET>
<p>
Every method of a generic function should be independently documented