Trailing commas

Recently Matthew Pare submitted an entry to Errata suggesting that the lack of a trailing comma in array definitions throughout the book is an error because the Drupal coding standards call for it.

I deleted that submission and thought I'd give an explanation. First, that convention is not used throughout core. Second, I consider adding a comma to the end of your array definition, e.g.

$foo = array(
'bar' => 1,
'baz' => 2,
);

just in case you might add an element later to be a bit like this code:

$zonk = '';
; // we might add something later here so we need a semicolon

However, I have heard something about some IDE not indenting correctly if the comma is not there. I'm light on details. But if that is the case, it sounds to me like a bug in the IDE.

In summary, I consider the practice of putting a trailing comma after the terminal array element to be something that's fine practice but not necessary.

Update: chx informs me that the comma was originally added to the coding standards because (1) var_export() adds it; (2) it makes moving an element of an array up or down in the array easier, and (3) it makes adding a new element to an array easier.

Comments

Missing qualitative

"Second, I consider adding a comma to the end of your array definition, [...]"

You consider that what? :-)

Missing common sense

Um, look below the example....
"Second, I consider adding a comma to the end of your array definition,"
....
"just in case you might add an element later a bit like this code:"

Always a good idea to know what you're talking about before criticizing someone. ;-)

Ummm....

I agree with the first poster - that sentence still doesn't make sense - What exactly is the subject? I? You?

Read it carefully.

"Second, I consider adding a comma to the end of your array definition, .... just in case you might add an element later a bit like this code:"

Here's what I mean!

I consider [subject] a bit like [object].

I consider [wolf whistling] a bit like [leering].

The verb "to be" is implied.

I consider [subject] [to be] a bit like [object].

I consider [adding a comma to the end of your array definition (just in case you might add an element later)] to be a bit like [the following code].

I've edited the page to include the implied "to be" and admit that it's not my most lucid bit of writing ever.

And I'll add that I'm a full convert now. I add the trailing comma all the time.

trailing commas++

drupal has lots of arrays, in many of these arrays, the order is not so significant as the key names

when arrays are stock in trade, they can be a LOT more legible if quickly sorted alphabetically

always having a trailing comma (+ any editor which will do this for you) makes this easy

easy == good

omitting the trailing comma makes sense to me only when the array's meaning is linear

to me it just feels less of an array if you have to treat one line differently :)

Trailing commas

Hi
very Nice one.Drupal has lots of arrays, in many of these arrays.and I agree with the first poster only .

thanks

http://www.drupal-web-developers.com
http://www.drupals-developers.com