• Jaloopa (unregistered)
    frist = new List<string>(4)
    for (int i = 0; i < frist.Capacity; i++)
    {
      switch (i)
      {
        case 0: frist.Add("F");
          break;
        case 1: frist.Add("r");
          break;
        case 2: frist.Add("1");
          break;
        case 3: frist.Add("s");
          break;
        case 4: frist.Add("t");
          break;
      }
    }
    
  • bvs23bkv33 (unregistered)

    Comments = new List<Comment>(2); // other comments will not be shown

  • Vilx- (unregistered)

    Suddely, a flash of inspiration allowed me to see into the mind of the misguided dev who wrote this.

    First of all, we need to do the same operation - add an item to the list - multiple times. That means a loop. Since we know how many, it's a FOR loop. However on each iteration we need to pass a different parameter. Thus the SWITCH to ensure that we are adding the right string on each pass. Simple, eh?

  • TruePony (unregistered)

    The worst part for me is using Capacity instead of Count in the for loop’s condition. This code probably actually worked at some time when the default List capacity was larger.

  • (nodebb)

    The programmer who wrote that was high on mushrooms.

  • Supersonic Tumbleweed (unregistered) in reply to Vilx-

    You're scaring me. Make it stoop

  • Yuriy (unregistered)

    Isn't there a problem in the very first statement?

    Destinatii = new List<Destinatii>(4)
    

    So the variable overrides the typedef, is this at all possible? I'm not familiar with .NET and dunno, of course, it might work, but looks quite ugly still.

  • WTFGuy (unregistered) in reply to TruePony

    @ TruePony: I'm not sure I follow. The only way I can see to use Count in the looping construct is something like this: Destinatii = new List<Destinatii>(4);
    while (Destinatii.Count < Destinatii.Capacity)
    {
    switch (Destinatii.Count)
    {
    case 0: Destinatii.Add(new Destinatii { code = "ANGAJARE", description = "ANGAJARE" }); break; // cases 1-6 here omitted for brevity ... }
    }

    That seems much less clear to me than the straight for( int i ...) iterator.

    Funny enough, if in the while() version the dev then made the fencepost error of typing
    while (Destinatii.Count <= Destinatii.Capacity) // less-or-equal-to
    then the thing would crash differently. The loop would try to add one more than the current capacity which would trigger the list to dynamically grow. IRC the list rows by more than one, so now the Capacity is bigger than Count bt no cases match and we're into an infinite loop. That'll get caught pretty early in testing even if you don't have any kind of real QA. :)

    As so often, the root of a WTF is a fundamental misunderstanding of the tools being used. The capacity of a List<T> is not fixed. If the dev was thinking a List<T> is really a sugar-coated array, then the requirement to guard against making entries beyond the declared size is correct thinking albeit coded poorly.

    Then the trap having been set there was just a simple error during maintenance: the dev who added the last two cases knew List<T>s are elastic, but didn't bother to examine the initialization or looping construct to understand what it really did.

    Two half-understood wrongs definitely don't make a right in the dev world. In fact they usually make a WTF.

    @Vilx: Perfect! When people's knowledge of algorithms and data structures is simply a rote list of the verbs in the language we get this kind of braindead coding. Their internal process is "see problem -> map to language verb -> fiddle until it works. Repeat". Scary.

  • my name is missing (unregistered)

    Clearly no one tested this code, as it should be patently obvious that it only showed 4 items. Yet I also imagine this person writing a unit test that passed 100% of the time.

  • Jay (unregistered)

    Why even bother with a loop anyway? I don't see how any of these cases will be executed out of order or more than once as it sequences through the loop. Just execute each add statement in sequence and you're done. This looks like a Rube Goldberg design for populating a ComboBox.

  • Klaus (unregistered)

    Personally, I always consider mixing native-language class and variable names into an English-based programming language a WTF in itself. My native language is German, so I am not biased in that regard, though university (+webcomics, +Terry Pratchett) may have created some bias in favor of English.

    @Jay I'm pretty sure that's the whole point of the article :)

  • (nodebb)

    What about using the same name for the instance and the type? Sure seems like a hell of a WTF to me.

  • (nodebb)

    ".... replaced [the wtf code] with a one-liner" . So he pulled some , err... code .... from https://thoughtcatalog.com/january-nelson/2018/06/funny-one-liners/ ?

  • gnasher729 (unregistered)

    Usually "size" means how many official entries a data structure has, and "capacity" means how many items could be stored without using more storage. A list with four items might have a capacity of 4, 6, or 8, with very different behaviour. And this might change as an implementation detail, so the capacity might be different on a 32 bit or a 64 bit version, or when the underlying library changes. Or for a debug and a release version.

    So this might work correctly for years, then break. Or work in a development environment (debug) and crash in production. Lots of fun to be had.

  • gnasher729 (unregistered) in reply to my name is missing

    "Clearly no one tested this code, as it should be patently obvious that it only showed 4 items. Yet I also imagine this person writing a unit test that passed 100% of the time."

    Clearly yes, if the unit test follows the same pattern.

  • WTF-Guest (unregistered)

    TRWTF is that they used JavaScript to load a select list with static data.

    Could have just written this HTML:

    <select id="wtfDropown"> <option value="ANGAJARE">ANGAJARE</option> <option value="INSCRIERE">ÎNSCRIERE</option> <option value="EXAMEN AUTO">EXAMEN AUTO</option> <option value="SOMAJ">SOMAJ</option> <option value="AJUTOR SOCIAL">AJUTOR SOCIAL</option> <option value="TRATAMENT">TRATAMENT</option> <option value="GRADINITA">GRADINITA</option> </select>
  • snoofle (unregistered) in reply to my name is missing

    void myUnitTest() { if (Destinatii.Capacity < 0) { // test fails } // test passes }

  • Chris (unregistered)

    I'm pretty sure this started as the first four entries, then the others got added later without updating the list's capacity. And no testing (or at least, no effective testing).

    @TruePony - the newly created list will have a count of 0. Mind you, if you make the conditional i <= Destinatti.Count, that would actually work, as it would stop once items stop being added to the list. Absolutely horrible way to do it, but still, it would work.

  • DCL (unregistered) in reply to Klaus

    That's assuming said programmer knows English. I've seen programs written by nationals of various European countries who didn't know English using COBOL, PL/I, REXX and Assembler. Of course the variable and procedure names won't/can't be in English even if the programming language is English based.

  • No Fun (unregistered)

    There was me hoping that TRWTF was pluralizing 'destination' to 'destinatii'

  • WTF done? (unregistered)

    Any new posts left?

  • Anonymous (unregistered) in reply to Yuriy

    As weird as it is, C# allows the typename and the variable name to be the same. This is perfectly valid code and will print 5 followed by 6:

        Int32 Int32 = 5;
        Int32 a = 6;
        Console.WriteLine(Int32);
        Console.WriteLine(a);
    

Leave a comment on “Switch the Dropdown”

Log In or post as a guest

Replying to comment #:

« Return to Article