Skip to content

Instantly share code, notes, and snippets.

@davelnewton
Created March 9, 2013 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davelnewton/258761cbf3143da4288c to your computer and use it in GitHub Desktop.
Save davelnewton/258761cbf3143da4288c to your computer and use it in GitHub Desktop.
Flattening a Java class; no other refactorings.
public class GuessChar2 {
public boolean guessCharacter(char guess) throws A8InvalidInputException, A8AlreadyGuessedException {
String sGuess = "" + guess;
if (invalidCharacter(guess)) { // Use the string?
throw new A8InvalidInputException(sGuess);
}
guess = Character.toLowerCase(guess); // Modifying a parameter?
if (guessedCharacters.contains(guess) ) {
throw new A8AlreadyGuessedException(sGuess);
}
numberOfGuessesLeft--;
guessedCharacters.add(guess);
return keyPhrase.contains(sGuess);
}
}
package com.davelnewton.guess;
public class GuessChar3 {
public boolean guessCharacter(char guess) throws A8InvalidInputException, A8AlreadyGuessedException {
assertValidGuess(guess);
assertNotGuessed(guess);
return processValidGuess(guess);
}
public void assertValidGuess(char guess) throws A8InvalidInputException {
if (invalidCharacter(guess)) {
throw new A8InvalidInputException("" + guess);
}
}
public void assertNotGuessed(char guess) throws A8AlreadyGuessedException {
guess = Character.toLowerCase(guess);
if (guessedCharacters.contains(guess) ) {
throw new A8AlreadyGuessedException("" + guess);
}
}
public boolean processValidGuess(char guess) {
numberOfGuessesLeft--;
guessedCharacters.add(guess);
return keyPhrase.contains("" + guess);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment