In addition to the basic structure provided by the design recipes, there are a number of details that matter when designing programs. These rules, or style conventions, are common through the world of software development. Different programming languages, communities, organizations, and individual projects have their own conventions and champions. And so it is with our course; this is the set of style conventions that we ask you to follow.
local
construct.;; HHState -> HHState
(define (hungry-henry-action-per-clock-tick hh-world-state)
(bump-tick-value
(eat-all-cupcakes-in-reach
(move-hungry-henry-closer-to-wp hh-world-state))))
;; HHState -> HHState
(define (bump-tick-value hh-world-state)
...)
;; HHState -> HHState
(define (eat-all-cupcakes-in-reach hh-world-state)
...)
;; HHState -> HHState
(define (move-hungry-henry-closer-to-wp hh-world-state)
...)
check-expect
. Otherwise, delete them.(cond [(> x 0) (sqrt x)] [(= x 0) 0] [(< x 0) (make-error "Negative input")])
(cond [(> x 0) (sqrt x)]
[(= x 0) 0]
[(< x 0) (make-error "Negative input")])
cond
case on separate lines from the tests.)Ctrl-i
(or Cmd-i
on OSX). If the result looks wrong, your code probably doesn’t say what you think it does, and you should check your parentheses.😀 Good:
(define (foo n)
(cond [(odd? n) 1]
[(even? n) 2]))
😫 Bad:
(define(foo n)
(cond[ (odd? n)1]
[ (even? n)2]))
😀 Good:
(define (f l)
(cond [(empty? l) 0]
[else (f (rest l))]))
😫 Bad:
(define (f l)
(cond [(empty? l) 0]
[else (f (rest l))]
)
)
Use concise conditionals.
Similarly, do not use an if
or cond
expression to evaluate an expression and return true or false. For instance,
(if (= x 42) #true #false)
and
(cond [(= x 42) #true]
[else #false])
are both redundant ways of writing (= x 42)
.
;; double : Number -> Number
;; Multiplies a number n by 2
;; draw-world : WorldState -> Image
;; WorldState -> Image
WorldState
rather than world-state
, world_state
or worldState
. This keeps them distinct from the lowercase-with-hyphens names used for functions and variables and UPPER-WITH-HYPHENS for constants, discussed in the rules below.count
or cnt
.lst
is 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
.join-words-with-hyphens
, rather than using mixedCaseVariables
or joining_with_underscores
when naming functions, constants, and variables; e.g., posn-x
or tock-ball
.?
, e.g., increasing-values?
.empty?
have question marks. Only the most basic mathematical predicates like >
or =
omit the question mark.)MAX-LENGTH
.😀 Good:
;; greet : String -> String
;; Produce a greeting by adding "Hello" before a string.
(define (greet str)
(string-append "Hello " str))
(check-expect (greet "world!") "Hello world!")
(check-expect (greet "and goodbye") "Hello and goodbye")
(check-expect (greet "darkness, my old friend")
"Hello darkness, my old friend")
😫 Bad:
; string -> string
; (greet "foo") produces "Hello foo"
(define (greet o)
;; put "Hello " before string
(string-append "Hello " o)
)
(check-expect (greet "World!") "Hello World!")
(check-expect (greet "goodbye")"Hello goodbye")
(check-expect(greet "darkns") "Hello darkns")
;(define (accio o) ; stub
; "a")
(greet "World!")
(greet "goodbye")
(greet "sddklfkldfakl")
Mostly based on notes by Jonathon Gordon who in turn credits John Hughes, Marc Smith, and Gregor Kiczales.