- 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
This kind of code makes me miss C-style macros.
Admin
comment == "frist" ? "frist" : (String) null;
Admin
Is the WTF the code itself or the fact that if either option is null, it returns true?
Admin
Wow...it's as if Java didn't have String comparisons built into the language...
Admin
It's an asymmetrical equals. If s1 is null and s2 is not null, return true. If s1 is not null and s2 is null, return false.
Admin
Wow, never did Java, but surely, to move from one server to another, developers do not need to go through each line of code. What ever happened to write once run everywhere?
Switching server farms should only involve looking at config files IMO.
Admin
So the real WTFs are:
What about those (also without Javadoc comments ;-)):
Admin
This is an error from Firefox users. What the hell did you do to break the layout in Firefox?
Looks like your HTML meta-comment might be responsible since none of the HTML after this point is being rendered. I'm posting this message from IE6 (excuse me while I take a shower, I feel dirty).
Admin
That implies the code is sane in the first place. You wouldn't believe the number of developers who think that hard-coded strings are an acceptable form of configuration.
Admin
TRWTF is whoever use "--" inside a comment tag.
Admin
Please fix your HTML comments for those of us that use a real browser. I'm not enjoying this re-visit of IE6.
Admin
Awesome meta-WTF guys, better than today's article IMO.
<!-- Gotta watch out for those HTML comments! -->Admin
Well, you have to WRITE your code to take advantage of that. If you hard-code server names and IPs into your code, it doesn't matter about 'Write Once.' It will only 'run everywhere you have the exact same servernames/IPs'.
Admin
Bah, whatever! Just use find/replace and run the new server in a test environment for a while. You should be able to find and fix most of the bugs. Then throw it over the wall and let the users report whatever else you missed. The introduction of new server should be an exciting event!
Admin
this is an error from postComment() method only accepts meaningful sentences, what the hell did you do?!
Admin
Not everybody wants to use a 'real browser' or even cares to install Lynx.
Admin
However, your having two "-->" is TRWTF...
Admin
If it's correct than it won't break Firefox's layout. In fact, "--" is disallowed in comments in HTML 51 and XML2.
Admin
Admin
Wow, comments are broken bad. Half of them seem to be missing, and what's with the new colors?
Admin
Admin
Getting back to the subject at hand - What is wrong with writing something that saves you having to explicitly check for null before calling string1.equalsIgnoreCase(string2)?
Admin
Well, it wasn't /intended/ to be asymmetrical, if I read the comment correctly; and it's the thought that counts really, isn't it? :)
Admin
Ahh, I see the problem. Nothing wrong with the intent, it is the execution that is flawed.
Admin
[quote user="grzlbrmft]What about those (also without Javadoc comments ;-)):
[/quote]Almost. I think the correct version would be:
But seriously, where is the WTF? He wants to compare two strings, both of which may or may not be NULL, without cluttering his main code with IFs. It's clumsily written alright but still a valid cause. In any case not nearly enough to pull an "enough is enough".
P.S.: You don't have to abandon all morals and use IE6. IE8 will do just as well.
P.P.S.: jumentum.
Admin
So I don't "do" Java, but I would have assumed that (s1 instanceOf string) would return false is s1 was null, and (s1 instanceOf Integer) would also return false is s1 is null (how can null be either a string or an integer?). So, therefore, if s1 actually is null, you'll get an error. So the function actually doesn't allow null at all. Am I wrong?
Also, the comment "allow me to compare strings and Integers" would lead me to think you could compare 1 string to 1 integer, which this function also doesn't do.
Admin
Quick Google search and it looks like I'm right:
http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm
So if s1 and/or s2 are actually null, this function won't return true, it'll give you an error.
The "tweaked" method, I think, does at least actually work.
Admin
Right, all versions of Firefox are 100% compliant with all DTDs.
Admin
I am reading and writing this from Firefox. It looks fine to me. I wonder what the difference is?
Admin
Correct. In Java, null is never instanceof anything.
Admin
Almost. If o1 is null and o2 is not null, you'll have a NullPointerException.
Well, it does not do what it advertizes (i.e. the whole "s1 is null s2 is not null results in true" story mentioned above), and the first version with the funny casting behavior is, in my book, a pretty resonable "get me out of here" moment.Admin
Admin
The ternary operator is correct here, but the WTFer got it wrong. You need
Since a compliant equals method will always return false for a null argument.
Admin
Wow, how is everyone coming up with such crap null-safe equals methods?
Admin
TRWTF = "We are currently narrating this post."
Admin
Another WTF, the comments mention:
... for which i write test cases works like a regular equalsIgnoreCase except ...
The developer obviously did not write the simplest test cases to validate the building block of some more complicated test cases.
assertTrue(equalsAllowNull(null, null)); assertFalse(equalsAllowNull(null, "a")); assertFalse(equalsAllowNull("a", null)); assertFalse(equalsAllowNull("a", "b")); // new() to prevent compiler from reusing same instance... assertFalse("a" == new String("a")); // make sure code is using .equals and not == assertTrue(equalsAllowNull("a", new String("a")));
Admin
Admin
TRWTF:
System.out.println("strcmp 1:"+s1+" 2:"+s2);
This is what happens when you let bad C programmers use Java - they try to make Java behave the way they're used to, as opposed to learning to work with it, and write the stupidest, shittiest code as a result.
Another symptom of this: writing error messages to the console instead of throwing an exception when you get unexpected input.
Admin
This common problem and many like them have already been solved. http://commons.apache.org/lang/
public static boolean StringUtils.equalsIgnoreCase(String str1, String str2)
Its 2010, people. TRWTF is thinking that you have to write this method yourself.
Admin
Admin
Nope. That's not it either. In Java, null does not have a type and so the instanceof will not test true even if you send two so-called "String" objects. So if either of the inputs (or both) is null, then the result is the message at the bottom.
Utterly broken!
Admin
I say give Colin the cookie! This is almost identical to what I write, the minor difference being that I typically say:
Testing o2 against null is not necessary for any of the standard classes, which will cleanly return false if the value compared against is null. But you can't necessarily be sure about user-defined classes.
P.S. There is a lot of code in the system I'm working on these days that goes quite beserk about this, with code like:
Sometimes they'll retest o1 inside the o2==null test, I guess just in case the first o1==null test gave the wrong answer.
Admin
Oops, I used the wrong "quote code" convention on my previouis post. I was thinking I was on Stack Overflow. Sorry for the ugly formatting, but hopefully you get the idea. Or don't care.
Admin
The operative word here being "should". People "should" respect each other's property, but I still lock my doors at night.
Admin
Who the hell cares about user-defined classes with bugs in them? If they want your function to work with them, they can conform to the specification for equals, which explicitly states that anyNotNullRef.equals(null) must return false.
Admin
Admin
In that case, whatever happened to using config files that store that kind of server-specific information?
Admin
Maybe if the author had heard Tom loud enough, they could've written code proofread enough.
Admin
Okay, someone please tell me I'm blind and I've missed something and explain why does s1==null return true? Also what's the point of casting string into string ? sure it looks cool with all those brackets, but it still hurts my eyes.
I know this is the daily wtf, but having that in a "fairly large Java client/server application" someone had to see the error sooner!
CAPTCHA: uxor - upper case XOR, used to compare strings and integers!
Admin
TRWTF is that it's 2010 and Java still needs a method call to test for object equality. In real languages, you use == to compare objects (or at least strings) and null == null (or whatever term the language uses for null)