• Alan Bellingham (unregistered)

    You can blame a lot on C++, but I'm pretty sure no self-respecting C++ compiler would even accept that code.

    Java, perhaps.

  • Chris Heald (unregistered)

    That...looks a lot more like Java than C++.

  • RJM (unregistered)

    I must have my stupid hat on, you say this doesn't work, but I can't spot the bug.

    I cant even figure out what language this is written in. "compareTo(Object o)", come on, Object isn't a value type. And "Character"?

    I'm thinking this is Java.

  • Ron (unregistered)

    Reading this code, it does some insane things. It appears to sort MirageObjects by their labels: primarily according to string length, and then to break ties when strings are the same length, it sorts them by their last character.

  • Ry4an (unregistered)

    Though it's not really a legal java.util.Comparitor he's using, I wrapped it in one. Despite not quite honoring the rules of the comparitor() method, the collections sort using the test above did this with the text in the intro to the post:


  • Guayo (unregistered)

    Yes, that's java, not c++ or I'm looking at some fata morgana.
    About the code there are a few things I can say.
    First the catching of Exception, it's ugly! You can like or not checked exceptions but swallowing any type of exceptions like the code above is hardly ever a good idea.
    Not to mention why should a getter method throw anything (unless there is a remote call or something like that, but I doubt it) anyway this brings my second issue with the code above? Why to construct an empty immutable string at all? (using 2 different ways, BTW).
    Third. Only when myL != toL the code returns something different from the built in String.compareTo so at least like 10 lines of code could be replaced wit a built in method call.
    But having said that I think the code above is not in the same order of previous WTF posts.

  • Diego Sevilla (unregistered)

    Come on. You show little knowledge on C++ saying this is in fact C++ when it is clear it's some form of Java. Moreover, little knowledge of C++ makes one say things like "it does not have garbage collection, array bounds"... A good C++ programmer don't need these. Seriously. You don't need array bounds checking when you correctly use iterators (STL iterators, please. Not Java ones), STL functors, etc. I won't lose any time explaining them, but please, speak properly.

  • Larry Osterman (unregistered)

    And of course the code fails MISERABLY when presented with actual multi-lingual data:

    See http://weblogs.asp.net/larryosterman/archive/2004/07/01/171078.aspx for more info.

  • Alex Papadimoulis (unregistered)

    @Diego, the things I said about C++ remain true; you must do all of those things. That doesn't make it a bad tool, just a pain in the ass to use for those who've gotten used to the fool-proof (-prone?) Visual Basic.

  • WanFactory (unregistered)

    I'm not sure what "it doesnt work" is supposed to mean. Its not a standard sort for sure and its implementation kinda stinks and in one block it could use a standard library function instead of rolling his own.

    The length check and the case-sensitivity definitely raise eyebrows but how are we supposed to know whether to consider this odd behavior as a bug or requirement?

    As for to.getUserLabel() throwing an exception, this easily throw our favorite NullPointerException if Object o happens to be null. Though every time I guard against null in my compare to, I briefly consider strangling the moron who stuck a null object inside a collection for sorting in the first place. But my murderous frenzy usually (but not always) subsides when I begin to suspect that I see the moron every day in the mirror.

    Its a fairly minor WTF all things considered. Nothing compared to other (java) doozies like the switch statement in the for loop.

  • OCD (unregistered)

    Sorry to pick nits, Alex, but you spelled "definitely" with only one 'a'. I'm sure you could dafanatalay find room for at least three in there if you were really committed to quality.

    Diego: In real life, sometimes you do have to deal with pointers, and array bounds become an issue. Comparing C++ to Java is idiotic in any case, regardless of which one you prefer. Apples and oranges. They serve different purposes.

  • Dave (unregistered)


    If we are being nitpicky -- "Apples and oranges"??
    Actually, those are easy to compare. Both edible, fruits, with seeds, round, with skin, grow on trees, contain sugar, make juice, etc., etc.

    If you are being nitpicky, then please do not use such inaccurate cliches. Refer instead to comparing apples and shovels. Now THERE is a comparison of two things that 'serve different purposes'.

  • OCD (unregistered)

    The purposes served by apples and shovels are enormously more differenter than them what's served by apples and oranges.

    You could say a shovel and a pickaxe, maybe, or an orange and a banana: You can't plausibly say "I can't hear you, there's an orange in my ear". For example. So those two differ in at least some significant ways, but they're not fundamentally different, which Java and C++ aren't either. I mean, they're both programming languages with curly braces, after all.

    By the way, the skin of an orange is usually called a "peel" or even a "rind"; it's not at all like the skin of an apple. Also, apples are crunchy. Ontologically speaking, that's a major issue. Borges tells us that the Chinese, 2000 years back, classified animals in similar way: Categories included animals which are very small, animals which hide under the couch, crunchy ones, and those which belong to the emperor.

  • Chris (unregistered)

    That is really, really messy code. Even if it did work I would have the author re-write it on general principle. What kind of exception would be thrown on getting a label? LabelNotFoundException?

  • Wayne Barker (unregistered)

    Java? But it also looks alot like C#.Net Syntax, except that i don't reconise the object "MirageObject"

  • Alan Bellingham (unregistered)

    I suspect that the author is a C programmer attempting to rewrite the C strcmp() in Java (or C# if it is that) ... and getting it horribly wrong (read the source, Luke!).

    As for the comments on garbage collection in C and C++: it's a state of mind. In C, you're expected to put stuff back where you got it, like a tidy child, not just drop it on the floor and wait for Mummy to tidy it away fro you.

    In C++, you'll use objects that automatically tidy themselves away when you drop them (std::string, std::stream, etc.) - this is the RAII idiom, which is sadly lacking in both Java and C#.

  • Dave (unregistered)

    If you can't tell C++ from Java then you are not competent to declare code "WTF". I warmly encourage you to stick to dodgy VB code, SQL, C# and the like which you seem to understand.

    It is completely normal to implement your own compareTo method - indeed it's required by the Comparable interface which must be implemented for your objects to be sortable.

    Furthermore, it's just not obvious that this is supposed to be sorting into lexical order, since we don't know what the MirageObject is intended to represent. Yet again, we don't have the context for the code, so why is it "wrong" ?

  • Alan Bellingham (unregistered)

    Dave has a good point - if the code is meant for use in an ordered sequence container (the equivalent of C++ std::set/std::map), then it merely matters that the ordering is consistent.

    If so, then the fact that names of different lengths are sorted first is actually good, since it provides much higher efficiency than a lexical comparison within sort routines.

  • Alex Papadimoulis (unregistered)

    @Dave, If I pay enough attention, I certainly can tell the difference. I thought this was C++ because it was in my C++ folder. It was in my C++ folder because all of the other submissions from David Shay were C++.

    At first glance, it made sense that this was C++, because W(hy)TF would someone even think of doing this in Java, as it has native support for strings. C/C++ has no native support for such a structure. It's only in the STL.

  • KoFFiE (unregistered)

    The purpose of this code could maybe be justified as the 2 posts above say (I personally doubt it), but that's no excuse for the horrible while-loop which creates 2 Character objects to compare each char. All performance gains he could maybe have won with comparing the lengths he loses more than big time in that loop when the string-lengths are the same. Don't forget most of those functions in the standard classes are implemented in pure C/C++, so I'm pritty sure there is no gain at all...

    But well, I really LOVE the way he initializes the i as -1 to be able to use the (++i < toL) comparison :) I think I'll start doing this also.. (j/k :D)

    Then what else is "wrong" with the code? First of all, you can just compare 'a' > 'A' of the 'char' type, without creating a Character-class instance for it. Substracting and adding also perfectly works, so it should have been:
    ret = myName.charAt(i) - toName.charAt(i);

    This should already be a lot faster. But offcourse he could have simply used the following line instead of the stupid loop:

    return myName.compareTo(toName);

    Well waddaya know, that also works...

    This kind of guys give java the name "slow", but if you know what you're doing, you can make it pritty fast (for an interpreted language that is :))

  • Dave (unregistered)

    While most of KoFFiE's points are fair enough (that loop doesn't look efficient) he compensates with this comment:

    'Don't forget most of those functions in the standard classes are implemented in pure C/C++'

    Absolute gibberish. Most of the functions in the standard classes are implemented in pure Java. VERY few of them are JNI native methods.

    This code might be a WTF (perhaps it IS supposed to be sorting to lexical order), but the lack of context makes it impossible to tell.

  • Bob (unregistered)

    Don't bloat my code with unneeded crap like garbage collection and bounds checking. There's a reason to work on design ahead of time rather than relying on the compiler to look over your shoulder.

    Every time I use java, it feels like I'm including a fat, ugly, mother-in-law to look over my shoulder and say, "Bad programmer. Bad. Don't do that."

    Where as c++ is on the other side of the scale (I don't touch mfc or any of that crap) and if I'm making a simple, straight forward application, I don't have to include a bunch of classes that make my code, yes platform independent, but also platform stupid. I mean, if I can use a system call, just use it and be done with it. And I don't think a site which is "powered by ASP.net" has any room to complain about portage issues.

  • Aarrgghh (unregistered)

    > Most of the functions in the standard classes
    > are implemented in pure Java.

    You're joking, right? Please tell me nobody would do anythng as dumb as that. Or at least, can we hope that it's true only of Sun's implementation? If not, I guess KoFFiE was mistaken about what "give[s] Java the name 'slow'". There's nothing you can call that but a deliberate pessimization.

  • Dave (unregistered)

    You see, this is why I think you guys should stick to VB, C#, and SQL, where I've seen mostly sensible commentary.

    You didn't know that the Java libraries are mostly written in Java, yet you feel confident that you can pronounce on this as a design decision. How old are you, twelve ?

    It's been discussed to death elsewhere, but just google for "Just In Time" (JIT) and HotSpot and you'll see why the decision isn't as clear cut as you seem to think.

  • Dave (unregistered)


    You seem to be missing something: There is more than one language to choose from. If you want a lean mean binary that is under your complete control, then don't choose Java.

    On the other hand, if you want a massive scaleable integrated system that's portable and plays nicely with numerous other technologies you'd be insane not to choose Java.

    Use the right tool for the job. 'XYZ is a crap language because ABC' is the kind of argument that makes you sound like an infant.

  • Bob (unregistered)


    I concede the point that you should use the tool which is best for your application. If you're developing software for the average user, don't complain that VB doesn't give you complete control over pointers and such. But wouldn't that make the point of this article moot? I mean, if your task is only to compare strings then wouldn't you use something that is better optimized for comparing strings?

  • Dave M. (unregistered)


    'don't complain that VB doesn't give you complete control over pointers and such'

    I didn't. Possibly another Dave did, in which case he's an idiot. I don't personally like VB, but it doesn't follow that it's a bad tool.

    'But wouldn't that make the point of this article moot?'

    "No" since we're discussing a specific code snippet, or alternatively "Yes" since we don't know anything about the context in which it arose.

    'I mean, if your task is only to compare strings then wouldn't you use something that is better optimized for comparing strings?'

    compareTo is usually implemented to compare objects (not "strings" as such). For example, you might choose to order a list of Staff objects by staffName field. It looks like something along those lines is being done in the original snippet. But without knowing how that list was to be sorted and the rest of the requirements, it's impossible to say that this code is right or wrong.

    It might be the dumbest code ever - certainly it's not as efficient as it might be - but it's a far from foregone conclusion that it's a WTF moment without knowing a bit more about it.

    So all I'm saying is that if I were vetting items for inclusion here my requirements would be that I (1) knew the language in question, and (2) knew the context in which the code snippet was being used. Even if I was really sure it was wrong...

    A code snippet that (s)he didn't even recognise as being Java hardly qualifies on either count.

  • Dave M. (unregistered)

    Oh, and to Alex...

    Please give us the context to the snippets unless they're so messed up they won't compile. That ObjectWrapper the other day was a classic case. There are 100% good reasons for wanting to do that - yet it was listed as a WTF. That makes you look like the idiot, not the guy who wrote it.

    Now, if the doofus was doing this in order to store it in an Object[] or something similarly cretinous that makes it quite entertaining.

    But without it you (on behalf of the guy's co-worker) just sound spiteful and possibly quite ignorant.

    If you don't get the context, reisist the urge to post it unless you're so sure of your ground that you're willing to bet your reputation on it...

  • Aarrgghh (unregistered)

    Dave -- Sorry, don't have time to wade through all the theology and apologetics, and I wish to hell I hadn't started a stupid language flamewar. I really don't give a damn about Java; enjoy it in good health. Whatever.

  • Dave M. (unregistered)

    You have time to run WTF but don't have time to comment!?

    I'm not blind to Java's pitfalls - I've seen things written in Java that would make your skin peel.

    But the above applies equally to VB, SQL, PHP, Perl, Intercal, Lisp... if you post the context to the snippets we can get on with mocking the pathetic coders of the world instead of bitching about the lack of information.

  • DrPizza (unregistered)

    "Please give us the context to the snippets unless they're so messed up they won't compile. That ObjectWrapper the other day was a classic case. There are 100% good reasons for wanting to do that - yet it was listed as a WTF. That makes you look like the idiot, not the guy who wrote it. "

    What are the 100% good reasons for it?

    I could understand IF IT HAD A SET METHOD--it'd then be useful for faking passing arguments by reference. But it didn't, so what the hell was the point of it?

  • Dave M. (unregistered)

    Collection c = new HashSet();
    System.out.println(c.size()); // 1
    c.add(new ObjectWrapper("Hello"));
    c.add(new ObjectWrapper("Hello"));
    c.add(new ObjectWrapper("Hello"));
    System.out.println(c.size()); // 4

    The distinction is of use in compression tool, serialization, building caches, etc.

    Someone who thinks that "faking passing arguments by reference" would be a good idea in Java is obviously a neophyte anyway.

  • DrPizza (unregistered)

    "The distinction is of use in compression tool, serialization, building caches, etc. "
    If you're writing a tool where string address is important rather than string value, Java is a pretty poor language to use (as for strings it forces you to conflate the two), a HashSet is a pretty poor container to use (surely you'd at least start with a container keyed by identity and not value?), and a class named "ObjectWrapper" is a pretty poorly named way to circumvent this fact.

    It's also not immediately clear why you'd want a cache to store multiple copies of identical values.

    "Someone who thinks that "faking passing arguments by reference" would be a good idea in Java is obviously a neophyte anyway. "
    I think it's rather more likely that they're someone who's not a convert at all than merely a newcomer, and as a non-convert they don't buy into Sun's deliberate dumbing down.

  • (cs)

    Wow! .....................

Leave a comment on “C++ does enough as it is”

Log In or post as a guest

Replying to comment #:

« Return to Article