Josh Bloch Charlie Garrod School of Computer Science 17-214 1 - - PowerPoint PPT Presentation

josh bloch charlie garrod
SMART_READER_LITE
LIVE PREVIEW

Josh Bloch Charlie Garrod School of Computer Science 17-214 1 - - PowerPoint PPT Presentation

Principles of Software Construction tis a Gift to be Simple or Cleanliness is Next to Godliness Midterm 1 and Homework 3 Post-Mortem Josh Bloch Charlie Garrod School of Computer Science 17-214 1 Administrivia Homework 4a due Thursday,


slide-1
SLIDE 1

1

17-214

School of Computer Science

Principles of Software Construction ’tis a Gift to be Simple or Cleanliness is Next to Godliness Midterm 1 and Homework 3 Post-Mortem

Josh Bloch Charlie Garrod

slide-2
SLIDE 2

2

17-214

Administrivia

  • Homework 4a due Thursday, 11:59 p.m.

– Design review meeting is mandatory

slide-3
SLIDE 3

3

17-214

Outline

I. Midterm exam post-mortem II. Permutation generator post-mortem

  • III. Cryptarithm post-mortem
slide-4
SLIDE 4

4

17-214

Midterm exam results

slide-5
SLIDE 5

5

17-214

Anyone know a simpler expression for this?

if (myDog.hasFleas()) { return true; } else { return false; }

slide-6
SLIDE 6

6

17-214

Hint: it’s not this

return myDog.hasFleas() ? true : false;

slide-7
SLIDE 7

7

17-214

Please do it this way from now on

We reserve the right to deduct points if you don’t

return myDog.hasFleas();

slide-8
SLIDE 8

8

17-214

DnaStrand should be immutable

  • Much safer – value can’t change underneath you
  • Trivial to use concurrently – no synchronization necessary
  • More efficient – can share instances
  • Always make simple value classes immutable!
slide-9
SLIDE 9

9

17-214

What’s the best representation for a base?

slide-10
SLIDE 10

10

17-214

What’s the best internal representation for a strand?

slide-11
SLIDE 11

11

17-214

In a real-world setting, performance concerns might intrude

  • The human genome has about 3 billion base pairs

– Would take up 24 GB with our current representation – But each base pair has only 2 bits of actual information – So you could cut this down by a factor of 16

  • This implies a bit-vector representation

– Strand would be represented as an array of (say) int – Where each int represents 16 bases

  • But you don’t do this sort of thing until you know you have to

– Avoid premature optimization

  • It would have been wrong to do this for the exam
slide-12
SLIDE 12

12

17-214

What are best input types for constructor (or factory)?

slide-13
SLIDE 13

13

17-214

A good, basic solution – Base enum (1/4)

slide-14
SLIDE 14

14

17-214

A good, basic solution – field and constructor (2/4)

slide-15
SLIDE 15

15

17-214

A good, basic solution – Object methods (3/4)

slide-16
SLIDE 16

16

17-214

A good, basic solution – complementarity methods (4/4)

slide-17
SLIDE 17

17

17-214

API is good – client code is pretty

slide-18
SLIDE 18

18

17-214

Why is this solution ¼ the length of many we received?

slide-19
SLIDE 19

19

17-214

Why is this solution ¼ the length of many we received?

  • Good choice of internal representation

– Fighting with representation adds verbosity

  • Makes good use of the facilities provided for us by the platform
  • Makes good use of itself

– Code reuse vs. copy-and-paste

slide-20
SLIDE 20

20

17-214

Outline

I. Midterm exam post-mortem II. Permutation generator post-mortem

  • III. Cryptarithm post-mortem
slide-21
SLIDE 21

21

17-214

Design comparison for permutation generator

  • Command pattern

– Easy to code – Reasonably pretty to use:

PermGen.doForAllPermutations(list, (perm) -> { if (isSatisfactory(perm)) doSomethingWith(perm); });

  • Iterator pattern

– Tricky to code because algorithm is recursive and Java lacks generators – Really pretty to use because it works with for-each loop

for (List<Foo> perm : Permutations.of(list)) if (isSatisfactory(perm)) doSomethingWith(perm);

  • Performance is similar
slide-22
SLIDE 22

22

17-214

A complete (!), general-purpose permutation generator

using the command pattern

slide-23
SLIDE 23

23

17-214

How do you test a permutation generator?

Make a list of items to permute (consecutive integers do nicely) For each permutation of the list { Check that it’s actually a permutation of the list Check that we haven’t seen it yet Put it in the set of permutations that we have seen } Check that the set of permutations we’ve seen has right size (n!) Do this for all reasonable values of n, and you’re done!

slide-24
SLIDE 24

24

17-214

And now, in code – this is the whole thing!

slide-25
SLIDE 25

25

17-214

Pros and cons of exhaustive testing

  • Pros and cons of exhaustive testing

+ Gives you “absolute assurance” that the unit works + Exhaustive tests can be short and elegant + You don’t have to worry about what to test − Rarely feasible; Infeasible for:

  • Nondeterministic code, including most concurrent code
  • Large state spaces
  • If you can test exhaustively, do!
  • If not, you can often approximate it with random testing
slide-26
SLIDE 26

26

17-214

Outline

  • Midterm exam post-mortem
  • Permutation generator post-mortem
  • Cryptarithm post-mortem

– Cryptarithm class (6 slides) – CryptarithmWordExpression (2 slides) – Main program (1 slide)

slide-27
SLIDE 27

27

17-214

Cryptarithm class (1/6) – fields

slide-28
SLIDE 28

28

17-214

Cryptarithm class (2/6) – constructor / parser

Sample input argument: ["send", "+", "more", "=", "money"]

slide-29
SLIDE 29

29

17-214

Cryptarithm class (3/6) – word parser

slide-30
SLIDE 30

30

17-214

Cryptarithm class (4/6) – operator parser

slide-31
SLIDE 31

31

17-214

Cryptarithm class (5/6) – solver

slide-32
SLIDE 32

32

17-214

Cryptarithm class (6/6) – solver helper functions

slide-33
SLIDE 33

33

17-214

CryptarithmExpressionContext

Naïve version; solves 10-digit cryptarithms in about 1 s.

slide-34
SLIDE 34

34

17-214

CryptarithmWordExpression

Naïve version; solves 10-digit cryptarithms in about 1 s.

slide-35
SLIDE 35

35

17-214

Cryptarithm solver command line program

slide-36
SLIDE 36

36

17-214

Conclusion

  • Good habits really matter

– “The way to write a perfect program is to make yourself a perfect programmer and then just program naturally.” – Watts S. Humphrey, 1994

  • Don’t just hack it up and say you’ll fix it later

– You probably won’t – but you will get into the habit of just hacking it up

  • Representations matter! Choose carefully.

– If your code is getting ugly, step back and rethink it – “A week of coding can often save a whole hour of thought.”

  • Not enough to be merely correct; code must be clearly correct

– Try to avoid nearly correct.