Last active
December 31, 2015 18:49
-
-
Save CodingFabian/8029438 to your computer and use it in GitHub Desktop.
The most dreadful code I have seen in a while
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
public static String makeValid(String value) { | |
value = value.replaceAll("a", "A"); | |
value = value.replaceAll("b", "B"); | |
value = value.replaceAll("c", "C"); | |
value = value.replaceAll("d", "D"); | |
value = value.replaceAll("e", "E"); | |
value = value.replaceAll("f", "F"); | |
value = value.replaceAll("g", "G"); | |
value = value.replaceAll("h", "H"); | |
value = value.replaceAll("i", "I"); | |
value = value.replaceAll("j", "J"); | |
value = value.replaceAll("k", "K"); | |
value = value.replaceAll("l", "L"); | |
value = value.replaceAll("m", "M"); | |
value = value.replaceAll("n", "N"); | |
value = value.replaceAll("o", "O"); | |
value = value.replaceAll("p", "P"); | |
value = value.replaceAll("q", "Q"); | |
value = value.replaceAll("r", "R"); | |
value = value.replaceAll("s", "S"); | |
value = value.replaceAll("t", "T"); | |
value = value.replaceAll("u", "U"); | |
value = value.replaceAll("v", "V"); | |
value = value.replaceAll("w", "W"); | |
value = value.replaceAll("x", "X"); | |
value = value.replaceAll("y", "Y"); | |
value = value.replaceAll("z", "Z"); | |
value = value.replaceAll("ä", "Ä"); | |
value = value.replaceAll("ö", "Ö"); | |
value = value.replaceAll("ü", "Ü"); | |
return value; | |
} |
My first impression was: Ok, stupid repetition to iterate whole string for each letter, but still O(n). Then I remembered that in Java this is no inplace-replacement, so each time a new String is allocated and copied. I guess that's the performance-killer... (though still O(n))
Oh, and BTW: what's with all the non-German-non-ASCII-letters?
Christian, you are right, it is at least mostly O(n). But there is another detail hidden.
Hint: Look at the source of replaceAll, preferably in Java 1.6 which was used.
Hint2: The Strings created are not the heaviest objects involved.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should I create a benchmark showing how bad it is for those who do not speak Java? :)