• (cs)

    The code populates a 24-element array of integers with "month" numbers, spanning a two-year period from July to June and starting at 7 (July) of the first year.  It can be replaced with one line of code as follows:

    int[] months = { 7, 8, 9, 10, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6 };
     

  • anon (unregistered) in reply to Alex Papadimoulis

    my eyes! the goggles! they do nothing!

  • elpargo (unregistered) in reply to Alex Papadimoulis

    ohhh come on, no way that is in a real app, it seems more like an excersive on a test that says the output is __________

  • Ilya Birman (unregistered) in reply to Alex Papadimoulis

    Hmm, let me guess. There must be an explanation...

    You know... Your one line code uses magic numbers, so it looks very amateurish. Everyone knows that using magic numbers is deprecated. The original code is free from that problem.

  • (cs) in reply to elpargo

    Anonymous:
    ohhh come on, no way that is in a real app, it seems more like an excersive on a test that says the output is __________

    I only wish ;-).

    Only real-world, production code need apply here. Exceptions are certainly made, but they're duly noted in the post. 

  • cm (unregistered) in reply to elpargo

    It's real. I found in some code on a real project.

  • (cs) in reply to Alex Papadimoulis

    And if you still want to do it in code:

    for (int i = 0; i != 24; ++i) { months[i] = 1 + ((i + 6) % 12); }

    (Not tested at all. Of course.)

  • (cs) in reply to Ilya Birman

    Anonymous:
    Hmm, let me guess. There must be an explanation... You know... Your one line code uses magic numbers, so it looks very amateurish. Everyone knows that using magic numbers is deprecated. The original code is free from that problem.

    Actually, no the original code has several acane magic numbers.  The original code is completely free from the problems of clarity, simplicity and efficiency, though.

    I hope this reply was meant to be sarcastic because that kind of one-dimensional adherence to misunderstood advice is a huge problem in the software industry.  I've seen some of the worst code written in the name of structured programming, design patterns, etc.

  • Strikes (unregistered)

    You mean you guys don't get paid by the kloc? [:D]

  • Chris Brien (unregistered) in reply to Alex Papadimoulis

    I'm sorry, but populating an array in your one-liner doesn't scale.

    The equivalent code would be:

    int[] months = new int[24]; for (int i = 0; i < months.length; i++) { months[i] = (i + 6) % 12; }

    Of course, rather than using a lookup table for such a simple operation, it would be better to do

    int getFriggedMonthNumber(int nonFriggedMonthNumber) { return (nonFriggedMonthNumber + 6) % 12; }

    replacing variable names to reflect WTF the code is supposed to actually be for, of course...

  • Fregas (unregistered) in reply to Chris Brien

    Oh god thats so fucking stupid.

  • Herb (unregistered) in reply to Bellinghman

    The above has the added bonus - if you consider it that way - of giving you a 2 year array of months starting at any given month with only trivial modifications... But then, we all know (esp. after reading this blog for a few months) that Math Is Hardtm

  • (cs) in reply to Alex Papadimoulis
    Alex Papadimoulis:

    int[] months = { 7, 8, 9, 10, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6 };


    In Perl, this can be

    @months = (7..12, 1..12, 1..6);

  • (cs)

    With such frequent use of foul language like "cnt", its no wonder there are so few women in this field

  • (cs) in reply to Chris Brien
    Anonymous:
    Of course, rather than using a lookup table for such a simple operation, it would be better to do

    int getFriggedMonthNumber(int nonFriggedMonthNumber) { return (nonFriggedMonthNumber + 6) % 12; }

    replacing variable names to reflect WTF the code is supposed to actually be for, of course...



    I completely agree.  The extra-special lucky bonus wtf here is that a lookup table was created when an inline function or a macro would do.

    Lookup tables are more for string lookups (for easly portability of applications to other languages, and to correct spelling) and speed-critical shortcuts in assembly programs (ie, replacing floating-point sin/cosine with a 256-degree pre-calculated integer lookup so you can use unsigned bytes as degrees, multiply an integer fraction, then shiftdown)

  • (cs) in reply to Fregas

    ..and today in pre-school we will be teaching the alphabet starting from M working outwardly by alternating left-right characters until reaching A and Z. WTFHIT?

     

  • (cs) in reply to anon

    Anonymous:
    my eyes! the goggles! they do nothing!

    LOL! [:D] Nice Simpson's quote.

    A agree....just wow.

  • (cs) in reply to j99

    Actually talking about school, they often have applications where calendar year starts in July or August, as this is the normal start of a school year.  I'm working in a school board and we actually have to display a full year calendar that way.

  • DjDanny32 (unregistered)

    <FONT face=Arial>That's pretty special!</FONT>

    <FONT face=Arial>However, a better way of doing it would be to put those values in a database, then pull them out as needed with some nice Javascript.</FONT>

  • (cs) in reply to j99
    j99:

    ..and today in pre-school we will be teaching the alphabet starting from M working outwardly by alternating left-right characters until reaching A and Z. WTFHIT?

     

     

    Missed the quoting for my school calendar year post right above.

  • tSQL (unregistered) in reply to Free

    Free:
    With such frequent use of foul language like "cnt", its no wonder there are so few women in this field

    There is nothing quite like engaging a college professor during a classroom exercise as to the current value of 'cont'.  Especially when his poor English  prevented me from gracefully retracting the question.  Yup, some things will just scar a man.  I have never named a variable 'cnt' or 'cont' since.

  • poser (unregistered)

    Surely then the general form as a loop is much simpler to come up with

    m = 24 # number of months
    s = 7   # starting month
    list = array [1..m] # output
    for i = 1 to m
       if s > 12 then s = 1
       list[i] = s
       s = s + 1
    end for
       

  • (cs)

    (Big sigh)...  Wow, definately must be paid by the line of code.  Unless he knows something we don't like the order of the months may change. [:P]

  • theorem (unregistered) in reply to Alex Papadimoulis

    Trivial, and hilarious at the same time --  but am I really the first one to point out that the "proper" one liner solution proposed does not match the description above ?

    int[] months = { 7, 8, 9, 10, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6 };

    Missing an 11 there eh ?  
    I can only assume it's a typo because there are only 23 elements. shrug

    Y'all are fools for missing that.   :-)

  • John (unregistered) in reply to tSQL

    I worked at a company that used the variable "NDK8R" for a loop counter everywhere. It took me a long time to figure out that it stood for "indicator".
    Nothing like confusing people for no reason.

  • (cs) in reply to Charles Nadolski
    Charles Nadolski:
    I completely agree.  The extra-special lucky bonus wtf here is that a lookup table was created when an inline function or a macro would do.


    Except that this is Java or C# where there's no such thing as inline functions or macros.


    I very minor code enhancement, though, replace:
    months[i] = cnt;
    cnt = cnt + 1;
    i = i + 1;

    with

    months[i++] = cnt++;

    (God, how I hate the x = x + ... syntax. I'll never use a language that doesn't have +=)
  • (cs) in reply to Free

    Free:
    With such frequent use of foul language like "cnt", its no wonder there are so few women in this field

    The WYSIWYG text editor this forum uses is the FreeTextBox.  However, there's another one just like it, called the "FCK editor"  (name from the author's initials)

  • (cs) in reply to tSQL

    Anonymous:
    There is nothing quite like engaging a college professor during a classroom exercise as to the current value of 'cont'.  Especially when his poor English  prevented me from gracefully retracting the question.  Yup, some things will just scar a man.  I have never named a variable 'cnt' or 'cont' since.

    Reminds me of a joke told by Freddy Prinze (Sr.) way back in the '70's.   His Puerto Rician-born mother was a very big fan of Columbo, and wanted to meet Peter Falk. When Freddy's TV show took off, he was able to arrange it, but he caution her, "Don't go wild and embrass me, and whatever you do, don't call him 'Columbo' --- he hates it when people do that".  When Freddy brought her into the room with Falk, she immediately shouts, "Hey Columbo! I LOVE You!".    Later, Freddy chastised her, "Why did you call him 'Columbo'?  I told you not to".   She replied "With my accent, I'm gonna take a chance with his last name?"  (*)

     

    (*) The story does lose a bit without Prinze doing his mother's accent.

  • (cs)

    A couple of the comments in reply to this had off-by-one errors (which I also call Obi-Wan errors -- "these are not the integers you're looking for"), but never mind that.

    I'm just wondering: is there a language in common use that allows automagic caching of immutable values?  So you'd write something link this:

    immutable int months(int i)
    {
      return 1 + (i+6)%12;
    }


    ... and it would compile as something like this:

    int _months(int i)
    {
      static int _result[];
      if (!defined _result[i]) {
        _result[i] = 1 + (i+6)%12;
      }
      return _result[i];
    }


    That would be an interesting opportunity for optimisation.

  • carlos (unregistered) in reply to bat

    Functional languages (e.g. Haskell) usually evaluate function calls at most once.  Although the implementation is nothing like your pseudo-code.

  • Boojum (unregistered) in reply to bat
    bat:
    I'm just wondering: is there a language in common use that allows automagic caching of immutable values?

    That's called memoization.  As another poster mentioned, it's used a lot in functional programming, but it does occasionally turn up elsewhere.  If you google for memoization you should find more than you ever wanted to know about it.
  • (cs) in reply to bat

    bat:
    I'm just wondering: is there a language in common use that allows automagic caching of immutable values?

    With Perl it's easy:

    <FONT face="Courier New">        use Memoize;
            memoize('slow_function');
            slow_function(arguments);    # Is faster than it was before</FONT>

    <FONT face=Arial>It works by comparing the arguments passed to a table of arguments it already got before, and only calling the function for arguments it has not seen yet.  It wraps the function at runtime by changing the symbol table, so the rest of the code does not have to be changed.</FONT>

     

  • John (unregistered)

    http://validator.w3.org/check?verbose=1&uri=http%3A//www.thedailywtf.com/

  • Taryn East (unregistered) in reply to Le Poete
    Le Poete:
    Actually talking about school, they often have applications where calendar year starts in July or August, as this is the normal start of a school year.  I'm working in a school board and we actually have to display a full year calendar that way.


    Ahem... normal if you are in the US, maybe, but everywhere else in the world (AFAIK) normally has their calendar start at the beginning of the year... schools included. Please be aware of US regionalisms when making sweeping statements.
  • plugwash (unregistered) in reply to Taryn East

    school year here in the uk is september-july (august is the middle of the summer holiday)

  • (cs) in reply to Taryn East

    Yeah, you tell 'em, Taryn!  It's a big world, and you can't expect people on a web forum like this to all know each other and know what they're all talking about!  Why, the chances against that are astronomical!

    (Hey Taryn, weren't we just talking about how easy it is to identify some people just by their names?  Spooky, huh?  See you on the Shambles!)

  • (cs) in reply to loneprogrammer

    Ah, that was it - memoization!  I knew I'd seen it somewhere.  Really wish I had it in Delphi sometimes.

  • Dave Lewis (unregistered) in reply to Taryn East
    Anonymous:
    Le Poete:
    Actually talking about school, they often have applications where calendar year starts in July or August, as this is the normal start of a school year.  I'm working in a school board and we actually have to display a full year calendar that way.


    Ahem... normal if you are in the US, maybe, but everywhere else in the world (AFAIK) normally has their calendar start at the beginning of the year... schools included. Please be aware of US regionalisms when making sweeping statements.


    Not necessarily.  I work for an edu in the UK and our calendar starts from August.  I have a bunch of inline SQL functions and other goodies for working out that week 1, for me, is actually week 22 or 23 on the calendar.
  • The Doctor (unregistered) in reply to Free
    Free:
    With such frequent use of foul language like "cnt", its no wonder there are so few women in this field


    That's nothing.  At one place I worked, all dictionaries were prefixed with "dic".  Using a dictionary to track data inserted into tables was always fun.

  • John Webber (unregistered) in reply to Alex Papadimoulis

    There's another gotcha in this, assuming the code is Java - if the rest of the code uses the months to relate to an instance of java.util.Calendar it will have strange results, because the MONTH field in Calendar is zero-based.

  • Big Jim (unregistered) in reply to John

    This reminds me of my old COBOL days in Decatur, Illinois.  To track rail cars, the code had a flag that said whether or not a car was in the town of Decatur:

    01  In-Decatur-Indicator         pic x(01).

    Sorry for the COBOL post, I haven't used it in 13 years...

  • FuIru (unregistered)

    Now, sometimes the simple math confuses me, but is that months array supposed to be 30 elements?

  • (cs) in reply to FuIru
    Anonymous:
    Now, sometimes the simple math confuses me, but is that months array supposed to be 30 elements?

    Nope. 24. The first loop executes 6 times, the second 12 times, the third 6 times.

  • (cs) in reply to FuIru
    Anonymous:
    Now, sometimes the simple math confuses me, but is that months array supposed to be 30 elements?

    not sure if you're being sarcastic, but the first loop starts at 7.
  • P (unregistered) in reply to Free

    In this woman's classroom, "cnt" is definitely pronounced "count". And programs I write for public consumption have it spelled out in full, although the unnecessary letters grieve my lazy programmer's mind.

  • (cs) in reply to Big Jim
    Anonymous:
    This reminds me of my old COBOL days in Decatur, Illinois.  To track rail cars, the code had a flag that said whether or not a car was in the town of Decatur:

    01  In-Decatur-Indicator         pic x(01).

    Sorry for the COBOL post, I haven't used it in 13 years...


    Hahaha, that's hilarious, but that's probably because I live in Champaign right now :)  I try to piss off the locals by pronouncing it all French like "Décateur".
  • SgtPepper (unregistered) in reply to wakeskate

    Hey all, new here waves  someone posted a one for loop to do this, but assuming you wanted to stay with a while loop, this does the trick as well:

    int[] months = new int[24];
    int x = 7;
    int z = 0;
    while ( z != 24 )
    {
        months[z] = x;
        if(x == 12)
        {
            x = 0;
        };
       
        x = x + 1;
        z = z + 1;

     }

  • (cs) in reply to P

    Anonymous:
    In this woman's classroom, "cnt" is definitely pronounced "count". And programs I write for public consumption have it spelled out in full, although the unnecessary letters grieve my lazy programmer's mind.

     

    I never realized women teachers visited this forum. I must say I usualy pronounce above variable namt as 'See-En-Tee' or Knut, never as the word inferred from above posts. Then again, I usually just use i for counting...

    Drak

  • Rob (unregistered) in reply to SgtPepper
    Anonymous:
    Hey all, new here *waves*  someone posted a one for loop to do this, but assuming you wanted to stay with a while loop, this does the trick as well:

    int[] months = new int[24];
    int x = 7;
    int z = 0;
    while ( z != 24 )
    {
        months[z] = x;
        if(x == 12)
        {
            x = 0;
        };
       
        x = x + 1;
        z = z + 1;

     }

    Why would you use a while loop???  A for loop is designed to initialize variables within a counter and do your incrementing all in one line.  Plus, what is that if(x==12)?  months[z]=x%12 works in one line.

    I hope you were joking :P
  • Anonymous Coward (unregistered) in reply to Drak

    in the off-topic-but-relevant-to-suspicious-naming department, it's always been a source of amusement how many people get caught out by legitimate but illicit-sounding domains.

    For example, a long time ago I was investigating some illicit web browsing that some HR department had picked up. Having not progressed past a script that simply searched for keywords, the poor guy was in the firing line for his habit of browsing to a dubious site.

    The URL?

    www.expertsexchange.com


Leave a comment on “... wait ... that's it?!?”

Log In or post as a guest

Replying to comment #33423:

« Return to Article