|
|
|
| Non-WTF Job: WPF Developer at Mediber (Charlottenburg, DE) |
| « ikownjou | I've Got The Monkey Now » |
For some tasks, you're presented an opportunity to do things the easy way or the hard way. When your friend is proudly showing off his work at replacing a light fixture, the switch, and all the wiring, you might wonder why not just replace the dead bulb?
"I was just fixing a bug in our software, and I came across this function," Johnny A. writes. "It works perfectly — does exactly what it says on the tin. I can't help thinking it could have been done with a few less lines of code, however. One, for example."
public static String replaceSpaceWithUnderscore(String str)
{
if (str == null || str.length() == 0)
{
return str;
}
StringTokenizer tokenizer = new StringTokenizer(str, " ");
String token = "";
String newStr = "";
int count = 1;
while (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();
if (count == 1)
{
newStr = token;
} else {
newStr = newStr + "_" + token;
}
count++;
}
return newStr;
}
|
Our code is littered with stuff like this, mostly because regex and elegant replacement methods didn't come around until much later (that and inexperience with Java's core methods). I don't know whether String.replace(char, char) was around for 1.0 or whenever this may have been written, but it's possible that it was not, or that the developer just didn't know about it.
Not using a StringBuffer, or even a char array, though, that's pretty silly. |
|
WTF? He didn't temporarily store the tokens in XML, interspersed with the underscores? That way he would be able to get the final string with one giant, performance crippling swoop of XSLT. That way it's really enterprisey, and customers will better make use of their oversized and overpriced hardware, thereby justifying the cost.
Something like this:
The XSLT:
Captcha: minim. WTF? Maxim would be better here! |
| « ikownjou | I've Got The Monkey Now » |