# Revisit trailing commas in function call parameters?

- Thread: https://externals.io/message/86730
- Original archive: https://news-web.php.net/php.internals/86730
- Started by Christian Schneider on 2015-06-17T12:05:37+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #86730 — Christian Schneider — 2015-06-17T12:05:37+00:00

URL: https://externals.io/message/86730#86730

Hi there,
now that syntax for variadic functions was added I quickly wanted to ask whether allowing trailing commas for function calls could be reconsidered.

It is a very small change to the language allowing something like
	my_variadic_function(
		"foo",
		"bar",
		"qux",
	);
which makes adding/removing additional parameters a little easier: No comma has to be added/removed from the last line.

Previously this was considered unnecessary but maybe things changed enough to reconsider it? It doesn't involve any BC break and I attached the very simple patch needed in the master branch.

If people still consider it more harm- than useful then please don't flame me and I'll shut up again :-)

Cheers,
- Chris


diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
index 9612324..f299f3e 100644
--- a/Zend/zend_language_parser.y
+++ b/Zend/zend_language_parser.y
@@ -639,7 +639,7 @@ return_type:
 
 argument_list:
                '(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
-       |       '(' non_empty_argument_list ')' { $$ = $2; }
+       |       '(' non_empty_argument_list possible_comma ')' { $$ = $2; }
 ;
 
 non_empty_argument_list:

---

## #86734 — Ferenc Kovacs — 2015-06-17T16:36:26+00:00

URL: https://externals.io/message/86730#86734 · In reply to: #86730

On Wed, Jun 17, 2015 at 2:05 PM, Christian Schneider <cschneid@cschneid.com>
wrote:

> Hi there,
> now that syntax for variadic functions was added I quickly wanted to ask
> whether allowing trailing commas for function calls could be reconsidered.
>
> It is a very small change to the language allowing something like
>         my_variadic_function(
>                 "foo",
>                 "bar",
>                 "qux",
>         );
> which makes adding/removing additional parameters a little easier: No
> comma has to be added/removed from the last line.
>
> Previously this was considered unnecessary but maybe things changed enough
> to reconsider it? It doesn't involve any BC break and I attached the very
> simple patch needed in the master branch.
>
> If people still consider it more harm- than useful then please don't flame
> me and I'll shut up again :-)
>
> Cheers,
> - Chris
>
>
> diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
> index 9612324..f299f3e 100644
> --- a/Zend/zend_language_parser.y
> +++ b/Zend/zend_language_parser.y
> @@ -639,7 +639,7 @@ return_type:
>
>  argument_list:
>                 '(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST);
> }
> -       |       '(' non_empty_argument_list ')' { $$ = $2; }
> +       |       '(' non_empty_argument_list possible_comma ')' { $$ = $2; }
>  ;
>
>  non_empty_argument_list:
>
>
> --
> 
> 
>
>
hi,

just linking to the previous rfc:
https://wiki.php.net/rfc/trailing-comma-function-args and discussion:
http://www.serverphorums.com/read.php?7,655532

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #86735 — Bishop Bettini — 2015-06-17T16:53:16+00:00

URL: https://externals.io/message/86730#86735 · In reply to: #86734

> On Wed, Jun 17, 2015 at 2:05 PM, Christian Schneider <
> cschneid@cschneid.com>
> wrote:
>
> > […]
> reconsidered.
> > […]
> enough
> > […]
> flame
> > […]
> hi,
>
> just linking to the previous rfc:
> https://wiki.php.net/rfc/trailing-comma-function-args and discussion:
> http://www.serverphorums.com/read.php?7,655532
>
>
I missed the vote on this one, but I'd be +1.

That said, the VCS blame issue doesn't seem strong enough for me.  Just put
the comma at the front of the list item and you don't have to modify two
lines:

// initial state
$fp = fopen(
    "sample.txt"
    , "r+"
);

// modified state
$fp = fopen(
    "sample.txt"
    , "r+"
    , true   // only this line changed
);

I realize this may grate on styles, and it doesn't quite align with
recommendations made in canonical books like Code Complete, but it's a
decent way to get around the VCS issue and "trailing comma errors" in
syntaxes like JSON.  And when you work in one language that doesn't have
the PHP nicety of trailing comma, you either adopt the lowest common
denominator (not using them) or take on the mental burden of doing both in
context (and the risk of using the trailing in unsupported contexts).

To me, the strongest argument is consistency.

Thanks,
bishop

---

## #86743 — Yasuo Ohgaki — 2015-06-18T06:25:12+00:00

URL: https://externals.io/message/86730#86743 · In reply to: #86730

Hi Christian,

On Wed, Jun 17, 2015 at 9:05 PM, Christian Schneider <cschneid@cschneid.com>
wrote:

> Hi there,
> now that syntax for variadic functions was added I quickly wanted to ask
> whether allowing trailing commas for function calls could be reconsidered.
>
> It is a very small change to the language allowing something like
>         my_variadic_function(
>                 "foo",
>                 "bar",
>                 "qux",
>         );
> which makes adding/removing additional parameters a little easier: No
> comma has to be added/removed from the last line.
>
> Previously this was considered unnecessary but maybe things changed enough
> to reconsider it? It doesn't involve any BC break and I attached the very
> simple patch needed in the master branch.
>
> If people still consider it more harm- than useful then please don't flame
> me and I'll shut up again :-)
>

PHP allows

array(
  1,
  2,
  3,
);

therefore

my_variadic_function(
   "foo",
   "bar",
   "qux",
);

is consistent behavior to me.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #86752 — Florian Anderiasch — 2015-06-18T17:16:24+00:00

URL: https://externals.io/message/86730#86752 · In reply to: #86743

> > […]
> 
> PHP allows
> 
> array(
>   1,
>   2,
>   3,
> );
> 
> therefore
> 
> my_variadic_function(
>    "foo",
>    "bar",
>    "qux",
> );
> 
> is consistent behavior to me.

If variadic functions allow this and normal functions don't (and by most
PHP coding standards you'll format like this all the time because of 80
chars limit I don't see how this is in any way consistent. It's still a
function call after all and not an array.

~Florian

---

## #86925 — kelerest123@gmail.com — 2015-06-28T14:19:31+00:00

URL: https://externals.io/message/86730#86925 · In reply to: #86752

+1 for allowing trailing comma in every function call.


Regards,
Kubo2


2015-06-18 19:16 GMT+02:00 Florian Anderiasch <ml@anderiasch.de>:
> 
> > […]
>
> If variadic functions allow this and normal functions don't (and by most
> PHP coding standards you'll format like this all the time because of 80
> chars limit I don't see how this is in any way consistent. It's still a
> function call after all and not an array.
>
> ~Florian

---

## #86974 — Fred Emmott — 2015-06-30T17:10:03+00:00

URL: https://externals.io/message/86730#86974 · In reply to: #86925

Some more context:

https://bugs.php.net/bug.php?id=64176 <https://bugs.php.net/bug.php?id=64176>
https://wiki.php.net/rfc/trailing-comma-function-args <https://wiki.php.net/rfc/trailing-comma-function-args> (failed 15-20)

On a more positive side, this change was very well received within Facebook when implemented within HHVM; this was a mix of:

 - the ‘git blame’ advantage is much bigger than we expected; this is probably true for any large project that has many contributors to the same file. It doesn’t help much if each file basically has an ‘owner’.
 - people new to PHP liked not having to remember where they are allowed and when they’re not. This felt like removing one small inconsistency without much of a downside

> 
> 
> +1 for allowing trailing comma in every function call.
> 
> 
> Regards,
> Kubo2
> 
> 
> 2015-06-18 19:16 GMT+02:00 Florian Anderiasch <ml@anderiasch.de>:
> > […]
