emacs.d/clones/lisp/llthw.common-lisp.dev/1-01-07-style-guide.html

2006 lines
65 KiB
HTML
Raw Normal View History

2022-08-24 19:36:32 +02:00
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Common Lisp Style Guide · Learn Lisp The Hard Way</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content=""the Phoeron" Colin J.E. Lupton">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-hints/plugin-hints.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-folding-chapters/folding-chapters.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="1-01-08-configuration.html" />
<link rel="prev" href="1-01-06-prefix-notation.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="header">LEARN LISP THE HARD WAY</li>
<li class="chapter " data-level="1.1" data-path="./">
<a href="index.html">
Second Draft (in-progress)
</a>
</li>
<li class="chapter " data-level="1.2" data-path="CHANGELOG.html">
<a href="CHANGELOG.html">
CHANGELOG
</a>
</li>
<li class="chapter " data-level="1.3" data-path="TODO.html">
<a href="TODO.html">
TODO
</a>
</li>
<li class="header">PREFACE</li>
<li class="chapter " data-level="2.1" data-path="preface.html">
<a href="preface.html">
TANSTAAFL
</a>
</li>
<li class="chapter " data-level="2.2" data-path="preface-part-two.html">
<a href="preface-part-two.html">
The Hard Way is Easier
</a>
</li>
<li class="chapter " data-level="2.3" data-path="preface-part-three.html">
<a href="preface-part-three.html">
Who Is This Book For?
</a>
</li>
<li class="chapter " data-level="2.4" data-path="introduction.html">
<a href="introduction.html">
Lisp: A Future History
</a>
</li>
<li class="chapter " data-level="2.5" data-path="acknowledgements.html">
<a href="acknowledgements.html">
Acknowledgements
</a>
</li>
<li class="header">PART ONE</li>
<li class="chapter " data-level="3.1" data-path="1-0-0-overview.html">
<a href="1-0-0-overview.html">
Grokking Lisp
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.1" data-path="1-01-00-lisp-bootcamp.html">
<a href="1-01-00-lisp-bootcamp.html">
Common Lisp Bootcamp
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.1.1" data-path="1-01-01-syntax-overview.html">
<a href="1-01-01-syntax-overview.html">
Syntax Overview in 5 Minutes
</a>
</li>
<li class="chapter " data-level="3.1.1.2" data-path="1-01-02-repl.html">
<a href="1-01-02-repl.html">
The REPL
</a>
</li>
<li class="chapter " data-level="3.1.1.3" data-path="1-01-03-expressions.html">
<a href="1-01-03-expressions.html">
Expressions, Parentheses, and Return Values
</a>
</li>
<li class="chapter " data-level="3.1.1.4" data-path="1-01-04-lists-cons-cells.html">
<a href="1-01-04-lists-cons-cells.html">
Lists, Cons-Cells, and Memory
</a>
</li>
<li class="chapter " data-level="3.1.1.5" data-path="1-01-05-symbols.html">
<a href="1-01-05-symbols.html">
Symbols and Namespaces
</a>
</li>
<li class="chapter " data-level="3.1.1.6" data-path="1-01-06-prefix-notation.html">
<a href="1-01-06-prefix-notation.html">
Prefix Notation
</a>
</li>
<li class="chapter active" data-level="3.1.1.7" data-path="1-01-07-style-guide.html">
<a href="1-01-07-style-guide.html">
Common Lisp Style Guide
</a>
</li>
<li class="chapter " data-level="3.1.1.8" data-path="1-01-08-configuration.html">
<a href="1-01-08-configuration.html">
Configuring Your Development Environment
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.1.2" data-path="1-02-00-input-output.html">
<a href="1-02-00-input-output.html">
Printing, Streams, and Strings
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.2.1" data-path="1-02-01-strings.html">
<a href="1-02-01-strings.html">
Strings
</a>
</li>
<li class="chapter " data-level="3.1.2.2" data-path="1-02-02-more-strings.html">
<a href="1-02-02-more-strings.html">
More Strings
</a>
</li>
<li class="chapter " data-level="3.1.2.3" data-path="1-02-03-unicode.html">
<a href="1-02-03-unicode.html">
Unicode and Strings
</a>
</li>
<li class="chapter " data-level="3.1.2.4" data-path="1-02-04-chars.html">
<a href="1-02-04-chars.html">
Characters
</a>
</li>
<li class="chapter " data-level="3.1.2.5" data-path="1-02-05-more-chars.html">
<a href="1-02-05-more-chars.html">
More Characters
</a>
</li>
<li class="chapter " data-level="3.1.2.6" data-path="1-02-06-char-codes.html">
<a href="1-02-06-char-codes.html">
Character Codes
</a>
</li>
<li class="chapter " data-level="3.1.2.7" data-path="1-02-07-strings-from-chars.html">
<a href="1-02-07-strings-from-chars.html">
Strings from Chars
</a>
</li>
<li class="chapter " data-level="3.1.2.8" data-path="1-02-08-printing.html">
<a href="1-02-08-printing.html">
Printing
</a>
</li>
<li class="chapter " data-level="3.1.2.9" data-path="1-02-09-more-printing.html">
<a href="1-02-09-more-printing.html">
More Printing
</a>
</li>
<li class="chapter " data-level="3.1.2.10" data-path="1-02-10-prin1.html">
<a href="1-02-10-prin1.html">
Printing With prin1
</a>
</li>
<li class="chapter " data-level="3.1.2.11" data-path="1-02-11-princ.html">
<a href="1-02-11-princ.html">
Printing With princ
</a>
</li>
<li class="chapter " data-level="3.1.2.12" data-path="1-02-12-format.html">
<a href="1-02-12-format.html">
A Brief Introduction to Format
</a>
</li>
<li class="chapter " data-level="3.1.2.13" data-path="1-02-13-more-format.html">
<a href="1-02-13-more-format.html">
A Little Bit More on Format
</a>
</li>
<li class="chapter " data-level="3.1.2.14" data-path="1-02-14-pathnames.html">
<a href="1-02-14-pathnames.html">
Pathnames
</a>
</li>
<li class="chapter " data-level="3.1.2.15" data-path="1-02-15-streams.html">
<a href="1-02-15-streams.html">
Streams
</a>
</li>
<li class="chapter " data-level="3.1.2.16" data-path="1-02-16-file-streams.html">
<a href="1-02-16-file-streams.html">
File Streams
</a>
</li>
<li class="chapter " data-level="3.1.2.17" data-path="1-02-17-binary-streams.html">
<a href="1-02-17-binary-streams.html">
Binary Streams
</a>
</li>
<li class="chapter " data-level="3.1.2.18" data-path="1-02-18-prompting-users.html">
<a href="1-02-18-prompting-users.html">
Prompting Users
</a>
</li>
<li class="chapter " data-level="3.1.2.19" data-path="1-02-19-pretty-printing.html">
<a href="1-02-19-pretty-printing.html">
Pretty-Printing
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.1.3" data-path="1-03-0-getting-input-from-users.html">
<a href="1-03-0-getting-input-from-users.html">
Extra Credit: Getting Input from Users
</a>
</li>
<li class="chapter " data-level="3.1.4" data-path="1-04-0-lists.html">
<a href="1-04-0-lists.html">
Lists and List-Operations
</a>
</li>
<li class="chapter " data-level="3.1.5" data-path="1-05-0-lookups-trees.html">
<a href="1-05-0-lookups-trees.html">
Extra Credit: Look-up Lists and Trees
</a>
</li>
<li class="chapter " data-level="3.1.6" data-path="1-06-0-math.html">
<a href="1-06-0-math.html">
Numbers and Math
</a>
<ul class="articles">
<li class="chapter " data-level="3.1.6.1" data-path="1-06-01-integers.html">
<a href="1-06-01-integers.html">
Integers
</a>
</li>
<li class="chapter " data-level="3.1.6.2" data-path="1-06-02-more-integers.html">
<a href="1-06-02-more-integers.html">
More Integers
</a>
</li>
<li class="chapter " data-level="3.1.6.3" data-path="1-06-03-hexadecimal-notation.html">
<a href="1-06-03-hexadecimal-notation.html">
Hexadecimal Integer Notation
</a>
</li>
<li class="chapter " data-level="3.1.6.4" data-path="1-06-04-octal-notation.html">
<a href="1-06-04-octal-notation.html">
Octal Integer Notation
</a>
</li>
<li class="chapter " data-level="3.1.6.5" data-path="1-06-05-binary-notation.html">
<a href="1-06-05-binary-notation.html">
Binary Integer Notation
</a>
</li>
<li class="chapter " data-level="3.1.6.6" data-path="1-06-06-ratios.html">
<a href="1-06-06-ratios.html">
Ratios and Rational Numbers
</a>
</li>
<li class="chapter " data-level="3.1.6.7" data-path="1-06-07-floating-point.html">
<a href="1-06-07-floating-point.html">
Floating-point Numbers
</a>
</li>
<li class="chapter " data-level="3.1.6.8" data-path="1-06-08-constants.html">
<a href="1-06-08-constants.html">
Numeric Constants
</a>
</li>
<li class="chapter " data-level="3.1.6.9" data-path="1-06-09-complex-numbers.html">
<a href="1-06-09-complex-numbers.html">
Complex Numbers
</a>
</li>
<li class="chapter " data-level="3.1.6.10" data-path="1-06-10-arithmetic.html">
<a href="1-06-10-arithmetic.html">
Arithmetic
</a>
</li>
<li class="chapter " data-level="3.1.6.11" data-path="1-06-11-more-arithmetic.html">
<a href="1-06-11-more-arithmetic.html">
More Arithmetic
</a>
</li>
<li class="chapter " data-level="3.1.6.12" data-path="1-06-12-even-more-arithmetic.html">
<a href="1-06-12-even-more-arithmetic.html">
Even More Arithmetic
</a>
</li>
<li class="chapter " data-level="3.1.6.13" data-path="1-06-13-exponents.html">
<a href="1-06-13-exponents.html">
Exponents
</a>
</li>
<li class="chapter " data-level="3.1.6.14" data-path="1-06-14-logarithms.html">
<a href="1-06-14-logarithms.html">
Logarithms
</a>
</li>
<li class="chapter " data-level="3.1.6.15" data-path="1-06-15-trigonometry.html">
<a href="1-06-15-trigonometry.html">
Trigonometry
</a>
</li>
<li class="chapter " data-level="3.1.6.16" data-path="1-06-16-psuedorandom-numbers.html">
<a href="1-06-16-psuedorandom-numbers.html">
Pseudo-Random Numbers
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3.1.7" data-path="1-07-0-arrays.html">
<a href="1-07-0-arrays.html">
Extra Credit: Arrays and Vectors
</a>
</li>
<li class="chapter " data-level="3.1.8" data-path="1-08-0-variables.html">
<a href="1-08-0-variables.html">
Variables, Parameters, and Constants
</a>
</li>
<li class="chapter " data-level="3.1.9" data-path="1-09-0-closures.html">
<a href="1-09-0-closures.html">
Extra Credit: Closures
</a>
</li>
<li class="chapter " data-level="3.1.10" data-path="1-10-0-functions.html">
<a href="1-10-0-functions.html">
Functions and Macros
</a>
</li>
<li class="chapter " data-level="3.1.11" data-path="1-11-0-text-adventure.html">
<a href="1-11-0-text-adventure.html">
Extra Credit: A Simple Text Adventure
</a>
</li>
<li class="chapter " data-level="3.1.12" data-path="1-12-0-namespaces.html">
<a href="1-12-0-namespaces.html">
Namespaces, Symbols, Packages, and Systems
</a>
</li>
<li class="chapter " data-level="3.1.13" data-path="1-13-0-simple-web-app.html">
<a href="1-13-0-simple-web-app.html">
Extra Credit: A Simple Web Application
</a>
</li>
<li class="chapter " data-level="3.1.14" data-path="1-14-0-conditionals.html">
<a href="1-14-0-conditionals.html">
Conditionals
</a>
</li>
<li class="chapter " data-level="3.1.15" data-path="1-15-0-command-line-utility.html">
<a href="1-15-0-command-line-utility.html">
Extra Credit: Command-Line Utilities
</a>
</li>
<li class="chapter " data-level="3.1.16" data-path="1-16-0-map-loop.html">
<a href="1-16-0-map-loop.html">
Mapping and Looping
</a>
</li>
<li class="chapter " data-level="3.1.17" data-path="1-17-0-iterate.html">
<a href="1-17-0-iterate.html">
Extra Credit: Revisiting Loops with Iterate
</a>
</li>
<li class="chapter " data-level="3.1.18" data-path="1-18-0-format.html">
<a href="1-18-0-format.html">
Format Strings
</a>
</li>
<li class="chapter " data-level="3.1.19" data-path="1-19-0-dsl.html">
<a href="1-19-0-dsl.html">
Extra Credit: Domain Specific Languages
</a>
</li>
<li class="chapter " data-level="3.1.20" data-path="1-20-0-review.html">
<a href="1-20-0-review.html">
Part One in Review
</a>
</li>
</ul>
</li>
<li class="header">PART TWO</li>
<li class="chapter " data-level="4.1" data-path="2-0-0-overview.html">
<a href="2-0-0-overview.html">
The Suffusion of Blue
</a>
<ul class="articles">
<li class="chapter " data-level="4.1.1" data-path="2-01-0-programming-paradigms.html">
<a href="2-01-0-programming-paradigms.html">
Programming Paradigms
</a>
</li>
<li class="chapter " data-level="4.1.2" data-path="2-02-0-regex.html">
<a href="2-02-0-regex.html">
Extra Credit: Regular Expressions
</a>
</li>
<li class="chapter " data-level="4.1.3" data-path="2-03-0-objects-control.html">
<a href="2-03-0-objects-control.html">
Objects and Control Structures
</a>
</li>
<li class="chapter " data-level="4.1.4" data-path="2-04-0-data-persistence.html">
<a href="2-04-0-data-persistence.html">
Extra Credit: Persistence and Databases
</a>
</li>
<li class="chapter " data-level="4.1.5" data-path="2-05-0-extended-types.html">
<a href="2-05-0-extended-types.html">
Extended Types
</a>
</li>
<li class="chapter " data-level="4.1.6" data-path="2-06-0-threads-memos-parallel.html">
<a href="2-06-0-threads-memos-parallel.html">
Extra Credit: Concurrency and Memoization
</a>
</li>
<li class="chapter " data-level="4.1.7" data-path="2-07-0-logic-and-more-math.html">
<a href="2-07-0-logic-and-more-math.html">
Logic and Advanced Math
</a>
</li>
<li class="chapter " data-level="4.1.8" data-path="2-08-0-number-theory.html">
<a href="2-08-0-number-theory.html">
Extra Credit: Number Theory
</a>
</li>
<li class="chapter " data-level="4.1.9" data-path="2-09-0-binary-octets-bits.html">
<a href="2-09-0-binary-octets-bits.html">
Binary Streams, Octet-Vectors, and Bit-Vectors
</a>
</li>
<li class="chapter " data-level="4.1.10" data-path="2-10-0-improved-text-adventure-engine.html">
<a href="2-10-0-improved-text-adventure-engine.html">
Extra Credit: An Improved Text Adventure Engine
</a>
</li>
<li class="chapter " data-level="4.1.11" data-path="2-11-0-conditions.html">
<a href="2-11-0-conditions.html">
Conditions and Error Handling
</a>
</li>
<li class="chapter " data-level="4.1.12" data-path="2-12-0-2d-game.html">
<a href="2-12-0-2d-game.html">
Extra Credit: Write a 2D Game
</a>
</li>
<li class="chapter " data-level="4.1.13" data-path="2-13-0-compiler.html">
<a href="2-13-0-compiler.html">
The Compiler
</a>
</li>
<li class="chapter " data-level="4.1.14" data-path="2-14-0-tree-shaker.html">
<a href="2-14-0-tree-shaker.html">
Extra Credit: Write a Tree-Shaker
</a>
</li>
<li class="chapter " data-level="4.1.15" data-path="2-15-0-docs-and-inspection.html">
<a href="2-15-0-docs-and-inspection.html">
Documentation and Inspection
</a>
</li>
<li class="chapter " data-level="4.1.16" data-path="2-16-0-foreign-libs.html">
<a href="2-16-0-foreign-libs.html">
Extra Credit: Foreign Libraries in Lisp
</a>
</li>
<li class="chapter " data-level="4.1.17" data-path="2-17-0-debugging-testing.html">
<a href="2-17-0-debugging-testing.html">
Debugging and Unit Testing
</a>
</li>
<li class="chapter " data-level="4.1.18" data-path="2-18-0-ffi.html">
<a href="2-18-0-ffi.html">
Extra Credit: Write a Foreign Function Interface
</a>
</li>
<li class="chapter " data-level="4.1.19" data-path="2-19-0-essential-libs.html">
<a href="2-19-0-essential-libs.html">
Essential Lisp Libraries
</a>
</li>
<li class="chapter " data-level="4.1.20" data-path="2-20-0-packaging-libs.html">
<a href="2-20-0-packaging-libs.html">
Extra Credit: Packaging Lisp Libraries
</a>
</li>
<li class="chapter " data-level="4.1.21" data-path="2-21-0-review.html">
<a href="2-21-0-review.html">
Detailed Syntax Review
</a>
</li>
</ul>
</li>
<li class="header">PART THREE</li>
<li class="chapter " data-level="5.1" data-path="3-00-00-overview.html">
<a href="3-00-00-overview.html">
Lisp So(u)rcery
</a>
<ul class="articles">
<li class="chapter " data-level="5.1.1" data-path="3-01-00-web-apps.html">
<a href="3-01-00-web-apps.html">
Real-world Web Apps
</a>
</li>
<li class="chapter " data-level="5.1.2" data-path="3-02-00-typesetting.html">
<a href="3-02-00-typesetting.html">
Typesetting
</a>
</li>
<li class="chapter " data-level="5.1.3" data-path="3-03-00-mobile.html">
<a href="3-03-00-mobile.html">
Native Mobile Applications
</a>
</li>
<li class="chapter " data-level="5.1.4" data-path="3-04-00-gui.html">
<a href="3-04-00-gui.html">
Cross-platform Desktop Applications
</a>
</li>
<li class="chapter " data-level="5.1.5" data-path="3-05-00-system-utils.html">
<a href="3-05-00-system-utils.html">
Drivers, Daemons, and System-Utilities
</a>
</li>
<li class="chapter " data-level="5.1.6" data-path="3-06-00-reverse-engineering.html">
<a href="3-06-00-reverse-engineering.html">
Reverse Engineering
</a>
</li>
<li class="chapter " data-level="5.1.7" data-path="3-07-00-graphics.html">
<a href="3-07-00-graphics.html">
Graphics Rendering
</a>
</li>
<li class="chapter " data-level="5.1.8" data-path="3-08-00-gaming.html">
<a href="3-08-00-gaming.html">
OpenGL, SDL, and 3D Game Development
</a>
</li>
<li class="chapter " data-level="5.1.9" data-path="3-09-00-audio.html">
<a href="3-09-00-audio.html">
Audio Generation and Manipulation
</a>
</li>
<li class="chapter " data-level="5.1.10" data-path="3-10-00-data.html">
<a href="3-10-00-data.html">
Data Aggregation and Analysis
</a>
</li>
<li class="chapter " data-level="5.1.11" data-path="3-11-00-cryptosec.html">
<a href="3-11-00-cryptosec.html">
Cryptography and Security
</a>
</li>
<li class="chapter " data-level="5.1.12" data-path="3-12-00-fintech.html">
<a href="3-12-00-fintech.html">
Financial Software and Crypto-Currencies
</a>
</li>
<li class="chapter " data-level="5.1.13" data-path="3-13-00-scientific-computing.html">
<a href="3-13-00-scientific-computing.html">
Scientific Computing
</a>
</li>
<li class="chapter " data-level="5.1.14" data-path="3-14-00-computational-physics.html">
<a href="3-14-00-computational-physics.html">
Computational Physics
</a>
</li>
<li class="chapter " data-level="5.1.15" data-path="3-15-00-quantum-computing.html">
<a href="3-15-00-quantum-computing.html">
Quantum Computing
</a>
</li>
<li class="chapter " data-level="5.1.16" data-path="3-16-00-nlp.html">
<a href="3-16-00-nlp.html">
Natural Language Processing
</a>
</li>
<li class="chapter " data-level="5.1.17" data-path="3-17-00-ai.html">
<a href="3-17-00-ai.html">
Artificial Intelligence
</a>
</li>
<li class="chapter " data-level="5.1.18" data-path="3-18-00-robotics.html">
<a href="3-18-00-robotics.html">
Robotics
</a>
</li>
<li class="chapter " data-level="5.1.19" data-path="3-19-00-space-tech.html">
<a href="3-19-00-space-tech.html">
Space Tech
</a>
</li>
<li class="chapter " data-level="5.1.20" data-path="3-20-00-neurotech.html">
<a href="3-20-00-neurotech.html">
Neuroscience and Thought-Controlled Computing
</a>
</li>
<li class="chapter " data-level="5.1.21" data-path="3-21-00-lispos.html">
<a href="3-21-00-lispos.html">
A Simple LispOS
</a>
</li>
<li class="chapter " data-level="5.1.22" data-path="3-22-00-lisp-machine.html">
<a href="3-22-00-lisp-machine.html">
Build Your Own Lisp Machine
</a>
</li>
<li class="chapter " data-level="5.1.23" data-path="3-23-00-gov-mil.html">
<a href="3-23-00-gov-mil.html">
Government and Military Grade Systems
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="index.html" >Common Lisp Style Guide</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="exercise-117">Exercise 1.1.7</h1>
<h2 id="common-lisp-style-guide">Common Lisp Style Guide</h2>
<p>Style is fundamental to programming&#x2014;particularly in Lisp, where the unlimited power and flexibility of paradigm can easily go to your head. A little carelessness can make for sloppy, illegible code; but if you make a point to consider your code as an art-form as well as a tool to get a job done, you, and anyone else who has to read your code later, will thank you for it.</p>
<p>But don&apos;t make the mistake of thinking these rules are prescriptive; and not everyone in the Lisp community agrees with them. Consider them more like practical suggestions which will get you thinking about style, and then you and your team can decide together which style rules you want to follow for your coding aesthetic.</p>
<p>At this stage of learning Lisp, you may not fully appreciate all the style rules given here, or understand everything being discussed. And that&apos;s okay. Take a deep breath, and familiarize yourself with these points to the best of your ability, and then treat this chapter like a reference; it will mean more and more after each chapter of this book you work through, and will be something you can keep referring to with each Lisp project.</p>
<h3 id="symbols-and-naming">Symbols and Naming</h3>
<p>Clear and meaningful symbol-names in your Lisp programs are one of the most important aspects of code readability.</p>
<p>While there are no hard-fast rules for naming symbols in the Common Lisp standard, beyond a list of which characters are allowed, certain conventions have arisen in the Lisp community which you should know, and usually follow religiously. There are times when you can, and may have to, break convention, but those are special cases which require experience and finesse.</p>
<p>Symbol names should be descriptive, short, typed in all lowercase, with words separated by a single hyphen:</p>
<pre><code class="lang-lisp">(defun my-addition-function (&amp;rest rest)
(apply #&apos;+ rest))
=&gt; MY-ADDITION-FUNCTION
(deftype my-integer-type ()
&apos;(and integer
(satisfies plusp)))
=&gt; MY-INTEGER-TYPE
(defvar my-hash-table (make-hash-table :test &apos;equal))
=&gt; MY-HASH-TABLE
(defvar my-alist &apos;((&quot;one&quot; . 1)
(&quot;two&quot; . 2)
(&quot;three&quot; . 3)))
=&gt; MY-ALIST
;; Note that symbol names created from strings should be in all-caps:
(intern &quot;MY-NEW-INTERNED-SYMBOL&quot;)
=&gt; MY-NEW-INTERNED-SYMBOL
NIL
;; or you&apos;ll have to reference it in surrounding hbars as a literal, case-sensitive symbol:
(intern &quot;my-funky-interned-symbol&quot;)
=&gt; |my-funky-interned-symbol|
NIL
</code></pre>
<p>Note that a pair of hbars is <em>syntactic</em>, not stylistic. It allows you, among other things, to define case-sensitive symbols; but it is generally only used for foreign-function interfaces.</p>
<p>Global variables, <em>i.e.</em>, variables declared as top-level forms with <code>defvar</code> or <code>defparameter</code>, are named using &quot;earmuffs&quot; because they are dynamic and special, such as the following built into Common Lisp:</p>
<pre><code class="lang-lisp">*package*
=&gt; #&lt;PACKAGE <span class="hljs-string">&quot;COMMON-LISP-USER&quot;</span>&gt;
*print-base*
=&gt; <span class="hljs-number">10</span>
</code></pre>
<p>In the case of earmuffs, these <em>are</em> stylistic. They are not parsed as syntactic tokens by the Lisp reader, they are simply read as part of the symbol name.</p>
<p>Another stylistic convention for symbol names uses a pair of plus-signs to wrap symbol-names of <em>constants</em>:</p>
<pre><code class="lang-lisp">(<span class="hljs-name">defconstant</span> +my-new-constant+ <span class="hljs-number">1.0</span>)
</code></pre>
<p>Package internal symbols are sometimes named with a prepended percent-sign, and not exported with the package API. These are similar in purpose to private functions, methods, and variables in other programming languages---only they can always be accessed by using the full symbol name:</p>
<pre><code class="lang-lisp">(<span class="hljs-name">defpackage</span> #<span class="hljs-symbol">:my-new-package</span>
(<span class="hljs-symbol">:use</span> <span class="hljs-symbol">:cl</span>)
(<span class="hljs-symbol">:export</span> #<span class="hljs-symbol">:mad-adder</span>))
(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:my-new-package</span>)
<span class="hljs-comment">;; Do some wonky stuff with a package-internal function</span>
(<span class="hljs-name">defun</span> %madder (<span class="hljs-name">x</span>)
(<span class="hljs-name">declare</span> (<span class="hljs-name">integer</span> x))
(<span class="hljs-name">apply</span> #&apos;+ (<span class="hljs-name">loop</span> for i from <span class="hljs-number">1</span> upto x
collect (<span class="hljs-name">*</span> x i))))
<span class="hljs-comment">;; Write an exported interface to your package internal function</span>
(<span class="hljs-name">defun</span> mad-adder (<span class="hljs-name">x</span>)
<span class="hljs-string">&quot;Call %MADDER with integer argument X.&quot;</span>
(%madder x))
(<span class="hljs-name">in-package</span> <span class="hljs-symbol">:cl-user</span>)
(<span class="hljs-name">my-new-package</span><span class="hljs-symbol">:mad-adder</span> <span class="hljs-number">10</span>)
(<span class="hljs-name">my-new-package</span>::%madder <span class="hljs-number">10</span>)
</code></pre>
<p>Predicate functions, <em>i.e.</em>, boolean tests, typically end with a suffixed &quot;p&quot;. If it is a multi-word symbol already separated by dashes, you append the suffix as &quot;-p&quot; (dash-p); while if it is a single word or mnemonic symbol name, the &quot;p&quot; can be appended without a dash.</p>
<pre><code class="lang-lisp">(<span class="hljs-name">bit-vector-p</span> #*01010001)
=&gt; T
(<span class="hljs-name">integerp</span> <span class="hljs-number">10</span>)
=&gt; T
</code></pre>
<h3 id="parentheses-indentation-and-whitespace">Parentheses, Indentation, and Whitespace</h3>
<p>Parentheses should all close on the same line, when ending multiple forms; while balancing parentheses on separate lines from your code, to demarcate a code block, is common practice in other languages, it is not the case in Common Lisp. If you are having trouble matching the parentheses in your head, use a source-code editor which highlights matching opening or closing parens. And since in Lisp, all non-atomic forms are lists, and wrapped in a pair of parentheses, it is also worthwhile to use tools to find unmatched parens.</p>
<pre><code class="lang-lisp"><span class="hljs-comment">;; bad style</span>
(<span class="hljs-name">defun</span> a-badly-formatted-function (<span class="hljs-name">x</span> y z)
(<span class="hljs-name">progn</span>
(<span class="hljs-name">setq</span> x (<span class="hljs-name">+</span> x x))
(<span class="hljs-name">setq</span> y (<span class="hljs-name">*</span> y y))
(<span class="hljs-name">mod</span>
(<span class="hljs-name">+</span> z z)
(<span class="hljs-name">+</span> x y)
)
)
)
<span class="hljs-comment">;; the right way</span>
(<span class="hljs-name">defun</span> a-pretty-function (<span class="hljs-name">x</span> y z)
<span class="hljs-string">&quot;Function definitions need docstrings.&quot;</span>
(<span class="hljs-name">declare</span> (<span class="hljs-name">integer</span> x y z))
(<span class="hljs-name">let</span> ((<span class="hljs-name">x2</span> (<span class="hljs-name">+</span> x x))
(<span class="hljs-name">y2</span> (<span class="hljs-name">*</span> y y)))
(<span class="hljs-name">mod</span> (<span class="hljs-name">*</span> z z) (<span class="hljs-name">+</span> x2 y2))))
</code></pre>
<p>If you have trouble getting Lisp style right, you can always run your code through the Pretty Printer to see how Lisp thinks it should be formatted.</p>
<p>If you use Emacs+SLIME (and many Lisp Hackers will tell you that you must), proper indentation is handled quite well; other editors, such as Sublime Text, need a plugin installed to indent Lisp code, but don&apos;t always do it right---so it&apos;s important to know how to manually indent your code.</p>
<p>Indentation in Lisp uses the space character, not Tabs. <em>Bodies</em> of expressions are indented by two spaces from their parent form, while paramaters and list members can be lined up into columns when they take up too many characters to fit on an ideal line of code. For example:</p>
<pre><code class="lang-lisp"><span class="hljs-comment">;; IF is a special operator, and doesn&apos;t have a body expression</span>
(<span class="hljs-name">if</span> <span class="hljs-literal">t</span> <span class="hljs-literal">t</span> <span class="hljs-literal">nil</span>)
<span class="hljs-comment">;; but you&apos;ll normally need to split the form for clarity; see how the three parameters line up?</span>
(<span class="hljs-name">if</span> <span class="hljs-literal">t</span>
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;Then: True~%&quot;</span>)
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;Else: False~%&quot;</span>))
<span class="hljs-comment">;; WHEN does have a body expression though, so its body isn&apos;t lined up with the test-form parameter</span>
(<span class="hljs-name">when</span> <span class="hljs-literal">t</span>
(<span class="hljs-name">format</span> <span class="hljs-literal">t</span> <span class="hljs-string">&quot;This is true, too!&quot;</span>))
</code></pre>
<p>You only need a single space character or line-break between forms in a list. Likewise, you only need one extra line between top-level forms. Extraneous whitespace makes your code more difficult for others to read.</p>
<p>You should also avoid the temptation to use the Tab character inside a form or list to force a table-like structure onto your code. This isn&apos;t FORTRAN, it&apos;s Lisp---and Lisp should <em>flow</em>.</p>
<pre><code class="lang-lisp"><span class="hljs-comment">;; a badly formatted class definition</span>
(<span class="hljs-name">defclass</span> march-hare ()
((<span class="hljs-name">name</span> <span class="hljs-symbol">:type</span> string <span class="hljs-symbol">:initarg</span> <span class="hljs-symbol">:name</span> <span class="hljs-symbol">:initform</span> <span class="hljs-string">&quot;Haigha&quot;</span> <span class="hljs-symbol">:accessor</span> name)
(<span class="hljs-name">tea-time-p</span> <span class="hljs-symbol">:type</span> boolean <span class="hljs-symbol">:initarg</span> <span class="hljs-symbol">:tea-time-p</span> <span class="hljs-symbol">:initform</span> <span class="hljs-literal">t</span> <span class="hljs-symbol">:accessor</span> tea-time-p)
(<span class="hljs-name">tie</span> <span class="hljs-symbol">:type</span> string <span class="hljs-symbol">:initarg</span> <span class="hljs-symbol">:tie</span> <span class="hljs-symbol">:initform</span> <span class="hljs-string">&quot;bow-tie&quot;</span> <span class="hljs-symbol">:accessor</span> tie)))
<span class="hljs-comment">;; the right way</span>
(<span class="hljs-name">defclass</span> march-hare ()
((<span class="hljs-name">name</span> <span class="hljs-symbol">:type</span> string <span class="hljs-symbol">:initarg</span> <span class="hljs-symbol">:name</span> <span class="hljs-symbol">:initform</span> <span class="hljs-string">&quot;Haigha&quot;</span> <span class="hljs-symbol">:accessor</span> name
<span class="hljs-symbol">:documentation</span> <span class="hljs-string">&quot;The name of the March Hare.&quot;</span>)
(<span class="hljs-name">tea-time-p</span> <span class="hljs-symbol">:type</span> boolean <span class="hljs-symbol">:initarg</span> <span class="hljs-symbol">:tea-time-p</span> <span class="hljs-symbol">:initform</span> <span class="hljs-literal">t</span> <span class="hljs-symbol">:accessor</span> tea-time-p
<span class="hljs-symbol">:documentation</span> <span class="hljs-string">&quot;Whether or not it&apos;s tea-time.&quot;</span>)
(<span class="hljs-name">tie</span> <span class="hljs-symbol">:type</span> string <span class="hljs-symbol">:initarg</span> <span class="hljs-symbol">:tie</span> <span class="hljs-symbol">:initform</span> <span class="hljs-string">&quot;bow-tie&quot;</span> <span class="hljs-symbol">:accessor</span> tie
<span class="hljs-symbol">:documentation</span> <span class="hljs-string">&quot;The style of tie the March Hare is wearing.&quot;</span>))
(<span class="hljs-symbol">:documentation</span> <span class="hljs-string">&quot;&apos;The March Hare will be much the most interesting, and perhaps as this is May it won&apos;t be raving mad---at least not so mad as it was in March.&apos; -- Lewis Carroll&quot;</span>))
</code></pre>
<h3 id="comments-and-documentation">Comments and Documentation</h3>
<p>You should always comment and document your code; that being said, Lisp, when written correctly, can be effectively self-documenting and self-explanatory (see &quot;Decomposition, Refactoring, and Abstraction&quot; below for more information). Your code should be clean and readable enough that comments and documentation are technically unnecessary, but then write clean, short, and to-the-point comments and documentation strings to expand on what the code itself already says.</p>
<p>Conventions for comments in Lisp are pretty loose, but if you read enough Lisp source-code, you&apos;ll find the following general pattern:</p>
<pre><code class="lang-lisp"><span class="hljs-comment">;;;; four preceding semi-colons for file headers, copyright and license information</span>
<span class="hljs-comment">;;; three preceding semi-colons for descriptive text</span>
<span class="hljs-comment">;; two preceding semi-colons for commentary on the immediately following text</span>
<span class="hljs-comment">; one preceding semi-colon for in-line comments, inside code blocks</span>
</code></pre>
<p>You should always include meaningful docstrings in your object definition forms---even in your global variables. In class definition forms, you can add docstrings to each slot of the class, the class itself, and each of the generic functions and methods. Lack of documentation, or documentation that has fallen out of sync with the codebase, is considered a bug just as much as code that does not work as expected.</p>
<h3 id="decomposition-refactoring-and-abstraction">Decomposition, Refactoring, and Abstraction</h3>
<p>Your Lisp code should be so clear and focused that, whenever possible, even a non-programmer can read and understand it. This is more difficult than you might expect, as it requires careful thought and planning to achieve successfully.</p>
<p>The general idea is to divide your code up into the smallest reusable pieces; if, for example, you were to write a new summation function, and it contained logic for sorting a list of numbers, instead of writing the logic for sorting a list in-line, inside the summation function, you could write a separate list-sorting function, and call it inside the summation function. This offers three immediate benefits: you get a new, generally useful function almost for free; the logic of your summation function is more tightly focused on the task it was designed for; and the code becomes easier to read and maintain. You can then improve the implementation of either function without worrying about the other.</p>
<p>Oftentimes, as you work on software, you&apos;ll find repetitive code-patterns that become cumbersome to write, and may all need to be changed separately during maintenance, refactoring, or major upgrades. Common Lisp, through the most-favoured Emacs+SLIME development environment, is highly interactive, so it supports incremental, functional development. Whenever you come across redundant patterns, it is in your interest to abstract away those code patterns into a macro, which at compile-time will be transformed into the code you need at run-time. This technique also allows you to do some pretty awesome stuff, like intelligently creating interfaces derived from a dynamic data-set; but you should get into the habit of this style of programming, because it will make your code cleaner and easier to manage over time.</p>
<h3 id="functions-macros-and-methods">Functions, Macros, and Methods</h3>
<p>Sometimes it can be difficult to decide when, in your software, to use a function, macro, or method and CLOS class for a specific problem. Again, since Lisp offers so many options for choice of paradigm, you can program in any number of styles, and even mix-and-match paradigms in the same source-code file without trouble; but this rampant mix-and-matching can create unnecessary frustration for maintenance, debugging, and upgrading; so it&apos;s useful to pick a dominant paradigm and stick to it.</p>
<p>That being said, if you want to take full advantage of Common Lisp&apos;s multi-paradigm toolset, you can do so sanely. It is simply a matter of deciding up-front how you&apos;re going to organize your code and express your logic.</p>
<p>A multi-paradigm approach to programming that I have found useful is straightforward, logically consistent, and in keeping with the design goals of the Common Lisp language:</p>
<ul>
<li>Separate application tasks into computation, transformation, and statefulness.</li>
<li>Decompose all application logic into pure, clearly-defined functions.</li>
<li>Use macros for monadic-style I/O in pure functional programs; object-oriented programs are designed with I/O in mind.</li>
<li>When writing functions, program functionally. Avoid writing functions that cause side-effects or are destructive, in-place operations; but if you have to, name them appropriately so that it is obvious.</li>
<li>Use macros when abstracting. The point of macros is compile-time code-transformation. Your macros will be more generally useful when you decompose logic from them into pure functions, and use them for their intended purpose.</li>
<li>When you need to track state, and thus cause side-effects, you should use CLOS classes and methods&#x2014;they were designed with that purpose in mind.</li>
</ul>
<h3 id="cross-platform-development">Cross-Platform Development</h3>
<p>Lisp is, by nature, a cross-platform development language; but each implementation of Common Lisp contains implementation-dependent extensions, and platform-specific functionality that is unavoidable. Generally speaking, you should make every effort to write cross-platform, cross-implementation software, so that it &quot;just works&quot; everywhere. When a problem requires implementation or platform specific code to solve, take the extra five minutes to find out how to make it work on other platforms. If you need to, refer to the source code of other popular cross-platform libraries, to see how they approach the problem.</p>
<p>As an example, you can and should write software that takes advantage of 64-bit architectures; but even today, you cannot always count on it being there for your users. Your software should scale gracefully to 32-bit architectures. And when certain features require it, document them as such and provide an alternative.</p>
<p>The same is true for web applications. You can&apos;t predict which browser your users will prefer; and users resent getting a page that tells them to upgrade or change their browser. You have no choice but to support every browser and platform&#x2014;so, think about Lisp software the same way, and you will be doing yourself and your users a favor from the beginning.</p>
<p>Be certain to test your code on every platform you have access to, and in multiple implementations of Lisp for each platform. It may seem like a lot of extra work for nothing at first, but it will save you a lot of time down the road.</p>
<h3 id="libraries">Libraries</h3>
<p>As well as learning the Common Lisp language, it is important to familiarize yourself and keep up to date with the wide variety of Lisp libraries available on-line, particularly those in the Quicklisp package repository. There are two important reasons for this:</p>
<ul>
<li>Instead of reinventing the wheel, you can use a feature-complete, dedicated library to implement your functionality.</li>
<li>When you know what libraries are freely available, you can dedicate more of your time to writing and testing new code.</li>
</ul>
<p>Also, whenever it is both possible and reasonable, the code you write should be packaged into logical units, and distributed as open-source libraries that are loadable through Quicklisp. Depending on the type of company you work for, however, this may be more difficult than you might expect; most software development shops like to control all IP produced by their employees, even code produced in their off-hours&#x2014;if you have a contract, be sure to check with your manager first before you release any software you write into the wild; and in the future, keep an eye out for such clauses and be certain to fight for your right to contribute to open-source software.</p>
<p>Knowing what code to push up from a software project into a general purpose library can be tricky, but there are a few rules you can keep in mind:</p>
<ul>
<li>If you find yourself copy and pasting code between projects.</li>
<li>If you abstract a common problem into a cleaner syntax.</li>
<li>If your code solves a known problem or lack in the Lisp community, that is too general for a specific commercial software project.</li>
</ul>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="1-01-06-prefix-notation.html" class="navigation navigation-prev " aria-label="Previous page: Prefix Notation">
<i class="fa fa-angle-left"></i>
</a>
<a href="1-01-08-configuration.html" class="navigation navigation-next " aria-label="Next page: Configuring Your Development Environment">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Common Lisp Style Guide","level":"3.1.1.7","depth":3,"next":{"title":"Configuring Your Development Environment","level":"3.1.1.8","depth":3,"path":"1-01-08-configuration.md","ref":"./1-01-08-configuration.md","articles":[]},"previous":{"title":"Prefix Notation","level":"3.1.1.6","depth":3,"path":"1-01-06-prefix-notation.md","ref":"./1-01-06-prefix-notation.md","articles":[]},"dir":"ltr"},"config":{"plugins":["hints","folding-chapters"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"hints":{"danger":"fa fa-exclamation-circle","info":"fa fa-info-circle","tip":"fa fa-mortar-board","working":"fa fa-wrench"},"folding-chapters":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"\"the Phoeron\" Colin J.E. Lupton","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"index.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Learn Lisp The Hard Way","gitbook":"*"},"file":{"path":"1-01-07-style-guide.md","mtime":"2022-08-22T22:13:05.971Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-08-24T16:15:01.957Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-folding-chapters/folding-chapters.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>