Cat Exercises Self Review

In the style of "you might be a redneck if..."

You might be on the wrong track if ...

... your functions use cat-1

You're on the wrong track if cat-1 appears anywhere in your code except in

All your functions that deal with cats should be passed either a cat parameter containing one cat, or a cats parameter, containing a list of cats. The code inside the function should use the value in the parameter passed in, not cat-1.

... your functions call list

You're on the wrong track if list is called anywhere in your code except

In particular, anything of the form (list (car cats)) as an argument to any function is probably wrong. Ditto anything that looks like (list value some-recursive-call). The function to use in a recursive loop to build a list is cons, as shown in the basic list recursion pattern.

You might have a maintenance problem if ...

... you call a list of cats cat

Look at your parameter names. In particular, look at the ones called cat. Do any of them contain a list of cats? Then you have a maintenance problem.

A variable name should always accurately describe what the variable contains.

... you use names like thing1 and thing2

Do you have one or more variable or function names that are the same except for a digit at the end? Then you have a maintenance problem.

Names should clearly say how one variable differs from another, or one function from another. A digit doesn't do that.

... you say click-cat when you mean cat-clicked?

A function name says what a function returns or does. The name click-cat says the function clicks a cat, whatever that might mean. The name cat-clicked? on the other hand says the function returns true if the cat was clicked.

... you have any function longer than 6 lines or so

The difficulty of reading, understanding, and maintaining a function gets much harder once you pass half a dozen lines or so. Which code below is clearer about what happens when you press the "c" key or the "p" key? Which code would support alternative methods of petting a cat, like clicking on it?

(define (handle-key cat key)
  (cond ((key=? key "c") 
         (new-cat ...
                  ...
                  ...
                  ...
                  ...))
        ((key=? key "p")
         (new-cat ...
                  ...
                  ...
                  ...
                  ...))
        (else cat)))
(define (handle-key cat key)
  (cond ((key=? key "c") (turn-cat cat))
        ((key=? key "p") (pet-cat cat))
        (else cat)))

(define (turn-cat cat)
  (new-cat ...
           ...
           ...
           ...
           ...))

(define (pet-cat cat)
  (new-cat ...
           ...
           ...
           ...
            ..))

Valid HTML 4.01 Strict