O'Reilly logo

Pragmatic Unit Testing in Java 8 with JUnit by Jeff Langr, Andy Hunt, Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Taking Refactoring Too Far?

Similarly, extract the code that calculates the total weighting of all matches:

iloveyouboss/22/src/iloveyouboss/Profile.java
 
public​ ​boolean​ matches(Criteria criteria) {
*
calculateScore(criteria);
 
 
boolean​ kill = false;
 
for​ (Criterion criterion: criteria) {
 
boolean​ match = criterion.matches(answerMatching(criterion));
 
if​ (!match && criterion.getWeight() == Weight.MustMatch) {
 
kill = true;
 
}
 
}
 
if​ (kill)
 
return​ false;
 
return​ anyMatches(criteria);
 
}
 
*
private​ ​void​ calculateScore(Criteria criteria) {
*
score = 0;
*
for​ (Criterion criterion: criteria)
*
if​ (criterion.matches(answerMatching(criterion)))
*
score += criterion.getWeight().getValue();
*
}

Double hmm. You might be wondering ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required