Throw away code can be ugly code

Recently, I needed to do a bunch of test setup in a web application. I had to manipulate around 5,000 records, and due to a time crunch and few people around over the holidays, it was easier for me to get at the GUI than it was the database. So I decided to write a quick Watir script to do the edits for me.

I had a spreadsheet with all the changes I needed, so I decided to let Excel write the bulk of my code. Using the CONCATENATE() function, I inserted the following into the first row:

CONCATENATE("ii == ", A1, " ? temp = ", A6, A3, A6, " : temp = temp")

That creates something that looks like this:

ii == 1 ? temp = "Some Value" : temp = temp

All I had to do was drag that down to fill each row and I was able to copy and paste a very large block of code from Excel into a loop in Ruby:

for ii in (1..5005)
#do some setup using Watir
ii == 1 ? temp = "Some Value" : temp = temp
ii == 2 ? temp = "Some Value" : temp = temp
ii == 3 ? temp = "Some Value" : temp = temp
ii == 4 ? temp = "Some Value" : temp = temp
ii == 5005 ? temp = "Some Value" : temp = temp
#do something afterward using Watir

Now, this is REALLY UGLY code. I know it's ugly, and you know it's ugly, but I'm defending it because it only took me five minutes to get my script running. It ran for a couple hours, and I was done. All of my test setup complete. I deleted the script and moved on.

While my solution wasn't elegant, it worked. Someone later told me about an Excel library I could have used, and I didn't feel bad about not using it. Sometimes you need to resist the urge to write an elegant solution if you're just hacking away at a simple problem.
AutomationMichael Kelly