Command-Query Separation

In Profile, we scrutinize the matches() method:

iloveyouboss/big-2/src/iloveyouboss/Profile.java
 
public​ ​boolean​ matches(Criteria criteria) {
 
MatchSet matchSet = ​new​ MatchSet(answers, criteria);
 
score = matchSet.getScore();
 
return​ matchSet.matches();
 
}

It has the awkward side effect of storing a calculated score on the Profile object. That makes no sense from the context of a Profile. A Profile doesn’t have a single score; it only has a score in conjunction with an attempt to match on criteria.

The score side effect causes another problem, which is that we can’t separate one interest from the other. If we want the score, we have to know to call the matches() method, which is counterintuitive, and we ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.