Command-Query Separation

In Profile, we scrutinize the matches() method:

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 live online training, plus books, videos, and digital content from nearly 200 publishers.