Okay, so if you're like me your getting sick of the “how fast can we get a book to market” approach with .NET development books these days. As it is probably seen in other genres as well, I can only attest to .NET books, as those are all I read. Today I finally boiled over after discovering one of the most blatant examples of untested code I have seen. So here I am minding my own business when an analyst approaches me for help with some ADO code working with DataSets and DataViews. He indicates that he has been working with the code for over 2 hours and cannot get it to work. He's damn near copied the exact example of the popular “ADO.NET in a nutshell” O'reilly published book, written by Bill Hamilton and Matthew MacDonald (pg 127 to be specific :). The code:
-----------------------------------------------------------------------------------------------------------------
DataSet objDS =
new DataSet();
DataTable objDT =
new DataTable();
SqlConnection objConn =
new SqlConnection("SERVER=localhost;DATABASE=Northwind;UID=sa;");
SqlDataAdapter objDA =
new SqlDataAdapter("select * from customers",objConn);
objConn.Open();
objDA.Fill(objDS);
objConn.Close();
DataView objDV =
new DataView(objDS.Tables[0]);
objDV.RowFilter = "Country is NULL";
foreach (DataRowView row in objDV)
{
row.Delete();
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
You're probably thinking what I was thinking, “I don't think that logic will work”. I quickly put the code, straight from the book, and ran it. No error. Next I went to the DB to check the Northwind DB Customer table for records with NULL country entries, and as I suspected, there were none. I quickly added a few rows with NULL country fields, and what do you know. The code bombs. So here we have a “perfect example” in a book that was most likely debugged, but would not error because the damn code never even reached the guts of the foreach loop. What a crock. The funny thing is, that the code most likely passed through many a developer's hands in testing and passed. That being said, my only request is: “If you're a developer and your stupid, stop teching published material. Just because you have a MSABADCESAAA does not make you smart.” I ahve learned that QA in .NET books right now is severely lacking. Publishers, please stop putting out crappy code samples. If we were smart, we'd be billing these guys for unproductive time :).