Article V2YE CodeSOD: The Cleaner

CodeSOD: The Cleaner

by
Remy Porter
from The Daily WTF on (#V2YE)

In software development, there are people who get the unenviable task of being the cleaner. Somebody makes a mess, and the cleaner comes in to take care of it. And that brings us to Tina.

thewolf.gif

Tina was brought in as a cleaner. There was an application that was a mess, and the powers-that-be wanted it taken care of. Tina took a look, and she noticed that there were a lot of round trips to the database. In fact, after profiling, it almost looked like every query ran at least twice. She saw code following this pattern everywhere:

 if (!IsTableEmpty("users")) { results = GetTableData("users"); }

Or, worse:

 if (!IsTableEmpty("users") && !IsTableEmpty("orders") && !IsTableEmpty("line_items")) { users = GetTableData("user"); orders = GetTableData("orders"); lines = GetTableData("lines"); for (int i = 0; i < users.count; i++) { for (int j = 0; j < orders.count; j++) { for (int k = 0; k < lines.count; k++) { //manually join all the records together //with a giant block of conditionals } } } }

With that sort of logic, Tina knew exactly how this particular application needed to be "taken care of", but she was curious. Curiousity, of course, isn't a good trait in a cleaner. Don't ask questions. Don't poke your nose where it doesn't belong. But she just had to know- how was IsTableEmpty implemented?

 private bool IsTableEmpty(string s){ SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM "+s,"server=****;database=****;User ID=****;Password=****"); DataSet ds = new DataSet(); adapter.Fill(ds); int count=0; foreach(DataRow row in ds.Tables[0].Rows){ count+=1; } if(count>0) return false; return true; }
release50.png[Advertisement] Release!is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped! TheDailyWtf?d=yIl2AUoC8zAbFOFu9ziPL8
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments