Contracts, Purpose Statements, Examples and Tests

Similar documents
Observer Templates. CS 5010 Program Design Paradigms Lesson 1.4

The Data Design Recipe

Halting Measures and Termination Arguments

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

More About Recursive Data Types

Working with images and scenes

ormap, andmap, and filter

Using Inheritance to Share Implementations

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

Trees. CS 5010 Program Design Paradigms Lesson 6.2

Model-View-Controller Architecture

Rewriting your function using map and foldr

Solving Your Problem by Generalization

Basics of Inheritance

CS 5010 Program Design Paradigms Lesson 6.1

From Templates to Folds

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5

Case Study: Undefined Variables

Case Study: Free Variables

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

Module 3: New types of data

Repetition Through Recursion

Doing it in Java. CS 5010 Program Design Paradigms Bootcamp Lesson 5.4

CSE / ENGR 142 Programming I

CS2500 Exam 2 Fall 2011

Module 8: Local and functional abstraction

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

The Design Recipe Fall 2017

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

Homework: Simple functions and conditionals

Functions that return lists

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

4. Java Project Design, Input Methods

CS115 - Module 4 - Compound data: structures

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Working with recursion

Shapes leading to CAD system project

The Design Recipe Fall 2018

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Binary Search. CS 5010 Program Design Paradigms Bootcamp Lesson 8.2

Lab 7: OCaml 12:00 PM, Oct 22, 2017

II. Functions. 61. Find a way to graph the line from the problem 59 on your calculator. Sketch the calculator graph here, including the window values:

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Basics of Computational Geometry

[Video] and so on... Problems that require a function definition can be phrased as a word problem such as the following:

Unit 4: Client View of a Component Methods

;; ;; Section 1 ;; ;; ;; What is the value of: (+ 2 (* 3 5)) ;; What is the value of: (string-append "Roberto" " " "Luongo")

CS1114 Section: SIFT April 3, 2013

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

Introduction to Computer Science with Python Course Syllabus

COPYRIGHTED MATERIAL. An Introduction to Computers That Will Actually Help You in Life. Chapter 1. Memory: Not Exactly 0s and 1s. Memory Organization

Activity 1: Introduction

Week 8: Operator overloading

Lecture Notes on Contracts

Type Checking and Type Equality

6.001 Notes: Section 15.1

Module 5: Lists. Readings: HtDP, Sections 9, 10.

LAB: INTRODUCTION TO FUNCTIONS IN C++

Lab 4 Projectile Motion

Python Basics. Lecture and Lab 5 Day Course. Python Basics

The Specification Phase

Section 1.2 Fractions

MITOCW watch?v=kz7jjltq9r4

Racket Style Guide Fall 2017

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Animations involving numbers

Module 10: Imperative Programming, Modularization, and The Future

NAME EET 2259 Lab 3 The Boolean Data Type

CSU211 Exam 2 Fall 2007

Spring CS Homework 3 p. 1. CS Homework 3

Week 3: Objects, Input and Processing

Module 4: Compound data: structures

Relation: Function: Represent the relation with a mapping. Is it a function? Vertical Line Test:

CSCI 355 Lab #2 Spring 2007

Watkins Mill High School. Algebra 2. Math Challenge

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Animations that make decisions

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

The Dynamic Typing Interlude

CSCI 355 LAB #2 Spring 2004

1.1 - Functions, Domain, and Range

Computing Seminar Introduction Oct

Binary Relations Part One

CS112 Lecture: Working with Numbers

foldr CS 5010 Program Design Paradigms Lesson 5.4

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Basic Keywords Practice Session

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2013

MSO Lecture 1. Wouter Swierstra (adapted by HP) September 11, 2017

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

Transcription:

Contracts, Purpose Statements, Examples and Tests CS 5010 Program Design Paradigms Bootcamp Lesson 2.1 Mitchell Wand, 2012-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1

Module 02 Basic Principles Tools and Techniques Object-Oriented Programming Designing Data Computing with Lists Interfaces and Classes Designing Functions Designing Systems Computing with Trees and Graphs Designing with Invariants Thinking about Efficiency Inheritance Objects with Mutable State Efficiency, Part 2 2

Module Objectives Last week, we introduced the Function Design Recipe, and examined the first step, Data Design, in detail. This week we will talk in more detail about the rest of the steps in the Function Design Recipe. We will also talk about the kinds of bugs you might encounter while running your programs and how to fix them. 3

Lesson Objectives At the end of this lesson, students will be able to: Write a contract and purpose statements for simple functions. Provide examples showing sample arguments and intended results. Write down the examples as human readable comments within the program.

Lesson Outline In this lesson we'll talk about two more steps in the Design Recipe: Step 2: Contract and Purpose Statement Step 3: Examples and Tests We'll also talk about a few other things, like how to choose good names for your functions and variables.

The Function Design Recipe The Function Design Recipe 1. Data Design 2. Contract and Purpose Statement 3. Examples and Tests 4. Design Strategy 5. Function Definition 6. Program Review

FDR Step 2: Contract and Purpose Statement Contract: specifies the kind of input data and the kind of output data Purpose Statement: A set of short noun phrases describing what the function is supposed to return. These are typically phrased in terms of information, not data. They generally take the form GIVEN/RETURNS, where each of these keywords is followed by a short noun phrase. When possible, they are phrased in terms of information, not data. 7

Examples of Contract and Purpose Statements ;; f2c: FarenTemp -> CelsiusTemp ;; GIVEN: a temperature in Fahrenheit, ;; RETURNS: the equivalent temperature in ;; Celsius ;; f2mars : FarenTemp -> CelsiusTemp ;; GIVEN: Any temperature in Fahrenheit ;; RETURNS: The mean temperature on the surface ;; of Mars, in Celsius 8

Examples of Contract and Purpose Statements (2) scene-with-cat : Cat Scene -> Scene GIVEN: a Cat c and a Scene s RETURNS: A Scene like s, except that the Cat c has been painted on it. 9

What makes a good purpose statement? It gives more information than just the contract. For example GIVEN: an Integer and a Boolean RETURNS: an Integer is not a good purpose statement It is specific. Ideally, a reader should be able to figure out what a function returns just by reading the purpose statement perhaps along with examples, other documentation, etc. but WITHOUT reading the code!

Good Function Names are Important A good choice of function name is important. When a function is used in some other piece of code, the reader should be able to tell roughly what a function computes just by looking at its name. If further detail is needed, then the reader can refer to the purpose statement of the function. If the function name is chosen well and the purpose statement is written well, the reader should rarely, if ever, need to refer to the function definition. For more discussion, see What's in a Name? 11

Conventions for Good Function Names Function names should almost always be nouns Should describe the result of the function e.g. area, not compute-area Predicates should end in? : e.g., square? (pronounced "huh?", as in "square-huh?") Use first component of the name to distinguish similar functions with different arguments, e.g.: circle-area, ring-area book-price, total-order-price

Conventions for Good Names In Racket, "-" and "?" are legal characters that may occur in names. Use the minus sign to separate components of a name, e.g. total-order-price Use the question mark to name predicates: eg, square?. These are our conventions. Other languages have other conventions; you should follow them.

Argument Names We use short names for arguments: b for a Book Or mnemonic names: cost, price Qualified names: mouse-x, bomb-x Avoid lame names, like list1. Names should refer to the information, not just the data type, whenever possible. These are our conventions. Your workplace may have different conventions for argument names.

Numeric Data Types In Racket, Number includes Complex numbers, so we'll hardly ever use Number. Integer vs. NonNegReal vs. PosReal? look to the data definition. If your number represents a quantity that is always non-negative (say, a length or an area), then call it a NonNegInt. if we're not dealing with physical quantities, then we'll typically use Integer. Your function has to handle any value of the type it says in the contract. 15

FDR Step 3: Examples and Tests Examples show sample arguments and results, to make clear what is intended. This may include showing how the function should be called. It should also illustrate the different behaviors of the function. How many examples, and what kind, will depend a lot on the function

Examples of Examples (1) If the function is a linear function of a single input, two examples are sufficient to uniquely determine the function. We saw this for f2c : ;; (f2c 32) = 0 ;; (f2c 212) = 100 17

Examples of Examples (2) If the function takes an argument that is itemization or mixed data, then choose examples from each subclass of the itemization. Example: ;; (next-state "red") = "green" ;; (next-state "yellow") = "red" ;; (next-state "green") = "yellow" If your function uses a cond to divide its inputs into classes, choose examples from each class.

Examples of Examples (3) Avoid coincidences in your examples. This example is coincidental: (book-profit-margin (make-book "Little Lisper" "Friedman" 2.00 4.00)) = 2.00 Is the answer 2 because we subtracted 2 from 4, or because it is the third field in the book? This example is not coincidental: (book-profit-margin (make-book "Little Lisper" "Friedman" 2.00 5.00)) = 3.00 we must have subtracted 2 from 5 to get 3.

Make your examples readable ;;; Here s an example: a rocket simulation. ;; INFORMATION ANALYSIS: ;; An Altitude is represented as a Real, measured in meters ;; A Velocity is represented as Real, measured in meters/sec upward ;; We have a single rocket, which is at some altitude and is ;; travelling vertically at some velocity. ;; REPRESENTATION: ;; A Rocket is represented as a struct (make-rocket altitude velocity) ;; with the following fields: ;; altitude : Altitude is the rocket's altitude ;; velocity : Velocity is the rocket's velocity ;; IMPLEMENTATION: (define-struct rocket (altitude velocity)) ;; CONSTRUCTOR TEMPLATE: ;; (make-rocket Real Real) 20

Not-so-readable examples ;; EXAMPLE: ;; (rocket-after-dt (make-rocket 100 30) 0) ;; = (make-rocket 100 30) ;; (rocket-after-dt (make-rocket 100 30) 2) ;; = (make-rocket 160 30) What do these examples illustrate? Where did those values come from? These are very simple structures, but for more complicated structures you d have a hard time telling. and so would your grader, or boss! And if you change the representation of rockets, you ll have to change all your examples, too! 21

Better Examples (define rocket-at-100 (make-rocket 100 30)) (define rocket-at-160 (make-rocket 160 30)) ;; (rocket-after-dt rocket-at-100 0) = rocket-at-100 ;; (rocket-after-dt rocket-at-100 2) = rocket-at-160 Here we ve introduced mnemonic names for each of the example values. These could serve as examples for the data definitions, too. You can inspect those definitions to check whether they represent the rocket they are supposed to represent. The example is in terms of information, not data. If you decide later to change the representation, you can still use the examples. 22

Turn your examples into tests (begin-for-test (check-equal? (f2c 32) 0) (check-equal? (f2c 212) 100)) Tests live in your file, so they are checked every time your file is loaded Exact technology for tests may change; see the example files for current technology LOTS more to say about testing, but this is enough for now. 23

Summary In this lesson, you have learned how to: Write a contract and purpose statements for simple functions. Provide examples showing sample arguments and intended results. Write down those examples as human readable comments within the program. Turn your examples into executable tests. 24

Next Steps Study the file 02-1-1-rocket-examples.rkt in the Examples folder. If you have questions about this lesson, post them on the discussion board. Go on to the next lesson. 25