Further proof that testing is for wimps and bad programmers

Escape Csv

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)

 

My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

Stefan on April 18, 2009 06:29 sez:

You were using SQL Server Management Studio as editor?

I normally do that in Excel or I use "START -> Run..."


Aleris on April 18, 2009 09:57 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 April 18, 2009 10:52 sez:

awesome, amen!


Dylan Bennett on April 18, 2009 14:35 sez:

Why are you checking the returned value of .IndexOf() instead of the .Contains() method?


lb on April 18, 2009 16:24 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 April 20, 2009 12:11 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 April 20, 2009 15:36 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 May 05, 2009 22:19 sez:

I'm awesome, too, which I why I never proof my blog postings (or commetns). :-)


meatbag on June 11, 2010 06:47 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....


(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.