Josh Bloch Charlie Garrod - cs.cmu.edu

38
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

Transcript of Josh Bloch Charlie Garrod - cs.cmu.edu

Page 1: Josh Bloch Charlie Garrod - cs.cmu.edu

117-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

Page 2: Josh Bloch Charlie Garrod - cs.cmu.edu

217-214

Administrivia

• Homework 4a due Thursday, 11:59 p.m.– Design review meeting is mandatory

Page 3: Josh Bloch Charlie Garrod - cs.cmu.edu

317-214

Outline

I. Midterm exam post-mortem – SET problem

II. Permutation generator post-mortem

III. Cryptarithm post-mortem

Page 4: Josh Bloch Charlie Garrod - cs.cmu.edu

417-214

Midterm exam results – histograms of raw scores(Excluding SET)

Page 5: Josh Bloch Charlie Garrod - cs.cmu.edu

517-214

Anyone know a simpler expression for this?

if (myDog.hasFleas()) {

return true;

} else {

return false;

}

Page 6: Josh Bloch Charlie Garrod - cs.cmu.edu

617-214

Hint: it’s not this

return myDog.hasFleas() ? true : false;

Page 7: Josh Bloch Charlie Garrod - cs.cmu.edu

717-214

Please do it this way from now onWe reserve the right to deduct points if you don’t

return myDog.hasFleas();

Page 8: Josh Bloch Charlie Garrod - cs.cmu.edu

817-214

SET problem – Card 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!

Page 9: Josh Bloch Charlie Garrod - cs.cmu.edu

917-214

What’s the best type for a feature?

Page 10: Josh Bloch Charlie Garrod - cs.cmu.edu

1017-214

What’s the best type for a SET?

Page 11: Josh Bloch Charlie Garrod - cs.cmu.edu

1117-214

A good, basic solution – features (1/4)

Page 12: Josh Bloch Charlie Garrod - cs.cmu.edu

1217-214

A good, basic solution – features (1a/4)

Page 13: Josh Bloch Charlie Garrod - cs.cmu.edu

1317-214

A good, basic solution – fields, constructor, accessors (2/4)

Page 14: Josh Bloch Charlie Garrod - cs.cmu.edu

1417-214

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

Page 15: Josh Bloch Charlie Garrod - cs.cmu.edu

1517-214

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

Page 16: Josh Bloch Charlie Garrod - cs.cmu.edu

1617-214

API is simple – client code is pretty

Page 17: Josh Bloch Charlie Garrod - cs.cmu.edu

1717-214

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

Page 18: Josh Bloch Charlie Garrod - cs.cmu.edu

1817-214

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

• Good choice of representation– Fighting with representation adds verbosity

• Makes good use of the facilities provided for us by the platform– Object methods on enum

– Utility methods such as Objects.requireNonNull and List.of

• Makes good use of itself– Code reuse vs. copy-and-paste

Page 19: Josh Bloch Charlie Garrod - cs.cmu.edu

1917-214

Using generics to make a reusable thirdInSet methodGeneric methods are powerful, but declarations are ugly and complex

private static <T extends Enum<T>> T thirdInSet(T first, T second) {… // Implementation redacted

}

You can call static method directly from Card.thirdInSetor dispatch to it from feature enums:public enum Number { ONE, TWO, THREE;

Number thirdInSet(Number second) {return Card.thirdInSet(this, second);

}}

public enum Color { RED, GREEN, PURPLE;Color thirdInSet(Color second) {

return Card.thirdInSet(this, second);}

}

Page 20: Josh Bloch Charlie Garrod - cs.cmu.edu

2017-214

Deep magic: how to “inherit” thirdInSet implementationDefault implementations, added in Java 8, are the secret ingredient

interface Feature<T extends Enum<T> & Feature<T>> { // 🤮🤮🤮🤮🤮🤮default T thirdInSet(T second) {

… // Implementation redacted}

}

public enum Number implements Feature<Number> { ONE, TWO, THREE }public enum Color implements Feature<Color> { RED, GREEN, PURPLE }public enum Shading implements Feature<Shading> {OUTLINE, STRIPED, SOLID}public enum Shape implements Feature<Shape> { DIAMOND, SQUIGGLE, OVAL }

Page 21: Josh Bloch Charlie Garrod - cs.cmu.edu

2117-214

A worthwhile performance tweak to CardYou didn’t need to do this on the exam, but it’s worth it in real life

Replace this

public Card(Number num, Color color, Shading shade, Shape shape);

with this

private Card(Number num, Color color, Shading shade, Shape shape);

private static final List<Card> deck = newDeck();

public static Card of(Number number, Color color, Shading shading,Shape shape) {

return deck.get(index(number, color, shading, shape));}

private int index(Number num, Color col, Shading shd, Shape shp) {return 27*num.ordinal() + 9*col.ordinal() + 3*shd.ordinal() + shp.ordinal();

}

• This is called an instance-controlled class– Only 81 instances ever exist (one per value)

Page 22: Josh Bloch Charlie Garrod - cs.cmu.edu

2217-214

Outline

I. Midterm exam post-mortem – SET problem

II. Permutation generator post-mortem

III. Cryptarithm post-mortem

Page 23: Josh Bloch Charlie Garrod - cs.cmu.edu

2317-214

Design comparison for permutation generator

• Command pattern– Easy to code

– Reasonably pretty to use:

PermGen.doForAllPermutations(list, (perm) -> { // lambdaif (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

Page 24: Josh Bloch Charlie Garrod - cs.cmu.edu

2417-214

A complete (!), general-purpose permutation generatorusing the command pattern

Page 25: Josh Bloch Charlie Garrod - cs.cmu.edu

2517-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 listCheck that we haven’t seen it yetPut 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!

Page 26: Josh Bloch Charlie Garrod - cs.cmu.edu

2617-214

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

Page 27: Josh Bloch Charlie Garrod - cs.cmu.edu

2717-214

Pros and cons of exhaustive testing

• Pros and cons of exhaustive testing+ Gives you (nearly) 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

Page 28: Josh Bloch Charlie Garrod - cs.cmu.edu

2817-214

Outline

• Midterm exam post-mortem

• Permutation generator post-mortem

• Cryptarithm post-mortem– Cryptarithm class (6 slides)

– CryptarithmWordExpression (2 slides)

– Main program (1 slide)

Page 29: Josh Bloch Charlie Garrod - cs.cmu.edu

2917-214

Cryptarithm class (1/6) – fields

Page 30: Josh Bloch Charlie Garrod - cs.cmu.edu

3017-214

Cryptarithm class (2/6) – constructor / parserSample input argument: ["send", "+", "more", "=", "money"]

Page 31: Josh Bloch Charlie Garrod - cs.cmu.edu

3117-214

Cryptarithm class (3/6) – word parser

Page 32: Josh Bloch Charlie Garrod - cs.cmu.edu

3217-214

Cryptarithm class (4/6) – operator parser

Page 33: Josh Bloch Charlie Garrod - cs.cmu.edu

3317-214

Cryptarithm class (5/6) – solver

Page 34: Josh Bloch Charlie Garrod - cs.cmu.edu

3417-214

Cryptarithm class (6/6) – solver helper functions

Page 35: Josh Bloch Charlie Garrod - cs.cmu.edu

3517-214

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

Page 36: Josh Bloch Charlie Garrod - cs.cmu.edu

3617-214

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

Page 37: Josh Bloch Charlie Garrod - cs.cmu.edu

3717-214

Cryptarithm solver command line program

Page 38: Josh Bloch Charlie Garrod - cs.cmu.edu

3817-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.