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 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. ;-)

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options