CMPU 101 § 55

Coding Style

These are notes on good coding style, both general and specific to Scheme. They will be updated through the semester.

Use good names

The names you give to functions, their arguments, and other variables should be as short as possible while being clear about their meaning. E.g., lst or 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 coords.

Scheme programmers follow some naming conventions:

Keep lines short

No line should span more than 80 characters.

Design concise functions

A general guideline is “one task, one function”. Ideally no function should span more than five to eight lines.

Don’t dangle parentheses

Good:

(define f
  (lambda (l)
    (cond ((empty? l) 0)
          (else (f (rest l))))))

Bad:

(define f
  (lambda (l)
    (cond ((empty? l) 0)
          (else (f (rest l)))
    )
  )
)

Indent consistently

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 does!

Acknowledgments

Based in part on notes by Marc Smith.