• nope (unregistered)

    Looked like someone was learning as they were coding. First they learned switch/case then they moved to arrays.

  • Daniel Migowski (unregistered) in reply to nope

    This is a completely useful function. It returns the 12 month and includes a rotation, so you get the month from June to December and Januar to May, if you pass 6 (June) as an argument.

  • fahadsadah (unregistered)

    monthList.add("thrist");

    captcha: distineo

  • (cs)

    OK, TRWTF here is definitely the developers. They've spent months trying to figure out what this function does? It's immediately obvious to me, and I don't even know C#.

    It inputs a month number and returns a list of the twelve months of the year, starting from that month. So if you input 7, you'll get July--December, then January--June.

    Granted, this is not the best possible way the original developer could have implemented this, but it's far from a WTF.

    Addendum (2010-01-13 12:15): My mistake. They didn't spend months trying to figure it out, just "a fair amount of time." Still ridiculous.

  • Terry (unregistered)

    Utterly riveting! What happened next?

    Seriously, a substandard but apparently working function, and someone using the 'refactor' tool in visual studio are... just completely normal. I mean the function could be better but it's not wrong. There's no wtf here. All I can say is that they were probably developing for an embedded system where no file system was available, that's all.

  • Bill (unregistered)

    Besides, in embedded systems, you don't have a file system so you can't just load some month-names table. Not a WTF.

  • Anish (unregistered) in reply to Terry
    Terry:
    Utterly riveting! What happened next?

    Seriously, a substandard but apparently working function, and someone using the 'refactor' tool in visual studio are... just completely normal. I mean the function could be better but it's not wrong. There's no wtf here. All I can say is that they were probably developing for an embedded system where no file system was available, that's all.

    Can you develop for embedded systems in .Net?

  • (cs) in reply to Anish

    Sure, but since there's no filesystem, you need about 200 MB worth of binary const files for the runtime.

  • Ramses So let it be written so let it be done (unregistered)

    The Battle of Hastings!!!

  • Anish (unregistered)

    Ran the function...

            for (int x = -1; x <= 13; x++)
            {
                Console.WriteLine("For x:"+x.ToString());
                 List<string> months =  GetMonths(x);
                 foreach (string month in months)
                     Console.WriteLine(month);
                 Console.WriteLine("");
    
            }
    

    Output..

    For x:-1 January February March April May June July August September October November

    For x:0 December January February March April May June July August September October November

    For x:1 January February March April May June July August September October November December

    For x:2 February March April May June July August September October November December January

    For x:3 March April May June July August September October November December January February

    For x:4 April May June July August September October November December January February March

    For x:5 May June July August September October November December January February March April

    For x:6 June July August September October November December January February March April May

    For x:7 July August September October November December January February March April May June

    For x:8 August September October November December January February March April May June July

    For x:9 September October November December January February March April May June July August

    For x:10 October November December January February March April May June July August September

    For x:11 November December January February March April May June July August September October

    For x:12 December January February March April May June July August September October November

    For x:13 January February March April May June July August September October November

  • Carl (unregistered)

    Time to combine two memes: Irish Girl and Embedded Systems. Write your own joke.

  • Ernie (unregistered) in reply to Anish
    Anish:
    Terry:
    Utterly riveting! What happened next?

    Seriously, a substandard but apparently working function, and someone using the 'refactor' tool in visual studio are... just completely normal. I mean the function could be better but it's not wrong. There's no wtf here. All I can say is that they were probably developing for an embedded system where no file system was available, that's all.

    Can you develop for embedded systems in .Net?

    Have you ever heard of Windows CE or Windows Mobile? Or how about Windows XPe?

  • (cs) in reply to Ernie
    Ernie:
    Anish:
    Terry:
    Utterly riveting! What happened next?

    Seriously, a substandard but apparently working function, and someone using the 'refactor' tool in visual studio are... just completely normal. I mean the function could be better but it's not wrong. There's no wtf here. All I can say is that they were probably developing for an embedded system where no file system was available, that's all.

    Can you develop for embedded systems in .Net?

    Have you ever heard of Windows CE or Windows Mobile? Or how about Windows XPe?

    Wow! A double Troll.

  • Plz Send Me The Code (unregistered)

    Hey, that's kinda handy! Mind if I use it?

  • Pete (unregistered)

    It looks ok to me as others have said it will return the 12 months from the month specified. It's not written in a great way but will work from first glance.

    Can we please stop having WTF's that aren't WTF's that's 2 out of the last 3 posts. What the WTF is that about!!

  • ... (unregistered) in reply to Plz Send Me The Code

    Wow, this is just... not a WTF at all. Is Alex even reading these before posting?

  • (cs)

    Other than being ugly, it's not much of a WTF. At the risk of being abused, here's how I would do it (static tests not shown):

    static List<string> m_months = new List<string>( new string[] { "January","February","March","April", "May","June", "July","August","September","October","November","December" } ); public static List<string> GetMonths(int month) { month--; List<string> months = new List<string>(); for (int i = month; i < month + 12; i++) { months.Add(m_months[i % 12]); } return months; }

  • Donald (unregistered)

    TRWTF is Philip, who submitted a piece of code that he didn't understand, and treated it as a WTF, when it was a decent way of performing the task.

    Everyone point and laugh at Philip.

  • SR (unregistered) in reply to Plz Send Me The Code
    Plz Send Me The Code:
    Hey, that's kinda handy! Mind if I use it?

    If I was your boss I would

    CAPTCHA: usitas - Use it and I'll call you an ass :o)

  • (cs)

    I am starting to wonder if Alex if knows how to code... or just looks at a long line of code as, wow, they must have done it wrong.

  • Anon (unregistered)
    The c-bitmap:
    For some unknown reason, the original developer decided that the best way to deal with bitmap images was to convert them to C-files and header-files.
    Helpful SQL Helpers:
    I have yet to understand what the function does, or how exactly it's used.
    Is this a new class of WTF's?

    Developers who inherit code who aren't capable of understanding what it does, so they think it's a WTF?

    Doesn't make for very interesting reading.

  • MikeCD (unregistered)

    That's a valid refactoring (assuming it was automated).

    One of the things about refactoring is that the sum effect is larger than you'd expect from looking at the individual commits. In this case it also sounds like the lead developer is going for a warning-free codebase which is laudable.

  • ShatteredArm (unregistered) in reply to Ernie
    Ernie:
    Anish:
    Terry:
    Utterly riveting! What happened next?

    Seriously, a substandard but apparently working function, and someone using the 'refactor' tool in visual studio are... just completely normal. I mean the function could be better but it's not wrong. There's no wtf here. All I can say is that they were probably developing for an embedded system where no file system was available, that's all.

    Can you develop for embedded systems in .Net?

    Have you ever heard of Windows CE or Windows Mobile? Or how about Windows XPe?

    Author already established that there is a file system. The code used app settings.

    Also, the function could have been easier using LINQ syntax: List<string> months = new List<string> { "January", "Feb... months = months.Skip(month).Concat(months.Take(month)).ToList();

  • Neville Flynn (unregistered) in reply to ...
    ...:
    Wow, this is just... not a WTF at all. Is Alex even reading these before posting?

    Perhaps Alex meant that Philip is the WTF. So it's sort of like a meta-WTF.

  • Carsten Sorensen (unregistered)

    The biggest WTF here is probably that the function is in the "SQLHelpers.cs" files.

    Or perhaps it's a meta WTF. WTF is it doing on TDWTF?

  • Dave (unregistered)

    OK, I think it's time for The Daily WTF to become The Monthly WTF

  • Bim Job (unregistered)

    "These are not the unmanaged resources you've been warned about..."

    I'm rather impressed by that. We've gone all the way from idiots who insist that "Warnings are just what compilers do. If it was really an error, the compiler wouldn't just call it a warning" to wannabe PHB idiots who think that a clean, warning-free build is more important than correctness.

    Oil will one day run out; climate change is inevitable; and, one way or another, it's pretty clear that God doesn't really like us all that much.

    But it's cheering to know that, no matter how hard we try to eliminate the bastards, idiots will always prosper.

  • (cs)

    The only thing wrong that I can see is that the switch doesn't have a default case. But not a WTF. Alex, this is 2 non WTFs this week. Are you feeling ok?

  • Zach Bora (unregistered) in reply to Anish
    Anish:
    Ran the function...
            for (int x = -1; x <= 13; x++)
            {
                Console.WriteLine("For x:"+x.ToString());
                 List<string> months =  GetMonths(x);
                 foreach (string month in months)
                     Console.WriteLine(month);
                 Console.WriteLine("");
    
            }
    

    Output..

    [...]

    I don't think it will output the same depending on your OS language. At least that's what happens in MSSQL and .NET when I do stuff like this on my French OS.

    One example I have is a Java application sending dates to the server in format "Feb 01, 2010". My computer would be sending "Fév 01, 2010" instead.

  • Otto (unregistered)

    The switch/case is a tad bit silly, but nevertheless I can see the usefulness of a function to get a month list starting from an arbitrary month. I'd probably name it better though.

    While I'm not sure how those Lists work, if it was me, I'd probably make list of the month names starting at January, count down the list to my starting month, then cut everything before my starting point into another array, and swap the two before appending them back together. This would be much faster with linked lists.

  • AA (unregistered)

    I've seen that switch/case code on here before. Like, I'm pretty sure it's the exact same code.

    And it doesn't make a lot of sense in context.

    I think Alex screwed up rewriting this, and copy-pasted the code from the next Classic WTF instead of this story.

  • (cs)

    I see what they're doing there, but it could be written a lot more concisely: put the months into an array, then iterate through the array 12 times from the start month, wrapping when you reach the end of the array of course.

    That wouldn't make it execute much faster or anything, but at least it would look a helluvah lot nicer and have a smaller code footprint.

  • (cs) in reply to Carl
    Carl:
    Time to combine two memes: Irish Girl and Embedded Systems. Write your own joke.
    Embedded Irish Girl?
  • (cs) in reply to Carl
    Carl:
    Time to combine two memes: Irish Girl and Embedded Systems. Write your own joke.
    Embedded Irish Girl?

    Edit: Great; first a double post, then the delete button takes me back to the top of the comments (an actual wtf!)

  • Ocson (unregistered) in reply to Zach Bora
    Zach Bora:
    Anish:
    Ran the function...
            for (int x = -1; x <= 13; x++)
            {
                Console.WriteLine("For x:"+x.ToString());
                 List<string> months =  GetMonths(x);
                 foreach (string month in months)
                     Console.WriteLine(month);
                 Console.WriteLine("");
    
            }
    

    Output..

    [...]

    I don't think it will output the same depending on your OS language. At least that's what happens in MSSQL and .NET when I do stuff like this on my French OS.

    One example I have is a Java application sending dates to the server in format "Feb 01, 2010". My computer would be sending "Fév 01, 2010" instead.

    Except that the month names are hardcoded, and no attempt at i18n is done. (Maybe thats TRWTF..........yeah I got nothing)

  • Anon (unregistered) in reply to Zach Bora
    Zach Bora:
    I don't think it will output the same depending on your OS language. At least that's what happens in MSSQL and .NET when I do stuff like this on my French OS.

    One example I have is a Java application sending dates to the server in format "Feb 01, 2010". My computer would be sending "Fév 01, 2010" instead.

    That's what InvariantCulture is for.

  • Anon (unregistered) in reply to ShatteredArm
    ShatteredArm:
    Also, the function could have been easier using LINQ syntax: List<string> months = new List<string> { "January", "Feb... months = months.Skip(month).Concat(months.Take(month)).ToList();

    Yes, but:

    1. Your version produces different results than the original.
    2. Your version is 2 to 3 times slower that the original.

    So, for the sake of refactoring code that is only moderately bad but (presumably) works, you're willing to introduce a bug and slow down the application?

  • Anon (unregistered)

    TRWTF is the lack of typos and/or grammatical errors. This doesn't feel like TDWTF at all.

  • me_again (unregistered)

    trwtf is that he took it to his boss and asked if any one has done refactoring and then pokes fun at his boss.

  • (cs) in reply to ShatteredArm
    ShatteredArm:
    Ernie:
    Anish:
    Terry:
    Utterly riveting! What happened next?

    Seriously, a substandard but apparently working function, and someone using the 'refactor' tool in visual studio are... just completely normal. I mean the function could be better but it's not wrong. There's no wtf here. All I can say is that they were probably developing for an embedded system where no file system was available, that's all.

    Can you develop for embedded systems in .Net?

    Have you ever heard of Windows CE or Windows Mobile? Or how about Windows XPe?

    Author already established that there is a file system. The code used app settings.

    Also, the function could have been easier using LINQ syntax: List<string> months = new List<string> { "January", "Feb... months = months.Skip(month).Concat(months.Take(month)).ToList();

    A Triple Lindy!

  • (cs)
    clearly, worrying about whether or not a particular part of the .net framework showing a warning because it had been deprecated was more important than whether or not un-managed resources were disposed of properly.

    There are no unmanaged resources in this code sample.

    I'd say that eliminating compiler warnings from a project is an important use of time. Even if they're just noise, they could obscure other significant warnings.

  • gray goat (unregistered)

    Most likely off by one, but:
    private static List<string> GetMonthsStartingFrom(int startingMonth) { var months = new List<string> { {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"}, {"Dec"}, {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"} }; startingMonth = startingMonth % 12; return months.GetRange(startingMonth, 12); }

  • Anish (unregistered) in reply to Tzimisce
    Tzimisce:
    clearly, worrying about whether or not a particular part of the .net framework showing a warning because it had been deprecated was more important than whether or not un-managed resources were disposed of properly.

    There are no unmanaged resources in this code sample.

    I'd say that eliminating compiler warnings from a project is an important use of time. Even if they're just noise, they could obscure other significant warnings.

    I Agree. There is a Delphi application I maintain. When I inherited it, there were upwards of a thousand warnings. We spent significant time clearing the warnings which enabled us to locate some bugs.

  • (cs) in reply to gray goat
    gray goat:
    Most likely off by one, but: private static List<string> GetMonthsStartingFrom(int startingMonth) { var months = new List<string> { {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"}, {"Dec"}, {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"} }; startingMonth = startingMonth % 12; return months.GetRange(startingMonth, 12); }

    Also, index out of range.

  • mk (unregistered)

    The line I am curious about is

    int _month = month;

    Is there a legitimate reason to do this in C#? In C or C++, I can't think of one. If the parameter month were a pointer and the program multithreaded, this would make sense. However, the parameter is an int, so there's no way that another thread could change. Moreover, there is nothing wrong with reassigning to your parameters, in C at least.

  • (cs)

    Can we use this for a code contest like Alex tried to get going a while back? I see we've already had a couple of people give it a miss.

    I'll toss my wrong entry into the pot, see how many errors you can find ...

    public List<string> ListNext12Months(int startingMonth) {
      List<string> potentialMonths = new List<string> {"January","Feb... "November","December","Jan... ,"December"};
      // so I'm lazy and don't want to put all the months
      List<string> returnList = new List<string>();
      int loopCounter = startingMonth;
      int loopLimit = startingMonth + 11;
      while ( loopCounter < loopLimit )
        returnList.add( potentialMonths[ loopCounter++ ] );
      // yes this does rely on a quirk, and is better written
      // as a two line process, where the increment is on the
      // next line, which I took more space in the block by
      // including these comments, therefore not making my code
      // smaller, but I wanted to demonstrate a shorter code
      // block and still head off the inevitable snide remarks
      return returnList;
    }
    

    Addendum (2010-01-13 13:45): Of course, that's if you like the style that the previous developer used, but I would go instead for something more akin to:

    public List<string> ListNext12Months(int startingMonth) {
      List<string> potentialMonths = new List<string> {"January","Feb... "November","December","Jan... ,"December"};
      return potentialMonths.GetRange(startingMonth, startingMonth + 11);
    }
    

    But I think I may be off by one as well...

  • Bim Job (unregistered) in reply to snoofle
    snoofle:
    Carl:
    Time to combine two memes: Irish Girl and Embedded Systems. Write your own joke.
    Embedded Irish Girl?

    Edit: Great; first a double post, then the delete button takes me back to the top of the comments (an actual wtf!)

    Embedded Irish Girl (C++ bitmap version), great idea! Best I can locate so far is an actual video of Erica. Knock yourselves out!

    No, seriously, knock yourselves out. Use a medium sized rock or a two-by-four or a wodge of PHP code or some horse tranquillizers or something. It'd be far more entertaining.

    She's still hot, though.

  • SpasticW3asel (unregistered) in reply to Anon

    Yeah, but it uses linq. Which is cool and shiny.

  • gray goat (unregistered) in reply to frits
    frits:
    gray goat:
    Most likely off by one, but: private static List<string> GetMonthsStartingFrom(int startingMonth) { var months = new List<string> { {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"}, {"Dec"}, {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"} }; startingMonth = startingMonth % 12; return months.GetRange(startingMonth, 12); }

    Also, index out of range.

    Yea, that too.

        private static List<string> GetMonthsStartingFrom(int startingMonth)
        {
            const int MONTHS = 12;
            var months = new List<string>
                             {
                                 {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"}, {"Dec"},
                                 {"Jan"}, {"Feb"}, {"March"}, {"April"}, {"May"}, {"June"}, {"July"}, {"Aug"}, {"Sept"}, {"Oct"}, {"Nov"}
                             };
    
            if (startingMonth < 0)
                startingMonth = MONTHS - Math.Abs(startingMonth) % MONTHS;
            startingMonth = startingMonth % MONTHS;
            return months.GetRange(startingMonth, MONTHS);
        }
    
  • Synchronos (unregistered)

    The most mind-boggling is this:

    _month = (_month % 13);
    if (_month == 0)
      _month = 1;
    

    Why would one consider using the above instead of:

    if (_month > 12)
      _month = 1;
    

    Of course, for the real solution, I would split a normal month name array in two, and then concatenate the parts back together in the reverse order. Not perhaps the most optimized solution (might create unneeded array allocations), but most probable to be written bug-free and the easiest to see what it is doing.

    In somewhat pseudo-syntax (not any particular language):

    function WrappingMonths(int startMonth) {
      const months = {"January", "February", ..., "December"}; // null-based indexing
      return months.slice(startMonth-1, 13-startMonth).concatenate(months.slice(0, startMonth-1));
    }
    

    (Feel free to point out the bugs and wtfs in my code...)

Leave a comment on “Helpful SQL Helpers”

Log In or post as a guest

Replying to comment #:

« Return to Article