Skip to content

Instantly share code, notes, and snippets.

@spilth
Created May 23, 2011 15:53
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 spilth/986928 to your computer and use it in GitHub Desktop.
Save spilth/986928 to your computer and use it in GitHub Desktop.
Translate Function After 2
package com.buildndeploy.piglatin;
public class Translator {
public String toPiglatin(String originalWord) {
if (startsWithVowel(originalWord)) {
return (simplePiglatinWord(originalWord));
} else {
return (complexPiglatinWord(originalWord));
}
}
private String complexPiglatinWord(String originalWord) {
String piglatinWord;
int firstVowelIndex = findFirstVowelIndex(originalWord);
String piglatinStart = originalWord.substring(firstVowelIndex);
String piglatinEnd = originalWord.substring(0, firstVowelIndex).toLowerCase();
piglatinWord = piglatinStart + piglatinEnd + "ay";
if (isCapitalized(originalWord)) {
piglatinWord = capitalizeWord(piglatinWord);
}
return piglatinWord;
}
private String capitalizeWord(String word) {
Character firstLetter = Character.toUpperCase(word.charAt(0));
word = firstLetter + word.substring(1);
return word;
}
private boolean isCapitalized(String originalWord) {
return Character.isUpperCase(originalWord.charAt(0));
}
private String simplePiglatinWord(String originalWord) {
String piglatinWord;
piglatinWord = originalWord + "way";
return piglatinWord;
}
private boolean startsWithVowel(String word) {
return isVowel(word.charAt(0));
}
private int findFirstVowelIndex(String word) {
int vowelIndex = 0;
int wordLength = word.length();
for (int i = 0; i < wordLength; i++) {
if (isVowel(word.charAt(i))) {
vowelIndex = i;
break;
}
}
return vowelIndex;
}
private boolean isVowel(Character letter) {
letter = Character.toLowerCase(letter);
return letter.equals('a')
|| letter.equals('e')
|| letter.equals('i')
|| letter.equals('o')
|| letter.equals('u')
|| letter.equals('y');
}
}
@jbrains
Copy link

jbrains commented May 26, 2011

Isn't originalWord really englishWord? Do you translate from other languages? Here, "original" is a structurally-accurate name, but not intention-revealing: it describes the role the parameter plays in implementing the algorithm, but not the role that the information plays in achieving the goal of translating into Pig Latin.

In the case of simplePiglatinWord(), I find this name accurate and vague but imprecise and slightly misleading. I see this method as using the simple English-to-Pig Latin translation method (adding "way" at the end), and so suggest the name translateWithSimpleRule() or translateWithSimpleMethod(). I worry about using the term method in OO code, because of its OO-specific meaning. I might expect reflection code when I see that name. I also thought about translateSimply(), which sounds clever, but the corresponding translateComplexly() sounds weird.

Finally, findFirstVowelIndex() represents another implementation-bound name: in particular, it uses Java's language where I would suggest positionOfFirstVowel(). I see this point less clearly, since we to accept index as a common term in Java.

I have code suggestions, too, but I know you wanted to focus on names here.

@spilth
Copy link
Author

spilth commented May 26, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment