These are notes on good coding style, both general and specific to Scheme. They will be updated through the semester.
The names you give to functions, their arguments, and other variables
should be as short as possible while being clear about their meaning.
l can be a reasonable mnemonic for
a list that has no other identifying properties, but if the list is
intended to contain pairs of (x, y) coordinates, a
better name would be
Scheme programmers follow some naming conventions:
joining_with_underscores, in Scheme we
#f– should have names that end with a question mark, e.g.,
*max-length*, and destructive functions should have names that end with exclamation marks, e.g.,
No line should span more than 80 characters.
A general guideline is “one task, one function”. Ideally no function should span more than five to eight lines.
(define f (lambda (l) (cond ((empty? l) 0) (else (f (rest l))))))
(define f (lambda (l) (cond ((empty? l) 0) (else (f (rest l))) ) ) )
Indenting makes it easier to read your code by exposing its structure.
The general rule is to indent by two spaces for each level of nesting,
though there are conventional exceptions. It’s fine to let DrRacket
indent your code for you by typing
Ctrl-I. If the result
looks wrong, your code probably doesn’t say what you think it
Based in part on notes by Marc Smith.