Richard Heathfield said:
Ben Pfaff said:
Surely the whole point of generating the code automatically is that you
don't have to maintain it?
I think Ben's point (not that I'd presume to speak for him) is that
the trailing-comma rule can make it easier to maintain the code that
generates the code.
If you want to generate an initializer list and you already have all
the information in an array, then the trailing-comma rule doesn't help
much; it's easy enough to know when you're about to write the last
element and drop the comma. It's *slightly* more convenient to be
able to generate a comma after every item, but not enough to be worth
marring the language.
But if you don't know whether the current item is the last one until
after you've written it (say, you're reading input from a file), then
being able to write the trailing comma after every item can be more
significantly convenient.
It's still not impossible to avoid the trailing comma (if nothing
else, you can build an array or list internally and write the whole
initializer only when it's complete), and I still have mixed feelings
about whether the rule is worthwhile, but it's not completely useless.
An interesting (I think) point is that I've never heard anyone
complain that semicolons are used as terminators rather than as
separators, as they are in Pascal. The fact that we can (and must)
write:
{
statement1;
statement2;
}
rather than
{
statement1;
statement2
}
makes programs easier to maintain when we want to add a third
statement. But the idea that a comma should be a terminator rather
than a separator seems somehow unnatural -- even though they're both
separators in written English.
To summarize:
{ statement1; statement2; } /* good */
{ statement1; statement2 } /* bad */
{ initializer1, initializer2, } /* bad */
{ initializer1, initializer2 } /* good */