• Anon. (unregistered)

    List<String> items = new ArrayList<String>(Arrays.asList("Frist".split(","))) String itemOne = items[2].toString()

  • Quite (unregistered)

    It's converting from a one-based array to a zero-based array, perhaps:

    itemZero = items[1].toString()

    posterior-brained way of doing it, I'll admit ...

  • Quite (unregistered)

    ... or perhaps the first couple of elements are placeholders of some kind, or otherwise ignorable elements.

    Still doesn't excuse it's fundament-headedness.

  • Mike (unregistered)

    Well, well well...

    Apparently you live under a rock and haven't had much to do with CI engines... Groovy is very much alive and kicking...

    Not a quality WTF more than a rant about being ignorant... I expect much better from this site!

  • Sole Purpose of VIsit (unregistered) in reply to Mike

    I'd say it's a decent-quality WTF, in the sense that anybody unfamiliar with Groovy (me, for instance) is going to go "WTF?"

    The fact that Remy is concerned with something as piffling as a variable name, when the very same line seems to need toString() to "convert" something that has apparently been extracted from a collection of String into, er, a String .... well, either this example is even more borked than it looks, or else three familiar letters spring to mind.

  • Leo (unregistered)

    TRWTF is that Groovy is supposed to be a dynamic language. Instead, the code in this example looks very much like Java, except the items[2] that would be items.get(2) in Java.

  • zudljk (github)

    The first line could actually make sense, if the original coder wanted to append or remove elements from the list later on.

    split() returns an array, so to modify its length, you need to convert it into a list. That's why Arrays.asList() is there. However, you still can't modify the resulting ArrayList, because the instance is of a specific subclass that's unmodifiable. If you want a "real" ArrayList, you've got to feed the result of Arrays.asList() to "new ArrayList<>()".

    The real WTFs I see in the first line of this code are 1. that split() returns an array and 2. ArrayList doesn't have a varargs constructor, but you'd have to blame that on Java, not Groovy itself.

    Another WTF is that this is meant to be an example for Groovy while it's not even using Groovy syntax. Converted to Groovy, this code would look like this:

    def items = string.split(",") as ArrayList def itemOne = item[2]

    Addendum 2017-07-31 07:28: Edit: The last line is missing a line break after ArrayList.

  • AL (unregistered)

    Without knowing what else is being done with "items", it's hard to call this a big WTF... this is also almost 100% Java code (which is also perfectly valid groovy). The final toString() is unnecessary, but certainly not really deserving of a whole article.

  • Martijn (unregistered) in reply to Sole Purpose of VIsit

    The WTF here isn't Groovy. The code shown is Java, which is a subset of Groovy. Only a Java programmer would program like this in Groovy. In Groovy, you can simply do:

    def items = data.split(",") as List def item = list[2]

    If you care about typing, you could make it:

    List<String> items = data.split(",") as List // or possibly as List<String>, it's been a while since I've done Groovy.

    The WTF about the itemOne = items[2] has nothing to do with Groovy or Java; it's just a programmer being silly, which you can do in any language.

  • Martijn (unregistered) in reply to Martijn

    I guess the real WTF is that thedailyWTF's editor starts adding

    tags only halfway through my post, and adds closing </string> tags at the end because I'm discussing generics.

  • my name is missing (unregistered)

    Also itemOne might be perfectly reasonable, as the first useful item in a list of things, the first couple of entries could be unneeded identifiers and this field is actual what they are interested in. I think the WTF is that the WTF is not a WTF.

  • (nodebb)

    Yeah... I wouldn't put this one on the language, but on the programmer being an idiot.

    In java, just as in groovy, all this mess can be written as:

    String itemTwo = data.split(",")[2];

  • Matthijs (unregistered) in reply to LordOfThePigs

    Careful. Down that slipperly slope lie people excusing PHP as "it's not the language but all the idiots using it".

  • Scott (unregistered)

    Seems like the biggest problem is not checking whether the List we came up with actually has two or more items before indexing into it?

    But, then again, not knowing this use, maybe throwing an exception is the right thing to do if you have a short list,

  • idioot in de boot (unregistered) in reply to Matthijs

    but it IS the idiots using (abusing) it. not that I care, I work in a Real Language

  • Zenith (unregistered) in reply to AL

    Definitely weak. I thought my recent submission might've been a little on the weak side but if this is all the higher they're setting the bar...

  • Ulysses (unregistered) in reply to Zenith

    +1. STILL waiting for mine.

  • Karl Bielefeldt (github)

    I have to write groovy occasionally for Jenkins integration. It is by far the most interesting programming language to try to google. My favorite is the time I was trying to connect to an SMB share from a Jenkins job, and it was a Linux host, so naturally I googled "groovy samba."

  • Wim (unregistered) in reply to zudljk

    and item should be items?

  • Alain Van Hout (google)

    Seems like run of the mill (but poorly written) Java. Assuming you're not interested in adding out-of-bound checks, the code can be reduced to:

        String[] items = data.split(",");
        String itemOne = items[2];
    
  • (nodebb)
    String itemOne = items[2].oneString()
    

    FTFY

  • ooOOooGa (unregistered)

    Ah yes. The infamous off-by-two error. A very common and easily made mistake.

  • Paul Neumann (unregistered)

    Not much different than this semi sanitized code snippet I've come across many times:

    public object DoQuux(int param1, long param2, string param3)
    {
        Log.TraceFormat("{0}: param1 {1}, param2 {2}, param3 {3}", nameof(DoQuux).ToString(), param1.ToStringEmptyIfNull(), param2.ToStringEmptyIfNull(), param3.ToStringEmptyIfNull().ToString());
        var result = _quuxHandler.DoQuux(param1, param2, param3;
        Log.TraceFormat("{0}: returns {1}", nameof(DoQuux).ToString(), result.ToStringEmptyIfNull();
        return result;
    }
    
  • someone-else (unregistered)

    Groovy is a dumb language. I got one bug:

    • In Ruby: a hash (map ) is {}, while a list is [].

    • In Groovy, a map with one element is ['a':'b']. When I tried to make an empty map, I used []. Turned out it was a stupid list.

    I hated Groovy from that day.

  • Anon Java Dev (unregistered) in reply to Alain Van Hout

    Probably not.

    I suspect that there is a good reason why items is defined as an ArrayList rather than a native array. Most likely so it can be updated later on in the code.

    really, the only thing that's odd here is why the developer is casting it to an ArrayList<String> when Arrays.asList already returns a List, and items is defined as a List.

Leave a comment on “Groovy Typing, Man”

Log In or post as a guest

Replying to comment #483451:

« Return to Article