Further proof that testing is for wimps and bad programmers
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

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)





'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....




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Who reads this legal bit anyhow?

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

TimeSnapper won last year's Developer Competition at Larkware.com, and is used by over 10,000 people.

Articles

JSON Query Languages: 5 special purpose editors JSON Query Languages: 5 special purpose editors
What then, is b? What then, is b?
SQLike: A simple editor SQLike: A simple editor
Yet Another BizPlan Generator. Yet Another BizPlan Generator.
HOT GUIDS: A hot or not site for guids HOT GUIDS: A hot or not site for guids
How does life get better? One tiny hack at a time. How does life get better? One tiny hack at a time.
24 things to do, and 100 things *not* to do (yet) for building a MicroISV 24 things to do, and 100 things *not* to do (yet) for building a MicroISV
Venture capital won't kill Jeff Atwood, it will only make him Jeffer. Venture capital won't kill Jeff Atwood, it will only make him Jeffer.
A handy workflow image for newbie mercurial users A handy workflow image for newbie mercurial users
Fractal Feedback, a diversion into recreational programming Fractal Feedback, a diversion into recreational programming
Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe. Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe.
Suggested User Experience Improvements for DiffMerge Suggested User Experience Improvements for DiffMerge
SQL Style Extensions for C# SQL Style Extensions for C#
The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's
Sysi: the ultimate administrators toolkit Sysi: the ultimate administrators toolkit

Archives .: secretGeek :: Complete Archives
TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 3.3: true productivity boost

Next Action NextAction
Managing the top of your mind

World's Simplest Code Generator (html edition) World's Simplest Code Generator

25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Universal Troubleshooting checklist Universal Troubleshooting Checklist
Top 10 SecretGeek articles Top 10 SecretGeek articles
ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Realtime CSS Editor, in a browser RealTime Online CSS Editor
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker


[powered by Google] 


How to be depressed How to be depressed
You are not inadequate.



Recommended Reading

The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
OJ Reeves
Eric Sink

Aggregated Links

proggit
dzone
hacker news
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
weekly link post

LinkedIn profile
LogEnvy - event logs made sexy
ShuffleText - fuzzy search for .net
PC Smart Buys - Computer Hardware in Australia
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy