Further proof that testing is for wimps and bad programmers
Couple of days ago I wrote a C# function for a colleague and emailed it to him.
This is the function:
private static string EscapeCsv(string value)
{
//Double all quote characters
value = value.Replace("\"", "\"\"");
//If it contains a comma or a quote char -- qualify it with quotes.
if (value.IndexOf('"') > -1 || value.IndexOf(',') > -1)
{
value = "\"" + value + "\"";
}
return value;
}
The syntax highlighting looks very strange, because I wrote it in sql server management studio. It just happened that the only text editor i had open at the time was SQL server, so that's what i used.
(Real developers don't use any particular editor – they just use whatever's open at the time. Even the act of opening notepad is too cumbersome for the ubergeek.)
After I emailed it to my colleague I had a moment of weakness, when I suffered the tiniest smidgen of self doubt.
"Perhaps I should’ve tested the code in some way. Or -- at least -- compiled it?"
Scott Bellware and the cool alt.net kids are always banging on about this sort of stuff, so I wrote an awesome console app to test it.
namespace tests_are_for_wusses
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(EscapeCsv("fred") == "fred");
System.Console.WriteLine(EscapeCsv("fre,d") == "\"fre,d\"");
System.Console.WriteLine(EscapeCsv("fre\"d") == "\"fre\"\"d\"");
System.Console.ReadLine();
}
private static string EscapeCsv(string value)
{
//Double all quote characters
value = value.Replace("\"", "\"\"");
//If it contains a comma or a quote char -- qualify it with quotes.
if (value.IndexOf('"') > -1 || value.IndexOf(',') > -1)
{
value = "\"" + value + "\"";
}
return value;
}
}
}
Naturally, the awesome code passed my awesome tests first go.
Thus I have once again shown that testing is a waste of time if you are awesome like me.
And the so-called cool kids from alt.net are just bad programmers.
Thank you.
Wait a second... I still feel I'm missing the point. Care to enlighten me?
(By the way -- pretty much every sentence in this post was sarcastic... while the story is true, my real interpretation is that i just got lucky this time. There's definitely some bugs still hidden even in a simple function like this)
'Stefan' on Sat, 18 Apr 2009 10:29:02 GMT, sez: You were using SQL Server Management Studio as editor?
I normally do that in Excel or I use "START -> Run..."
'Aleris' on Sat, 18 Apr 2009 13:57:56 GMT, sez: Your function is missing something: end of line characters within a field must also be escaped. Incidentally your tests are also missing this case. Which actually proves your point.
'http://' on Sat, 18 Apr 2009 14:52:40 GMT, sez: awesome, amen!
'Dylan Bennett' on Sat, 18 Apr 2009 18:35:58 GMT, sez: Why are you checking the returned value of .IndexOf() instead of the .Contains() method?
'lb' on Sat, 18 Apr 2009 20:24:19 GMT, sez: @Dylan
>Why are you checking the returned value of
>.IndexOf() instead of the .Contains()
>method?
ah, there is a reason, i'd forgotten all about this little sub story.
Because I wasn't using Visual Studio, I had no intellisense at my disposal. I originally typed Contains, but then had a brain-fart and wasn't sure if contains only took chars, not sub strings. Without intellisense to check, i wasn't sure, so i reverted to the older and more familiar IndexOf. Charles Petzold was right: vs rots the mind!
'diaphanein' on Mon, 20 Apr 2009 16:11:01 GMT, sez: Strictly speaking, you should check to make sure value is not null. NullReferenceExceptions should never be thrown by library code implicitly. If you don't have access to the source, your only real help is to disassemble the code (I'm looking at you Managed DirectX).
'lb' on Mon, 20 Apr 2009 19:36:15 GMT, sez: @diaphanein
Awesome feedback!
Null checking will be added once i get to work.
I'm thinking null input should return null output. There's no great consensus around nulls in the csv world.
'mike' on Wed, 06 May 2009 02:19:53 GMT, sez: I'm awesome, too, which I why I never proof my blog postings (or commetns). :-)
'meatbag' on Fri, 11 Jun 2010 10:47:34 GMT, sez: "(By the way -- pretty much every sentence in this post was sarcastic... while the story is true, my real interpretation is that i just got lucky this time. There's definitely some bugs still hidden even in a simple function like this)"
Damn, and I was looking forward to cleverly shooting down your argument....
|