- 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
Admin
setDelay("WillWeHaveDinnerTonight")
Woah there big boy, slow down and keep those pants on.
Admin
Admin
Which is why it is a REALLY BAD idea to compare those strings with '=='.
In Java, == is REFERENCE EQUALITY, so it works out if the strings are stored at the same memory location (so it is the same string object). Due to internal optimizations in the JVM, it may indeed happen that they are, but it is not guaranteed. If both strings are initialized from constant within the same class file, chances are very good. It may even work with constants in different class files. But if one string is user input, no way.
Admin
It works, if you're handling Unicode that needs normalization you don't want a String class to do that automatically since there are different ways to normalize.
Admin
Ah, yes - that's a plausible explanation for the claim. Yeah, equals() is testing equality, not equivalence - two characters might be equivalent in usage, but if they're not the same Unicode codepoint, they're not equal.
Admin
Conversely, C# has an operator called "==" which is equivalent to the object.ReferenceEquals(object) method, except for string where it is equivalent to the string.Equals(object) method. Clear as mud...
Admin
Admin
Hello Nagesh, haven't seen you around lately. It's good to see you commenting again. I hope all is well.
Captcha: quis - My brother's name is Quis, short for Quistopher Wobin.
Admin
Enlighten me: why is this an antipattern? It makes short work of the calling code:
... which is considerably more maintainable than:
particularly if there is a number of different "someDatabaseCall" functions and invocations of "doSomething".
Admin
I had to compare two arrays once. Zillions of times, because of a "Project Euler" challenge.
I did not find a function to compare arrays, and so I calculated the CRC-32 hash for each array...
By the way: Semper paratus!
Admin
If
is seriously that unpredictable then it's worse than "always returning false" -- if Paula places some unit tests in place that specifically happen to test strings which (for one of the above reasons) evaluate as the same, she will think her code works as expected.Admin
C# also has an upper case 'String' keyword that compiles to the same as 'string'.
Admin
Yes, this is one of the warts of Java that bites pretty much all programmers one time or another, despite it being pretty much the first thing that is told when reading up about string equality checking in Java. Rule of thumb for Java, if you are using == for anything but primitives, you are doing something wrong. Exceptions exist, of course, but if you are writing code that is one of those exceptional cases then you generally know the ugly warts of Java pretty well.
Admin
Bluebottle: Eccles, what time is it?
Eccles: Hang on, I've got it written down on a piece of paper....It's 6 O'clock!
Bluebottle: Where did you get the piece of paper?
Eccles: I asked a man the time, and he wrote it down for me.
[Pause]
Bluebottle: Eccles, what do you do if someone asks you the time and it's not 6 O'clock?
Eccles: I don't show them the piece of paper !
Admin
I think you mean System.String. String could be anything, depending on what namespaces you are including.
Admin
Ignoring the fact that the two bits of code do different things, the second is much more readable IMHO.
Admin
Admin
Admin
Bonus WTF in the source:
Using single-quotes for tag data and string apostrophes? Sign me up. Your HTML parser won't be confused at all.
Admin
Because the proper code is just having this:
And if you want to cancel out the reference:
//DoSomething();
Having that extraneous argument is pointless and toxic to your code.
Admin
HTML: forgiveness by default
Admin
Admin
There's no way it could be deliberate is there? Like the kind of code I saw when given a task as part of a competition to find all the security vulnerabilities in an application. Some other code would be stopping an unauthorised user sending "yes" but not stopping an unauthorised user working out another string with the same hash code.
Admin
What do you have against this pattern:
And why is any of them "more maintainable" than the others?
Admin
Written and performed by members of The Goon Show
Admin
public static void main(String[] args) { setDelay(null); }
public static void setDelay(String delay) { String yes = "YES"; if ((delay.hashCode()) == yes.hashCode()) Scenario.delay= 10000; }
Exception in thread "main" java.lang.NullPointerException
Admin
TRWTF is the fact that he uses a setter to set the delay but inside that method he uses an ugly "Scenario.delay= 10000"
Admin
Excuse my ignorance - What is an example of a perfect hash?
Admin
Ok, I see a perfect hash for a closed set - how do you create one for an open string?
Admin
Admin
Admin
I have a file that can handle the true. It's right here ... um ... somewhere ... now where did it go ...
Admin
No, you can't do that, because
String
is very deliberately afinal
class.No, what == does is completely predictable. The "unpredictable" part is whether you're being passed a String which has been intern'ed or not, and if you find yourself asking the question you've probably done something very wrong.
Admin
[code]if(s1 == s2) // Note: Reference comparison { // Do something }[code]
That's what comments are good for.
Admin
Paula should have been asking whether the two strings contain the same characters instead, if that's what she wanted to know. Her oversight was caused by her failure to RTFM, not because of any inconsistency in the language.
Admin
By what metric is the second version less maintainable? Because it has 6 more characters of code?
Maintainability is adaptability to changing requirements in my opinion. The only scenario where the first is more maintainable would be if you decide you want to ignore the condition in the future and do always nothing or always something, in which case the calling code would become misleading.
Now consider these changes:
You want to add another condition in one particular instance. Do you:
a) add another parameter to the function, forcing every other call to include a redundant "true", or b) wrap it in an "if"?
Or suppose you want to also doSomethingElse() when the condition is true. Do you:
a) add a parameter to doSomethingElse (which is impossible if you don't own the function) and repeat the condition, or b) wrap it in an "if"?
I hope you'll agree that in both cases, option B is preferable, and it would have saved you the trouble if you had just chosen it in the first place.
Admin
Thanks for warm welcome!
Admin
Umm ... no. String.equals compares the characters in each string. Characters are 16 bits to accommodate Unicode. Maybe you have some particular application that doesn't work, but the problem is not String.equals.
Admin
Well, I understand the problem, I fell into the trap a few times, like probably every Java programmer in the world.
But suggesting that there's a problem with the design of the language because incorrect code might sometimes work ... that just doesn't hold water.
To take a trivial example, suppose I write "y=x*2" when I should have written "y=x+2". This code will sometimes work: if x=2. Does that mean that there's a flaw with Java's arithmetic operators? No, it just means that I made a coding error that, under the right conditions, will manage to give the right answer. There are lots and lots of ways you can do that. I've probably made thousands of such coding errors.
Admin
+1
if they were the same, they'd be one character.
Admin
"By what metric is the second version less maintainable? Because it has 6 more characters of code?"
Partly. Shorter code is a good reason of itself. In general, shorter code is easier to read, because I don't have to grasp so much material at one time.
But mostly, because it includes a piece of logic only once instead of multiple times. If in the future we decide that some of what this function does should happen all the time while other things are only done when the flag is true, we only need to change the one function, and not every caller.
If someone adds another call to this function, with the parameter, he will know that there is some "are you sure" condition that needs to be checked. Without the parameter he might overlook that fact.
As to your horror stories: Why is having a function with two parameters a big deal? Is it because it's more characters to type when you write the function call? But you just finished saying that "more characters to type" is not a valid criteria for deciding whether code is good or not. I have functions with more than one parameter all the time and it doesn't traumatize me.
Admin
Code is not easier to read by virtue of being shorter. "if condition then action" is very straightforward and understandable. "action(condition)" is less so. If the syntax in question was particularly unwieldy, you might have a point (but even then, writing some kind of "if(condition, action)" helper function would be preferable to baking it into individual functions), but we are talking about a two-character keyword and some braces here.
The fact that there would be more than one parameter is not the "horror story". The problem is that it only exists for certain cases, and in every other case you're including "true" for no other reason than the signature demands it.
Regarding "Without the parameter he might overlook that fact (that something needs to be checked)" -- This should be a red flag to you. You should not be in a situation like this in the first place, where two processes are co-dependent and a caller needs to be responsible for both of them. Again, the check should be part of a self-contained process.
Admin
this article sucks
it was not funny
you had a long section for a single joke
the real joke is in your pants
Admin
Admin
TheDailyWTF-tradtions forced me to write this:
Adverse effects: Might overflow the intern pool. Still possible: NullPointerException (like in the original).
Admin
It's a little more broken than that. Here's a C program that computes a preimage for any arbitrary String.hashCode() very very quickly, with only printable characters. If I /don't/ restrict myself to printable character's, String.hashCode() becomes very very laughable.
Aren't non-cryptography hashes fun~?