TDD is a Habit

Up-front admission: I am not “a software developer.” I dabble in software development. I work with software developers; I test and support their code and, though I write some code (and some of that has even shipped to customers as part of various products) I am not “officially” a developer. I don’t even play one on TV. What I am is someone who enjoys working with, learning from, and exploring the software development zeitgeist. For the past year or so, perhaps longer, that has included Test Driven Development (TDD).

I'm test-driven!The basic idea behind TDD is that a developer should not only write code to test their code (in itself a recommended practice, though not widely enough embraced), but that those tests should be written first. In a way, this falls into the “measure twice, cut once” idea of making sure you’re doing the right thing. If you’re a software developer and haven’t heard of TDD, stop right now and go start reading.

Friday I was working on a project, a small application I’ve been writing to compare data on multiple systems. I had started off earlier in the week rather idealistically, designing a few classes and writing the tests for them first. That all went well. Then I got to the “real work” of the app, opening connections to the databases, getting data from them, combining & comparing data. I ran into some difficulties, and I must have spent about two hours trying to figure it out including about an hour of one of our senior developers’ time. Once we found the problem, it was one of those simple “oh, I should have seen that!” moments. Problem solved, only three “man-hours” spent.

Then this morning, over my Cheerios and coffee, I’m reading a little of Robert Martin‘s Clean Code, specifically Chapter 7 on Error Handling. There’s a section on writing one’s try-catch-finally statements first, and while I’m thinking about that I realize that – though I did that Friday – what I didn’t do was to write any tests for all that database code. I was catching exceptions and all that, but I’d never actually tested what the catch was reporting. Doh! If I’d taken half an hour to do things “right” on Friday morning, the afternoon would have been a few hours shorter.

So why didn’t I do it “right” the first time? Because it’s not a habit. It’s not the way I’m used to working. Even though I know I should, when things get rolling it’s just not the first thing that my brain and hands jump to. And again, I’m not a full-time, “real” developer with years and years of ingrained habits.

Peter Provost writes that Kata are the Only Way to Learn TDD. I don’t know about only, but without forcing yourself into some repetition, the habits won’t be formed and you’ll too easily find yourself three hours into fixing a problem that shouldn’t have existed in the first place.

Intro/Refresher Resources for SQL

SQL iconI was asked to find a few references for our people here; folks who are new to SQL or just haven’t used it in a while. Rather than have my notes tied up in email, I figured I’d put them here for all to use. We use both Oracle Database (10g now, 11g soon) and Microsoft SQL Server.

For a good SQL intro/refresher, I would recommend Murach’s SQL Server 2008 for Developers: Training & Reference by Bryan Syverson and Joel Murach. It is, as the title suggests, targeted toward the use of Microsoft SQL Server, but the 90+% of SQL Language features will apply to other SQL-based databases as well.

added later: also check out Craig Earl‘s SQL for Testers.

Another very good resource is Learning SQL, by Alan Beaulieu; this book covers the SQL language as a whole, without being specific to any one implementation.

[added later] As you progress, these also look to be good if you’re interested in learning more about SQL development:

For a practice environment, you can download and setup either MS SQL-Server Express or Oracle Express on your computer.

There are some basic differences between Oracle and Microsoft SQL Server, I’d recommend wandering through these if you, like us, will be playing in both worlds:

The first difference that we will see right away (from the StackOverflow question above): Transaction control. In Oracle everything is a transaction and it is not permanent until you COMMIT. In SQL Server, there is (by default) no transaction control. An error half way through a stored procedure WILL NOT ROLLBACK the DDL in previous steps. There are settings that can change it, but this is the default behavior.

There are also excellent courses available at Pluralsight, and they have a 10-day free trial available. You may also wish to check out the SQL University (start here).

Enjoy, and let me know if you have other resources that have worked well for you.

Practices of an Agile Developer

This past week, I read Practices of an Agile Developer, not because I’m currently employed as a developer, nor because my employer uses them, but because I believe the guidelines are good ones to be practiced no matter what the profession. Check out some readers’ notes, then invest some time in the book yourself, especially if you are in the software development arena.

Is your site intentionally broken?

As a follow-up to my previous post, Is Your Text Broken, see also John Gruber’s article today on Tynt, the Copy/Paste Jerks:

All they’re really doing is annoying their readers. Their websites are theirs, but our clipboards are ours. Tynt is intrusive, obnoxious, and disrespectful. I can’t believe some websites need to be told this.

Another example of intentionally broken websites. It’s sad, really.

It’s the Little Things

Application development, especially User Interface, is very much about “the little things.” The things that make life easier for users. Here’s a simple example, one that bugs me almost every time I use a browser.

There are probably URLs that you type often and, for whatever reason, you don’t bother to make into bookmarks or favorites. Maybe you’re just a keyboard person, and Control-L type-type-type is easier for you. Maybe you’ve got them memorized and there are too many to fit nicely in your tool bar. Whatever.

So you alt-Tab to your Firefox window, click in the address bar (or use the Control-L keyboard shortcut) and start typing a URL. If the letters you type are part of an address you’ve used before, it shows you a list.

screenshot from Firefox

This is all well and good, and Internet Explorer does something similar. It’s fine. You type, you mouse or cursor down, you go on with your day.

Until you use Chrome.

Control-L, type type type, and…

Screenshot from Chrome
(There really was also a menu, Chrome just didn’t leave it down for my my screen-capture software to grab in the image.)

Hey, check that out – the URL that matches my typing and that I use the most is already in my address bar. I don’t have to cursor- or mouse-down to it, I can just drop my right pinkie on the Enter key and zoom, away we go. Imagine that. Control-L, face[enter] and I’m on my way to Facebook. Control-L, netf[enter] and I’m looking at my Netflix queue. No more hunting through a drop-down list unless I’m looking for something different than usual.

It’s not a big deal but as with most conveniences, once you’ve gotten used to it everything else is just a little more frustrating.

So – what’s frustrating your users/customers, what’s your competitor is making easier for them?

A Python-MySQL Project

I assigned myself a project for the weekend, writing a little program that I could use to disable comments on blog posts based on their age. Python’s already installed on the Mac, so I figured it’d be pretty easy to use that to make updates to my WordPress database.

The first thing needed is the connecter to allow Python to talk to a MySQL datbase, mysql-python. The setup program for the newest version, 1.2.2, didn’t want to run for me and the hints I found around the ‘net didn’t seem to apply, so I downloaded version 1.2.1_p2. There was still some work to do to get it to install though.

To install the connector, MySQL needed to be installed locally first. Sun now owns MySQL but fortunately there’s a Community version available for download free of charge for personal use.

Once that’s installed, it’s back to the MySQL-Python connector installation. Several sites had notes which helped me find that tries to call mysql_config but can’t find it, so I edited the line from ...popen("mysql_config... to ...popen("/usr/local/mysql/bin/mysql_config...

The setup program gave me another error, this time because I don’t have gcc installed – to complete the build it needs to be able to call the C compiler. Back when I set up my PowerBook I’d not installed that part of the developer tools, so I had to dig out my OS X disks and do that. Then finally the setup worked cleanly:

python clean
python build
python install

Now that all the bits are in the right places, I’m finally ready to start getting some code going. I added permission to my databases, creating a new user that has remote access, and used that.

# use the connector library
import MySQLdb
# connect to the database, using the correct username and password
conn = MySQLdb.connect (host = "", user = "my_db_username", passwd = "my_db_password", db = "my_database") # look in your wp-config.php file to find the right database
# update the fields, using an SQL call
conn.query("""UPDATE wp_posts SET comment_status = 'closed' WHERE year(post_date) < 2008 """) # close the database connection conn.close ()

It's not long or fancy, but it does exactly what was needed at the moment. I'll build more of a wrapper around it later, to make it more flexible, add error checking etc.

An evening of forensic work while watching a movie with my family, followed by some light development this morning... success. And, perhaps more importantly, it gets my system back to a place where I can start digging into larger projects I've been thinking about.

Got Firebug?

Web developers, have you looked into and downloaded Firebug yet? I just came across a mention of it on Matt’s weblog and it certainly looks worth a good look or three. Integrating live website editing — CSS, HTML, Javascript — fight into Firefox! Watch Jesse Newland’s screencast and I think you’ll be as interested as I am.

There are still many whole-site managment features that Dreamweaver has that Firebug doesn’t appear to, but I’m wondering… could this be the open-source project that puts a dent in for DW and other “full-featured” web development tools?

[later] see comments from Firebug creator Joe Hewitt.