Always Add That Trailing Comma!

Let's say you are defining a long array in PHP. It's so long that you put every element on its own line like so:

$schema = [
    'userID:i' => 'ID of the user.',
    'name:s' => 'Name of the user.',
    'password:s' => 'Password of the user.',
];

Notice how the last item in the array has a comma after it? That's not a mistake and has been supported in PHP for a long time. We haven't used this feature all that much, but it is considered a best practice. Why? Here are a couple of reasons:

  1. If you decide to move lines around you can do so without having to add or remove commas depending on whether the line is the last item in the array.
  2. If you add lines to the array in a later commit then the diffs are cleaner because only the line you added will show as changed.

It's really number 2 that we want. Clean diffs make pull requests easier to read for humans and easier to merge for computers. Trailing commas are one thing humans and computers can agree on.

Here are a few gotchas and tips when using trailing commas:

  1. Don't use trailing commas for arrays that are on a single line. That's gross.
  2. Don't go back and change every array declaration you can find. That's a make-work project we don't need to do. Just try and use trailing commas on new arrays and maybe old arrays that you touch doing some other task.
  3. You can put trailing commas in typescript as well. In fact, Adam has put a lint rule to enforce this.
  4. Typescript also allows trailing commas in function declarations and calls; PHP does not.

Comments

  • Let me just say that, as a devout follower of Lord Brackos, this offends me deeply in my core.

    That said, Brackos is pragmatic if nothing else, and so I am certain he would advocate for this as well.

  • I've been a big fan of this practice for a long time! We should probably add that in our coding standard doc!

  • FWIW our typescript auto-formatter automatically inserts trailing commas on multi-line arrays and objects.

  • Being new to the application and internal processes, we seem to spend a lot of time on ensuring things that could be automated are performed manually (with all of those human mistakes that accompany manual processes).

    If we run Prettier (an opinionated code re-formatter that works wonderfully on JavaScript and is in pre-release for PHP) on git commit to all changed files, we will never have incorrectly formatted code making it into the repository.

    Is this a path we wish to go down? It does require a one-time dev effort to implement.

    (And yes, it can also automatically add the trailing commas to arrays)

  • Unknown
    edited May 2018

    @chrisdepage said:
    Being new to the application and internal processes, we seem to spend a lot of time on ensuring things that could be automated are performed manually (with all of those human mistakes that accompany manual processes).

    If we run Prettier (an opinionated code re-formatter that works wonderfully on JavaScript and is in pre-release for PHP) on git commit to all changed files, we will never have incorrectly formatted code making it into the repository.

    Is this a path we wish to go down? It does require a one-time dev effort to implement.

    (And yes, it can also automatically add the trailing commas to arrays)

    I've been recommending that we use git hooks to handle issues like that for a little while now. I think that it would be great.

  • We're currently using prettier for typescript currently. If we're going with a code formatted like that it might be a good idea to stick with the same tool. We should probably wait until the PHP plugin reaches a stable version though.

    https://github.com/prettier/plugin-php

    Please note that this plugin is currently in alpha stage and still under active development. We encourage everyone to try it and give feedback, but we don't recommend it for production use yet.

  • There are other solutions specific to PHP too:
    https://github.com/friendsofphp/PHP-CS-Fixer
    https://github.com/mmoreram/php-formatter

    We should try to get requirements and evaluate the options before anything else.