How We Should (Not) Teach Programming

0 downloads 0 Views 491KB Size Report
Feb 8, 2007 - Lewis, J. Loftus W. Java Software Solutions Foundations of. Program Design Addison Wesley 2001. King K.N. Java Programming from the ...
How We Should (Not) Teach Programming Chris Stephenson, Istanbul Bilgi University

February 8, 2007

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Data design

Much more often, strategic breakthrough will come from redoing the representation of the data or tables. That is where the heart of a program lies. Show me your flowchart and conceal your tables and I shall continue to be mystified. Show me your tables and I won’t usually need your flowcharts, they’ll be obvious. F.P. Brooks (1975!)

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Abstraction

Software entities are more complex for their size than perhaps any other human construct because no two parts are alike (at least above the statement level). If they are, we make the two similar parts into a subroutine–open or closed. In this respect, software systems differ profoundly from computers, buildings, or automobiles, where repeated elements abound. F.P. Brooks (1975!)

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Data Design

Object Oriented Programming Model-View-Controller JSEE - XML glue

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Abstraction

Inheritance Iterators and Collections Design Patterns

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Do we teach our students this way? No.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Do we teach our students this way? No. Not only do we not teach them this way, but, in general, in programming teaching, we teach an approach to programming that makes it more difficult for our students to subsequently grasp these concepts.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

What are the Symptoms

“copy-paste” programming No programming Google for the answer, not find the answer for Google

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

What did I study?

A simple sort 7 books from my freebies (Some other books think sorting is “too difficult”)

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

The works

Bell, P. Parr, M. Java for Students Prentice Hall 2002 Deitel, H.M. Deitel, P.J. Java: How to Program Prentice Hall 2003 Deitel, H.M. Deitel, P.J. C: How to Program Prentice Hall 1992

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

The works

Lewis, J. Loftus W. Java Software Solutions Foundations of Program Design Addison Wesley 2001 King K.N. Java Programming from the Beginning Norton 2000 Liang, D.Y. Introduction to Java Programming Prentice Hall 2003 Riley, D.D. The Object of Java Introduction to Programming Using Software Engineering Principles Addison Wesley 2002

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java for Students

A sort is “not the easiest program to write” Left as an exercise -with no warning about the traps

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Self plagiarised from C: How to Program The code: // swap two elements of an array public void swap(int array3[], int first, int second) { int hold; // temporary holding area for swap hold = array3[ first ]; array3[ first ] = array3[ second ]; array3[ second ] = hold; }

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped?

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped? References and mutability

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped? References and mutability Why do you need a third variable?

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped? References and mutability Why do you need a third variable? Imperative programming

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped? References and mutability Why do you need a third variable? Imperative programming Why is the method declared public?

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped? References and mutability Why do you need a third variable? Imperative programming Why is the method declared public? Not actually necessary here!

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped? References and mutability Why do you need a third variable? Imperative programming Why is the method declared public? Not actually necessary here! Why is the array parameter given the name array3?

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Why do you pass an array as a parameter and two indices to perform a swap? Why not just pass the two values to be swapped? References and mutability Why do you need a third variable? Imperative programming Why is the method declared public? Not actually necessary here! Why is the array parameter given the name array3? To confuse the student!

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Testing No critical cases tested Only one, hard coded test. Test result only checked visually.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

The law of the Purple Box Text in purple boxes is usually incorrect!

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Purple Box 1 Performance tip 7.2 (C:How to Program, performance tip 6.5) Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realise maximum performance.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java: How to Program

Purple Box 1 Performance tip 7.2 (C:How to Program, performance tip 6.5) Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realise maximum performance.

Wrong!

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java Software Solutions Foundations of Program Design

public static void insertionSort (int[] numbers){ for(int index = 1; index < numbers.length; index++){ int key = numbers[index]; int position = index; // shift larger values to the right while (position > 0 && numbers[position-1] > key) { numbers[position] = numbers[position-1]; position--; } numbers[position] = key; } }

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java Software Solutions - Foundations of Program Design

public static void insertionSort (int[] numbers){ for(int index = 1; index < numbers.length; index++){ int key = numbers[index]; int position = index; // shift larger values to the right while (numbers[position-1] > key && position > 0 ) { numbers[position] = numbers[position-1]; position--; } numbers[position] = key; } }

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

If the line is written as: while (numbers[position-1] > key && position > 0 ) The program will fail with an array bounds exception.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java Programming from the Beginning

forward running insert into a sorted array is developed then it isn’t used to write the insert sort, which works backwards. What is the Lesson?

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java Programming from the Beginning

forward running insert into a sorted array is developed then it isn’t used to write the insert sort, which works backwards. What is the Lesson? Do not reuse code!

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java Programming from the Beginning

Purple Box 2 When you’re designing an algorithm, don’t worry about the special cases at first. Design the algorithm to work in the typical case. After you have completed the algorithm, go back and check to see if it correctly handles the special cases. If it doesn’t, add additional tests for these cases.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java Programming from the Beginning

This jewel is repeated in the text In other words, don’t waste time and complicate your algorithms by worrying about all the special cases before you’ve designed the general algorithm.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Java Programming from the Beginning

Say a little prayer I hope none of the students who learn programming from this book going on to work writing the programs that control aircraft or nuclear power stations.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Introduction to Java Programming

Purple Box 3 Most students will not be able to derive the algorithm on their first attempt. I suggest that you write the code for the first iteration to find the largest element in the list and swap it with the last element, and then observe what would be different for the second iteration, the third, and so on. Fron the observation, you can write the outer loop and derive the algorithm.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

The Object of Java Introduction to Programming Using Software Engineering Principles

more ambitious full of pre and post conditions but it fails! only explains insert gives only a single example of its working, not a proof does not examine critical cases (empty, insert to begining, insert to end)

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

The Object of Java Introduction to Programming Using Software Engineering Principles

public void insert (int j) { start(); while (!isOff() && j > itemInt()){ forth(); } super.insert(new Integer(j)); } What is wrong?

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

The Object of Java Introduction to Programming Using Software Engineering Principles

public void insert (int j) { start(); while (!isOff() && j > itemInt()){ forth(); } super.insert(new Integer(j)); } What is wrong? The unexplained && trap.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Summary

Deitel C Deitel Java Lewis King Liang Riley

sort type bubble bubble insert insert selection insert

onetest1 yes yes yes no yes yes5

p. 3 4 6 5 3 5

loc 22 37 27 17 32 24

correct2 no no no no no no

fragile3 yes yes yes yes no yes

p. box4 Wrong Wrong Absent Wrong Wrong Absent

1

one test is enough argue general correctness 3 fragile, misleading or inexplicable code 4 purple box 5 only one example is considered 2

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Treatment of the Disease

Teach systematic design Teach structuring of data Don’t teach syntax Concentrate on design not languages

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

At Bilgi

design oriented approach data centred approach use a functional programming language (Scheme) drscheme.org use a design oriented text (How To Design Programs) www.htdp.org

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Data Design

Data Design Input is list of numbers Output is a sorted list of numbers.

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Data Design

Data Definitions What is a list of numbers? Either it is empty Or it is a number followed by a list of numbers

Let’s write a checking program (define (is-a-list-of-number? l) (cond ((empty? l) true) (else (and (number? (first l)) (is-a-list-of-numbers? (rest l))))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

The idea of a template

Now a template (define (some-func l) (cond ((empty? l) ...) (else ... (first l) ... (some-func (rest l)))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Another Data Definition What is a sorted list of numbers? Either it is an empty list Or a list of one number Or a pair of a number and a sorted list whose first element is not less than that number. Now a checker: (define (is-a-sorted-list-of-number? l) (cond ((empty? l) true) ((empty? (rest l)) true) (else(and ( empty (sort (pair 1 empty)) -> (pair 1 empty) (sort (pair 1 (pair 2 empty))) -> (pair 1 (pair 2 empty))) (sort (pair 2 (pair 1 empty))) -> (pair 1 (pair 2 empty))) (sort (pair 3 (pair 2 (pair 1 empty)))) -> (pair 1 (pair 2 (pair 3 empty))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

we can actually code these examples as a series of tests: (equal?

(sort empty) empty)

(equal?

(sort (pair 1 empty)) (pair 1 empty))

(equal? (sort (pair 1 (pair 2 empty))) (pair 1 (pair 2 empty))) (equal? (sort (pair 2 (pair 1 empty))) (pair 1 (pair 2 empty))) (equal? (sort (pair 3 (pair 2 (pair 1 empty)))) (pair 1 (pair 2 (pair 3 empty))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Now back to the template (define (sort l) (cond ((empty? l) ...) (else ... (first l) ... (sort (rest l))))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

The examples help us fill it in: The empty case (define (sort l) (cond ((empty? l) empty) (else ... (first l) ... (sort (rest l))))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

To fill the other ...s we need a helper function. Let us call it insert (define (sort l) (cond ((empty? l) empty) (else (insert (first l) (sort (rest l))))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Contract for the helper function

This gives us the contract for insert It must consume a number and a sorted list of numbers and produce a sorted list of numbers

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Use a template

How to write insert? Use a list template (define (insert n slon) (cond ((empty? slon) ...) (( (pair 3 empty) (insert 2 (pair 3 empty)) -> (pair 2 (pair 3 empty)) (insert 3 (pair 2 empty)) -> (pair 2 (pair 3 empty)) (insert 2 (pair 1 (pair 3 empty))) -> (pair 1 (pair 2 (pair 3 empty))) (insert 1 (pair 2 (pair 3 empty))) -> (pair 1 (pair 2 (pair 3 empty))) (insert 3 (pair 1 (pair 2 empty))) -> (pair 1 (pair 2 (pair 3 empty)))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Examples to Tests Again we can code these as a series of tests (equal?

(insert 3 empty) (pair 3 empty))

(equal? (insert 2 (pair 3 empty)) (pair 2 (pair 3 empty))) (equal? (insert 3 (pair 2 empty)) (pair 2 (pair 3 empty))) (equal? (insert 2 (pair 1 (pair 3 empty))) (pair 1 (pair 2 (pair 3 empty)))) (equal? (insert 1 (pair 2 (pair 3 empty))) (pair 1 (pair 2 (pair 3 empty)))) (equal? (insert 3 (pair 1 (pair 2 empty))) (pair 1 (pair 2 (pair 3 empty)))))

Chris Stephenson, Istanbul Bilgi University

How We Should (Not) Teach Programming

Fill in the form

Now fill in the template (define (insert n slon) (cond ((empty? slon) ...) ((