- 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
Clever of Joachim to find such an able person to protect his job while he was gone :)
This is so bad that even I, who thinks that COBOL was a good language, can taste it's WTFness.
Admin
The real WTF is that such people get an assignment.
Admin
Senior "java" developer... he had some problem and I told him "use toString()", he then called me again "now I get NullPointerException" I went to see his code:
System.out.println(null.toString());
I fainted.
Admin
Admin
I agree, it's the first time I laughed out loud at one of these.
Admin
But, hey, what are new technologies like notebooks and Wi-Fi for?
Admin
This is a pretty lame WTF... I can see legitimate/semi-legitimate reasons to do most of these things.
As for the different ways of setting a string value (with the exception of the last, which is silly), it seems like these were probably taken out of context. On this site we always assume the worst of the submission, but without context can we really tell? Couldn't the context have been:
someString = anotherString.Clone();
Likewise for the casting and .copy methods...
I also don't see what's so wrong with re-using a variable name (in this case, docOwnerUsername). Yes, it's counterintuitive, but it can reduce memory usage on functions that recurse deeply (I got in this habit when I was doing a lot of C coding - where it also was useful to avoid the hassle of scrolling up to the beginning of your function to declare another variable). Naturally, this applies less in Java, where you can declare variables on the fly and, and you really should use a more generic name to avoid confusion (say, scratch or junk), but it is entirely possible this is just a strange habit being carried over to a new language.
As for setting the string to be "null" instead of null, that's lame, sure, but is it that big of a deal? Probably not.
Are any of these really all that horrible, except perhaps the double ToString() ? Nope.
Admin
Hah, no wonder. That solution is not scalable!
Object o = anotherString.ToString();
for(int i = 0 ; i != SCALABILITY_FACTOR ; ++i) {
Method m = anotherString.getClass().getMethod("ToString", null);
o = m.invoke(o, null);
}
someString = (String)o;
Of course, the true WTF is that they shouldbe using javascript.
Admin
I'm going to go with the assumption that this is C#, since the "ToString" method is actually part of the Object class there, so string does have a ToString function.
In this case, "string foo = bar" does indeed assign a reference, but that is in fact the correct thing to do. Strings in C# are immutable, so the concept of making a copy so you can modify it doesn't apply (you'd use a StringBuilder for that). You can make a copy of a string by using the Copy() method, though I'm not sure what use there is for that.
The String.Clone() method doesn't do what you might expect in C#. That is, it doesn't make a copy of the string, it just returns a reference to the string itself. That is, foo.Clone() is identical to foo.
Admin
Just when I think there's nothing that'll surprise me... *dies*
Admin
Operations: Henceforth no time will be allowed for any operation. The company believes that as long as you are an employee here you will need all of whatever you already have to do your job and you should not consider having any of it removed. The company has engaged you for a particular job with all your parts and to have anything removed would mean that the company is getting less than it contracted for.
Admin
This actually looks like C# to me, but most of what you said holds true in C#.
Admin
Admin
It's worth pointing out that he did use the Copy() method in one place. That does makes a copy of the string, whereas his other attempts just copy a reference. WTF?
Admin
Of course. It should have been:
String someString = null.toString();
Admin
Do I need to point out that the Java String class is immutable, so there's really no point whatsoever in cloning or copying it? You can just assign away perfectly happily...
From the horse's mouth: "Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared." -- Java 2 Platform API documentation.
Admin
But... a String... is an Object... brain explodes
Admin
...and then I notice that this is not Java. Sorry.
Admin
Too sad to have to endorse you, cause I was there too and catched you when falling...
See my previous post...
Admin
someString = anotherString.ToString().ToString();
It has to be this way. anotherString is a class with a ToString Method, but ToString doesn't return a string as you might guess, but instead an enhancedString object. So anotherString.ToString cannot be assigned directly to a string as the types are wrong. You have to call ToString on the result if you want a native string type instead of an enhancedString.
See, it all makes perfect sense once you know what is going on and why.
Admin
Because sometimes Strings get passed around as Objects, and you want to be able to convert Objects to Strings?
Generally, using toString is not the way to convert objects to strings. Using format or convert objects is better especially if you need to worry about internationaliztion or localization. The toString should really be used for debugging.
Admin
Man, it must really sting to see your code on here, huh?
Just think of it as a learning experience ...
Admin
If scrolling back to the top of your function is too much for you, one of the following is true...
No wonder you want to remain anonymous!!
Admin
Very good point. This weird rule was part of the WTF, to me. 8)
Admin
An ... acquaintance ... of mine had a heart attack while managing a fast food joint. She called her district manager, who responded with "well, can you finish your shift before you go to the hospital?" (only 7 more hours). Called her 2 days later to find out if she would be back for the weekend shift.
We need more leaders like this ...
Admin
Sorry, but that is just WRONG!
String someString = "null";
is not the same as
String someString = null;
So, later in the code, when others are checking for null, they're doing...
if (someString == null) {
...
}
and you just broke their software!
Admin
I guess that explains why some people go off to get implants... and then their pay gets increased.
Admin
Excuse me? If your function recurses, it doesn't matter if you reuse the variable or not. You are getting another one on the stack.
I reuse variables, they tend to be called "i" for an index. If you are worried about reducing memory (if you are why are you using java or c#?) then scope your variables. This will also save the tediousness of hitting the page up key.
Admin
I want to reply to several things that have been thrown around. I'm going to paraphrase them:
It obviously should not throw an exception, because if you've got a String object reference in an Object variable, you shouldn't be shot for using .toString(). And using .toString() is fine for lots of purposes (most UI components do this to some extent, e.g. JTable, JList, etc.) If you need i10n then you plan more carefully, of course. I do this a lot for the same reasons: save memory, space, etc. But that is not a good reason. Any half-ass optimizing compiler will know that you only use a variable between some two lines, and optimize the stack usage. Granted if you take pointers of the variables (think C here) it will have to optimize more carefully.Premature optimization is a Bad Thing. Why do so many people on this forum need to be reminded of this? Including me; I do this often. It's a nasty habit.
Shut the hell up. It's not funny anymore. Except that in both Java and C# (or any .NET language) [Ss]trings are immutable. Making a copy is a bad thing because it duplicates memory use for no good reason. I cannot think of a single instance when you want two different immutable objects that contain the same data. Maybe it would happen e.g. results from a read-only database, but why would you specifically need to request a copy of an immutable object?My $0.02.
Admin
Reminds me of a memory leak I had to track down. If I remember correctly it went something like this (paraphasing the code)
CBstr *cbstrOrginal = new CBstr();
CBstr *cbstrNew = cbstrOriginal.Copy();
CBstr is a class that wraps a BSTR. Of course it overrides the = operator, which does the appropriate thing and makes a copy of the BSTR. So TWO copies of the orignal was created, one was assigned to cbstrNew and the other was leaked.
Admin
Everything you guys have been harping on are all minor WTF's. Reusing one variable for random text? Bad practice, yes, confusing, certainly, but not by any means a major WTF. To me, the real WTF here is that he wrote a 500 line function - apparently the highly paid consultant hasn't mastered the concept of proceedural programming.
That said, I can't in good conscience think of this as a real WTF - I think it's more of a CF - Cluster Fsck.
Cheers.
Admin
Mostly so that every object can have a functional ToString method. Works nicely when you want to have an abstract way of getting a text representation of an object value.
Admin
And the other rule is, we all seem to think that we're the one good programmer out of your 111.
Admin
void CopyString (string& rzOut, const string& rzIn)
{
return CopyString(rzOut, rzIn.ToString());
}
...
CopyString(someString, anotherString);
Make sure the job gets done
Sincerely...
Admin
using .toString() is fine for lots of purposes (most UI components do this to some extent, e.g. JTable, JList, etc.)
I know you were using JTable as an example but if you look at the output of JTable.toString then it obvious that the method should not be used except for debugging.
The output of JTable.toString is
getClass().getName() + "[" + paramString() + "]"
where paramString() returns a csv of the various member variable values. Not a useful format for display. Also the javadoc for the paramString() method says the "method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations." Not very useful if you need to support multiple jvm versions.
Admin
Where can I get this job. I could be second highest
Admin
What the hell is wrong you people who don't think this is a WTF? Okay, the variable name reuse is shitty, but it won't break anything, and the inconsistent strings, and multiple ToString()s the same. but someString = "null"? As has already been pointed out, this will break things. If it doesn't break it, you'll get incorrect data in your output, and possibly even shown to the user. Your company effectivly looks as incompentent as you. Let's just hope this isn't air traffic control or tanning booth management software, where someone(s) really could die as a result of this.
Admin
Hello WTF-ers,
I find this site very amusing and yet exasperating. Are these the work of our past, present and future co-workers? Not to mention those of whom we'll never get the pleasure...
In any case, I've noticed that a large part of these WTFs are due to last minute hire or essentially the unresearched hire of a contractor. I highly suggest that the next time you are put in a position to hire a developer of any sort you ask for their code samples. Just like a postfolio, ask the propective hire to submit some of their best past work.
If their code samples look like anything that could be posted on this site don't hire them!
Like I said, this site is amusing but after a while I just get pissed off at all the lost time and money and plain old lack of professionalism.
Software Developer
Admin
You misunderstand. toString() is usually used as the display text for cell rendering components in JTree, JList, etc. If I have MyCustomClass and I add an instance of it to a JList, the output of MyCustomClass 's toString() method will be displayed.
Admin
The idea is that a method could recieve an Object, and want to get the String value (for debugging, etc), but since the method is getting an Object, it has to call toString() to get the String value, and it would be silly to test the Object to see if it was a String, just so you could avoid calling toString();
For example, if log() is defined
public void log(String s) {
// do something here
}
then this makes more sense
public void contrived(Object o) {
log( o.toString() );
}
rather than
public void contrived(Object o) {
if (o instance of String) {
log((String) o); // all this to avoid calling toString() on a String!
} else {
log(o.toString()); // oh, its not a String, so we can call toString()!
}
}
Yes the example is contrived, but it shows a way in which you would end up calling toString() on a String, without seeming completely silly.
Admin
BTW, this turns out to be irrelevant. Try this in Java or C#:
String foo = "foo";
String bar = "foo";
String baz = bar;
foo==bar, foo==baz, and bar==baz will all evaluate to true (same instance). With any class other than String, this would be an issue, but after:
baz += "more";
now baz!=bar. A seperate instance isn't created until necessary. The compiler can make this optimization because strings are immutable. BTW, C# interprets == as a value comparison for strings and Java interprets == as a reference comparison. Notice how the C# method produces the same results and doesn't make you remember to use .equals() or else scratch your head for 20 minutes trying to find out what's wrong.
So, .Clone() is unnecessary as the language will create a new instance for you when you modify it.
Admin
<font size="2"><font face="Courier New">someString = String.Format("{0}{0}", Convert.ToString(</font></font><font face="Courier New"><font color="#0000ff" size="2">"").ToString() + Convert.ToString(new</font><font size="2"> String(((</font><font color="#0000ff" size="2">string</font><font size="2">)</font><font color="#0000ff" size="2">string</font><font size="2">.Copy(anotherString.ToString().Clone().ToString()).ToString()).ToString().ToCharArray()))).Substring(0,anotherString.Length-1);</font></font>
Admin
Hmmm... think he's still talking about the code samples? [8-)]
Admin
I think these are once again hard evidence that consultants != software engineers.
Admin
Serves them right for having such a ridiculous code guideline.
<ideological>
They should teach definite assignment semantics and their advantages in school, and ban C programmers from making code guidelines for C#/Java. And while ya there...
</ideological>
Admin
Admin
Admin
Admin
It makes sense that he's so expensive.
When you're that incompetant it can get really hard to get jobs. When you DO manage to find someone willing to take you on you gotta make enough to live unemployed until the mess you make blows over.
Admin