March 2015
Intermediate to advanced
236 pages
5h 26m
English
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 waste. For this reason (among others[33]), avoid doing any real work in constructors.
Change the code to calculate the score when it’s requested:
| iloveyouboss/big-5/src/iloveyouboss/MatchSet.java | |
| | public class MatchSet { |
| | // ... |
| | |
| | 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; |
| | } |
| | // ... |
Read now
Unlock full access