- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Steve is a Java refugee, who had his hand slapped for using == for string comparison instead of equals and found that it didn't work. After his migration to c#, he messed up his frist assignment.
Admin
Admin
Reminds me of How Not to Parse Command Line Arguments.
Admin
Bull. C#'s string is lowercased.
Admin
PostComment("X" + Character.toString(961 + (int)'E') + "S")
Admin
"In C#, the string keyword is an alias for String. Therefore,String and string are equivalent, and you can use whichever naming convention you prefer."
http://msdn.microsoft.com/en-us/library/ms228362.aspx
Admin
Admin
If he's a septic doesn't that just mean he's from the USA? Yank => septic tank => seppo...
Admin
Or maybe this is all rubbish, and people are smarter than hashing algorithms.
Admin
TRWTF is that while being overly sceptical about the String comparison, he obviously sees null checks as a complete waste of time...
Admin
The real WTF is the long and strange introduction for a short snippet of code.
Admin
String filenotfound = "FILE_NOT_FOUND"; if ((result.hashCode()) == filenotfound.hashCode()) throw "File not found!";
Admin
A man with two watches either knows what time it is and is reasonably sure that it's correct, or knows that either of the clocks are incorrect
Admin
Admin
Admin
Admin
Ha ha, and calling a phone a "dog" comes from "dog and bone" to "telephone," if it sounds like something your grandparents would come up with, that's because it is.
Admin
So, TRWTF is the author for not knowing that using == to compare Java Strings for equality flat-out doesn't work.
Admin
That is java code.
Admin
It doesn't flat out not work. If both values have been interned, it works - alternately if values have not been interned but happen to actually be the same reference, it works. If it flat out didn't work, that would actually be a kindness. However, one might do best to go along thinking it flat out doesn't work.
Admin
Admin
Admin
equals in 1.6 does not handle Unicode. try to equal two character in Devnagari and you will know what I mean.
Admin
I know what.
We should come up with two methods.
One called Equals, and the other ReferenceEquals.
That way we know exactly what we're checking each time.
Admin
[quote user="yourName]"In C#, the string keyword is an alias for String. Therefore,String and string are equivalent, and you can use whichever naming convention you prefer." [/quote] As long as you prefer the lowercased version.
Admin
I see nothing in this code that makes me think this is a language other than Java. After all, .hashCode() is a Java construct. The C# equivalent is .GetHashCode()
Admin
That sounds like a bug since Java Strings are stored as UTF-16 internally (likely Big Endian across all platforms for consistency, but I'd have to consult the standard). They were originally USC-2 in early Java versions, but switched to UTF-16 when it became apparent that 16-bit characters still weren't large enough to store all the characters for all the world's languages.
Admin
setDelay("YES"); ...do stuff... setDelay("NO"); // <---- FAIL
Admin
TRWTF is hardwiring the number of milliseconds of delay, yeah?
Admin
TRWTF is setting a property internally (Scenario.delay) rather than returning its value, yeah?
Admin
TRWTF is what appears to be a global variable (Scenario.delay), yeah?
Admin
TRWTF is an inline if statement, yeah?
Admin
TRWTF is spelling "sceptic" with a 'k', yeah?
Admin
TRWTF is a function that does nothing but take a value of one type ("YES") and overwrite a public value of another type (10000). Apparently the developers invoking this function can't be trusted to put sane values in Scenario.delay, so now they're just supposed to give a "YES" or "NO". Not to worry, since they can overwrite it with whatever they want if they just forgo the use of this useless function...
Admin
Has been fixed in 1.7 as far as I know.
Admin
Just for fun, I wrote a program to try to find some hash collisions with "YES" (which has hash code 87751, as mandated by the Java spec). I've found a few hundred or so so far among the search space of 7-character strings made up of [A-Za-z0-9]. Here's a small, randomly-selected sample:
3CCAKWA EldSFZQ FNEReZQ mDiVzaR vZLAXqy KIFHESU
So if anybody ever calls setDelay("3CCAKWA"), they'll be in for a rude surprise. Well, if getting the Scenario.delay set to 10000 ms when they were expecting no change is surprising.
Admin
No kidding. I facepalmed when I read it.
As long as the Unicode code points are the same, String.equals() works correctly regardless of the language involved.
When similar but not identical characters are involved, or when multiple combining diacritics are not in the same order between strings, they obviously won't compare equally via .equals().
Either way, comparing hash codes is not a working solution.
Admin
That's not what skepticism is about, and you know it. Disappointing.
Admin
TRWTF is even having the delay argument. If you don't want to set it, don't call it.
Reminds me of this anti-pattern:
Admin
Does anybody really know what time it is?
Admin
The BuildMaster cannon is right next to the insufferably cute kitten, but it's pointing the wrong way!
Admin
Admin
"Passing a string as a parameter instead of a Boolean is something that, possibly, could be forgiven."
No, it can't.
Admin
Only in wish-it-was-strongly-typed languages.
Admin
Does anybody really care?
Admin
That's why you need to wear 3 watches.
MOE: Hey! What's the idea of the three watches?
CURLY: That's the way I tell the time.
MOE: How do you tell the time?
CURLY: This one runs ten minutes slow every two hours. This runs twenty minutes fast every four hours. The one in the middle is broken and stopped at two o'clock.
MOE: Well, how do you tell the time?
CURLY: I take the ten minutes on this one and subtract it by the twenty minutes on that one. Then I divide it by the two in the middle.
MOE: Well, what time is it now?
CURLY: [takes a clock out of his jacket pocket] Uh, ten minutes to four.
Admin
Admin
That's also a part of statistics called "confidence."
Admin
Admin
java is an object-oriented language. He's obviously over-ridden .hashcode