Commas at the Beginnings of Lines

This is another in a series of "Strange Programmer Habits" articles. In each article we highlight a strange-looking construction that looks wrong at first glance, but has a legitimate purpose. You don't have to use this construct to be a decent programmer. You don't even have to agree with it's justification. But after reading this article, you'll hopefully understand what was going through the mind of the programmer that used the construction it describes.

So, consider this C program:


   #include <stdio.h>

   #define _VERB_LENGTH 5

   char *verbs[] = {
     "quit"
   , "score"
   , "inventory"
   , "go"
   , "get"
   };

   int main() {
     int i;

     for( i = 0; i < _VERB_LENGTH; i++ ) {
       printf( "verb %02d: %s\n", i, verbs[ i ] );
     }
   }

or it's javascript equivalent:


   var verbs = [
       "quit"
     , "score"
     , "inventory"
     , "go"
     , "get"
   ];

   for( var i = 0, il = verbs.length; i < il; i++ ) {
     console.log( "verb " + i + ": " + verbs[ i ] )
   }

Both these programs declare an array of strings and then print them out. But contrary to popular convention, the commas separating individual elements of the verbs array come not at the end of the line, but at the beginning.

The compiler (or interpreter) couldn't care less about this stylistic convention, of course. All it cares about is if there are commas between array elements. The comma-first style is there to make it easier for you to add, delete or move single lines in the array. By putting the comma at the beginning of the line, you move the elements in the array around without having to manually add (or remove) a trailing comma at the end of the array.

This strange habit doesn't effect the output your compiler produces and it's main benefit is to save a couple milliseconds when cutting and pasting entries in an array. But a small number of programmers (myself included) have gotten used to seeing arrays that look like this, so don't be surprised if you see this style from time to time.

At this point, you might be thinking, "this is crazy, it's just as likely you'll want to move an item from the beginning of an array where you don't have a leading comma as it is you'll want to move an item at the end of an array where there's no trailing comma."

And you're right.

This strange habit is only beneficial if you seldom modify the beginning of an array. In this case I'm not trying to convince you this is a thing you should do as try to explain what someone was thinking when they did it. A counter example is this code:


   #include <stdio.h>

   char *verbs[] = {
     "quit" ,
     "score" ,
     "inventory",
     "go",
     "get",
     NULL
   };

   int main() {
     int i;

     for( i = 0; verbs[i] != NULL; i++ ) {
       printf( "verb %02d: %s\n", i, verbs[ i ] );
     }
   }

This code is functionally equivalent to the C program above, but removing the NULL pointer from the list of verbs will break this code. Thus, you're sort of guaranteed to never want to remove the last item. Again, not trying to convince you to do this. Just pointing out this is something people sometimes do.