Created
May 23, 2011 15:53
-
-
Save spilth/986928 to your computer and use it in GitHub Desktop.
Translate Function After 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
Author
spilth
commented
May 26, 2011
via email
Hey,
Thanks for your thoughts. I looked up Piglatin last night (on Wikipedia, at least) and saw that it's strictly for english, so that does become redundant.
Jlangr has some better method names to replace simple and complex. I think his were addWay and transposeAndAddWay. Those give some insight into what the method is actually doing. I agree that my versions were vague. They didn't sit well with me.
I want to set up a github project for the code so people can see the history of the changes over time and better discuss it. I also may rewrite the story I did with Storify as a blog post so Thr changes can be clearer.
…-brian
Sent from my iPad
On May 26, 2011, at 5:05 PM, ***@***.*** wrote:
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.
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/986928
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment