Comment On Helpful SQL Helpers

"I recently had the chance to work on one of our projects that has been in development for longer than I have been employed here," writes Phillip, "back when the project was first started, a large part of it was outsourced overseas because, well, that was all the rage back then." [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Helpful SQL Helpers

2010-01-13 12:03 • by nope (unregistered)
Looked like someone was learning as they were coding. First they learned switch/case then they moved to arrays.

Re: Helpful SQL Helpers

2010-01-13 12:05 • by Daniel Migowski (unregistered)
295714 in reply to 295713
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.

Re: Helpful SQL Helpers

2010-01-13 12:08 • by fahadsadah (unregistered)
monthList.add("thrist");

captcha: distineo

Re: Helpful SQL Helpers

2010-01-13 12:09 • by Mason Wheeler
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.

Re: Helpful SQL Helpers

2010-01-13 12:16 • by 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.

Re: Helpful SQL Helpers

2010-01-13 12:16 • by 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.

Re: Helpful SQL Helpers

2010-01-13 12:20 • by Anish (unregistered)
295719 in reply to 295717
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?

Re: Helpful SQL Helpers

2010-01-13 12:23 • by Mason Wheeler
295720 in reply to 295719
Sure, but since there's no filesystem, you need about 200 MB worth of binary const files for the runtime.

Re: Helpful SQL Helpers

2010-01-13 12:25 • by Ramses So let it be written so let it be done (unregistered)
The Battle of Hastings!!!

Re: Helpful SQL Helpers

2010-01-13 12:27 • by 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

Re: Helpful SQL Helpers

2010-01-13 12:28 • by Carl (unregistered)
Time to combine two memes: Irish Girl and Embedded Systems. Write your own joke.

Re: Helpful SQL Helpers

2010-01-13 12:29 • by Ernie (unregistered)
295724 in reply to 295719
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?

Re: Helpful SQL Helpers

2010-01-13 12:32 • by frits
295725 in reply to 295724
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.

Re: Helpful SQL Helpers

2010-01-13 12:34 • by Plz Send Me The Code (unregistered)
Hey, that's kinda handy! Mind if I use it?

Re: Helpful SQL Helpers

2010-01-13 12:36 • by 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!!

Re: Helpful SQL Helpers

2010-01-13 12:37 • by ... (unregistered)
295728 in reply to 295726
Wow, this is just... not a WTF at all.
Is Alex even reading these before posting?

Re: Helpful SQL Helpers

2010-01-13 12:37 • by frits
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;
}

Re: Helpful SQL Helpers

2010-01-13 12:38 • by 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.

Re: Helpful SQL Helpers

2010-01-13 12:39 • by SR (unregistered)
295732 in reply to 295726
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)

Re: Helpful SQL Helpers

2010-01-13 12:44 • by matthewr81
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.

Re: Helpful SQL Helpers

2010-01-13 12:44 • by 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.

Re: Helpful SQL Helpers

2010-01-13 12:44 • by 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.

Re: Helpful SQL Helpers

2010-01-13 12:48 • by ShatteredArm (unregistered)
295736 in reply to 295724
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();

Re: Helpful SQL Helpers

2010-01-13 12:48 • by Neville Flynn (unregistered)
295737 in reply to 295728
...:
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.

Re: Helpful SQL Helpers

2010-01-13 12:48 • by 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?

Re: Helpful SQL Helpers

2010-01-13 12:49 • by Dave (unregistered)
OK, I think it's time for The Daily WTF to become The Monthly WTF

Re: Helpful SQL Helpers

2010-01-13 12:51 • by 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.

Re: Helpful SQL Helpers

2010-01-13 12:53 • by Rick
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?

Re: Helpful SQL Helpers

2010-01-13 12:55 • by Zach Bora (unregistered)
295742 in reply to 295722
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.

Re: Helpful SQL Helpers

2010-01-13 12:56 • by 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.

Re: Helpful SQL Helpers

2010-01-13 13:02 • by 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.

Re: Helpful SQL Helpers

2010-01-13 13:06 • by metzomagic
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.

Re: Helpful SQL Helpers

2010-01-13 13:08 • by snoofle
295746 in reply to 295723
Carl:
Time to combine two memes: Irish Girl and Embedded Systems. Write your own joke.
Embedded Irish Girl?

Re: Helpful SQL Helpers

2010-01-13 13:08 • by snoofle
295747 in reply to 295723
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!)

Re: Helpful SQL Helpers

2010-01-13 13:08 • by Ocson (unregistered)
295748 in reply to 295742
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)

Re: Helpful SQL Helpers

2010-01-13 13:15 • by Anon (unregistered)
295749 in reply to 295742
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.

Re: Helpful SQL Helpers

2010-01-13 13:16 • by Anon (unregistered)
295750 in reply to 295736
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?


Re: Helpful SQL Helpers

2010-01-13 13:16 • by Anon (unregistered)
TRWTF is the lack of typos and/or grammatical errors. This doesn't feel like TDWTF at all.

Re: Helpful SQL Helpers

2010-01-13 13:18 • by 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.

Re: Helpful SQL Helpers

2010-01-13 13:20 • by frits
295753 in reply to 295736
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!

Re: Helpful SQL Helpers

2010-01-13 13:26 • by 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.

Re: Helpful SQL Helpers

2010-01-13 13:32 • by 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);
}

Re: Helpful SQL Helpers

2010-01-13 13:34 • by Anish (unregistered)
295757 in reply to 295754
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.

Re: Helpful SQL Helpers

2010-01-13 13:35 • by frits
295758 in reply to 295756
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.

int _month = month;

2010-01-13 13:40 • by 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.

Re: Helpful SQL Helpers

2010-01-13 13:40 • by drachenstern
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...

Re: Helpful SQL Helpers

2010-01-13 13:41 • by Bim Job (unregistered)
295761 in reply to 295747
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.

Re: Helpful SQL Helpers

2010-01-13 13:43 • by SpasticW3asel (unregistered)
295762 in reply to 295750
Yeah, but it uses linq. Which is cool and shiny.

Re: Helpful SQL Helpers

2010-01-13 13:52 • by gray goat (unregistered)
295763 in reply to 295758
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);
}

Re: Helpful SQL Helpers

2010-01-13 13:55 • by 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...)
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment