Comma Trouble

Published 2011-07-11

The comma at the end of the list, good or bad? For javascript, the latter.


I’ve paid the bills coding in TCL, Perl, and (lately) Python for a long time. I’ve had a few fun digressions into C, C++ (really C with classes), and now Go, but I’ve been banging on the keyboard in dynamic languages for systems and web development since way before the millennium…

Before I came to find TDD a necessity, I had a lot of defensive coding habits. You know why: setting up the test and associated data and then finding you made a syntax error is frustrating – gotta setup the test again. One of those defensive habits was always putting a comma after the last item in a list to avoid missing the comma when adding another item. Here’s an example in Python:

def foo():
    l = [

Or similarly, creating a visual reminder for languages that don’t allow the extra comma (my fave, SQL):

    `id`                 int(11)   NOT NULL auto_increment
  , `user_id`            int(11)   NOT NULL
  , `create_time`        timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP


The other day I was preloading data in a template for use in some DHTML – you select an item, and a drop down gets populated based on that item – standard stuff. Following my old habit, I added a comma after the last item in the Javascript list. Worked fine in FF, Chrome, and Safari, but IE counted the list as one longer than the others and the DHTML broke. I Qunit tests for the Javascript, so running it in IE showed the issue quickly, but it put a nail in the coffin of that habit.

As I’ve gone over to the dark side and become a dedicated unit tester, a lot of those old habits are no longer necessary. As long as I have good coverage (famous last words), I’ll catch the missing commas before I push to prod.