02-design-post3up

# 02-design-post3up - The design recipe Readings: HtDP,...

This preview shows pages 1–5. Sign up to view the full content.

The design recipe Readings: HtDP, sections 1-5 (ordering of topics is different in lectures, different examples will be used) Survival, Style, and Submission Guides CS 135 Fall 2009 02: The design recipe 1 Programs as communication Every program is an act of communication: Between you and the computer Between you and yourself in the future Between you and others Human-only comments in Scheme programs: from a semicolon ( ; ) to the end of the line. CS 135 Fall 2009 02: The design recipe 2 Some goals for software design Programs should be: compatible, composable, correct, durable, ef±cient, extensible, ²exible, maintainable, portable, readable, reliable, reusable, scalable, usable, and useful. CS 135 Fall 2009 02: The design recipe 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The design recipe Use it for every function you write in CS 135. A development process that leaves behind written explanation of the development Results in a trusted (tested) function which future readers (you or others) can understand CS 135 Fall 2009 02: The design recipe 4 The fve design recipe components Contract: Describes what type of arguments the function consumes and what type of value it produces. Purpose: Describes what the function is to compute. Examples: Illustrating the use of the function. Defnition: The Scheme de±nition of the function. Tests: A representative set of inputs and expected outputs. CS 135 Fall 2009 02: The design recipe 5 Using the design recipe We’ll write a function which squares two numbers and sums the results. Mathematically: sum-of-squares: R × R R ;; sum-of-squares: num num num ;; Purpose: produces sum of squares of arg1 and arg2 ;; Examples: ( check-expect ( sum-of-squares 3 4 ) 25 ) ( check-expect ( sum-of-squares 0 2.5 ) 6.25 ) ( de±ne ( sum-of-squares arg1 arg2 ) ( + ( arg1 arg1 )( arg2 arg2 ))) CS 135 Fall 2009 02: The design recipe 6
Contracts We will be more careful than HtDP. num : any Scheme numeric value int : restriction to integers nat : restriction to natural numbers (including 0) any : any Scheme value We will see more types soon. CS 135 Fall 2009 02: The design recipe 7 Tests Examples should be written before the code body. Tests should be written after the code body. Tests don’t need to be “big”. In fact, they should be small and directed. The number of tests and examples needed is a matter of judgement. Do not ±gure out the expected answers to your tests by running your program! Always work them out by hand . CS 135 Fall 2009 02: The design recipe 8 The teaching languages offer a convenient testing method. ( check-expect ( sum-of-squares 3 4 ) 25 ) ( check-within ( sqrt 2 ) 1.414 .001 ) ( check-error ( / 1 0 ) " /: division by zero " ) Tests written using these functions are saved and evaluated at the very end. This means that examples can be written as code. CS 135 Fall 2009 02: The design recipe 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
;; sum-of-squares: num num num ;; Purpose: produces sum of squares of arg1 and arg2 ;; Examples: ( check-expect ( sum-of-squares 3 4 ) 25 ) ( check-expect ( sum-of-squares 0 2.5 ) 6.25 ) ( deFne ( sum-of-squares arg1 arg2 ) ( + ( arg1 arg1 )( arg2 arg2 ))) ( check-expect ( sum-of-squares 0 0 ) 0 ) ( check-expect ( sum-of-squares 2 7 ) 53 )
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/12/2010 for the course CS 135 taught by Professor Vasiga during the Fall '07 term at Waterloo.

### Page1 / 16

02-design-post3up - The design recipe Readings: HtDP,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online