Refactoring Teaser IV – Part 2
Time to take the next baby step.
Lets draw our attention to:
public class IDTokens extends ChildStrategyParam { public IDTokens(final String token1, final String token2) { super(token1, token2, null); } @Override public String getToken3() { throw new UnsupportedOperationException(); } } |
This code is quite interesting. It suffers with 3 code smells:
- Black Sheep
- Refused Bequest
- Dumb Data Holder
Also this class violates the “Tell don’t Ask” principle.
Then we look at who is constructing this class, and turns out that we have this deadly SuggestionsUtil class (love the name). This class suffers with various code smells:
- Blatant Duplicate Code
- Primitive Obsession
- Switch Smell
- Conditional Complexity
- Null Checks
- Long method
- Inappropriate Naming
And now the code:
public class SuggestionsUtil { private static int MAX_ATTEMPTS = 5; private final DomainNameService domainNameService; public SuggestionsUtil(final DomainNameService domainNameService) { this.domainNameService = domainNameService; } |
public IDTokens getIdentityTokens(String token1, String token2) { if (isCelebrityName(token1, token2)) { token1 = token1.substring(0, token1.length() - 1); token2 = token2.substring(0, token2.length() - 1); } int loopCounter = 1; do { loopCounter++; String generatedFirstToken = generateFirstToken(token1); String generatedSecondToken = generateSecondToken(token2); if (generatedFirstToken == null || generatedSecondToken == null) return null; else if (isCelebrityName(generatedFirstToken, generatedSecondToken)) { token1 = generatedFirstToken.substring(0, generatedFirstToken.length() - 1); token2 = generatedSecondToken.substring(0, generatedSecondToken.length() - 1); } else return new IDTokens(generatedFirstToken, generatedSecondToken); } while (loopCounter != MAX_ATTEMPTS); return null; } |
private String generateSecondToken(String token2) { int loopCounter = 0; String restrictedWord = null; do { restrictedWord = domainNameService.validateSecondPartAndReturnRestrictedWordIfAny(token2); String replacement = null; if (restrictedWord != null) { replacement = restrictedWord.substring(0, restrictedWord.length() - 1); token2 = token2.replaceAll(restrictedWord, replacement); loopCounter++; } } while (restrictedWord != null && loopCounter != MAX_ATTEMPTS); if (loopCounter == MAX_ATTEMPTS) return null; return token2; } |
private String generateFirstToken(String token1) { int loopCounter = 0; String restrictedWord = null; do { restrictedWord = domainNameService.validateFirstPartAndReturnRestrictedWordIfAny(token1); String replacement = null; if (restrictedWord != null) { replacement = restrictedWord.substring(0, restrictedWord.length() - 1); token1 = token1.replaceAll(restrictedWord, replacement); loopCounter++; } } while (restrictedWord != null && loopCounter != MAX_ATTEMPTS); if (loopCounter == MAX_ATTEMPTS) return null; return token1; } |
private boolean isCelebrityName(final String token1, final String token2) { return domainNameService.isCelebrityName(token1, token2); } public String appendTokensForId(final String token1, final String token2) { return token1.toLowerCase().concat("@").concat(token2.toLowerCase()).concat(".com"); } |
Also have a look at SuggesitonsUtilsTest, it has a lot of Duplication and vague tests. Guess this will keep you busy for then next couple of hours.