Skip to content

Instantly share code, notes, and snippets.

@CodingFabian
Last active December 31, 2015 18:49
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 CodingFabian/8029438 to your computer and use it in GitHub Desktop.
Save CodingFabian/8029438 to your computer and use it in GitHub Desktop.
The most dreadful code I have seen in a while
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;
}
@CodingFabian
Copy link
Author

Should I create a benchmark showing how bad it is for those who do not speak Java? :)

@ChristianLangmann
Copy link

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?

@CodingFabian
Copy link
Author

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