Other Design Thoughts

Learn about the other design principles that can improve test performance.

We'll cover the following...

The MatchSet() constructor does the work of calculating the score. If the calculated score isn’t consumed by a client, the effort to compute it is a waste. For this reason, avoid doing any real work in constructors.

Change the code to calculate the score when it’s requested:

Press + to interact
public class MatchSet {
// ...
private Map<String, Answer> answers;
private Criteria criteria;
public MatchSet(Map<String, Answer> answers, Criteria criteria) {
this.answers = answers;
this.criteria = criteria;
}
public int getScore() {
int score = 0;
for (Criterion criterion: criteria)
if (criterion.matches(answerMatching(criterion)))
score += criterion.getWeight().getValue();
return score;
}
//...
}

Lazy initialization

The score field ...