Monday, May 8, 2006

Trailing Commas

I was reviewing some code the other day and I noticed something like this:

 

public enum Foo {

  One,

  Two,

  Three,

}

 

Note the "extra" comma after the last element. I thought to myself, "There's no way this can compile." But in fact it does, and when I stopped to think about it, I realized how nice this C# feature is. It means I can more easily reorder these elements, more easily generate source code, and in general is just one less thing to worry about. And it turns out this works for static array declarations, too, like

 

string[] foo = { "a", "b", "c", };

 

Little touches like this make life in C# pleasant.

 

I'm sure I’m not the first to notice it, but I'll bet I'm not the last, either. ;)

16 comments:

  1. I wonder if this is intentional... Microsoft's C++ compilers have allowed it for ages as well, and it freaked me out the first time I saw it too.



    I've been thinking it's easier to let the parser accept it than reject it, and that they never bothered to fix the bug, but maybe it is in fact a feature.

    ReplyDelete
  2. I'd classify it as a feature, not a bug. I can't think of anything that it hurts, as there's no need to specify an empty element anywhere I can think where I'd want to use this.



    Anyway, bug or feature, I hope they don't change it. :)

    ReplyDelete
  3. Support for trailing coma is part of the C99 standard. AFAIK, it should be included in C++ standard as well. See here:



    http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#518

    ReplyDelete
  4. Kim, I don't have a reference, but I'm pretty sure it's intentional and part of the C# spec. I recall it being explained as to make code generation easier.



    But I could be confusing it with C++...

    ReplyDelete
  5. Isn't it document in the K&R book too?

    ReplyDelete
  6. Just be forewarned that if you do the same thing in JavaScript to an array, you'll get an extra nameless object, that'll really hork up Behaviour.js [not that I speak from experience].



    Personally, I prefer the leading-comma strategy...

    ReplyDelete
  7. Ineresting - a trailing comma on an enumerator-list has been in ANSI C syntax from the start (6.7.2.2), but it does not appear in my copy of the C++ 98 spec (ISO/IEC 14882).



    I just thought that it's been there from the beginning (maybe even K&R days).



    ReplyDelete
  8. Craig, I don't see how this is a cool feature because it seems to simply ignore the empty element.

    ReplyDelete
  9. Shepard: not sure if you're joking or not. In all the context where this would be used, an empty element isn't legal.

    ReplyDelete
  10. Craig, My question is in the context of what you said: "It means I can more easily reorder these elements, more easily generate source code". Unless I am missing something, how would it make it easier to reorder the enum elements and generate source code?

    ReplyDelete
  11. >> how would it make it easier to reorder the enum elements and generate source code <<



    Well for reordering you can simply cut and paste lines - there's no need to remove the trailing comma on whatever enum happens to be last now, and there's no need to add a comma to whatever enum used to be last, but no longer is.



    And for code generation, it's a bit of state that no longer needs to be tracked in whatever is generating the code - just always slap a comma at the end of the enum-list-item. When the generated code is compiled, the compiler won't care if there's a 'dangling' comma at the end.



    These aren't earth shattering issues, but they are small plusses without any drawbacks (as far as I can see).

    ReplyDelete
  12. This is OLD SKOOL C!!! Kernighan and Ritchie... Wow, some people really were born yesterday!

    ReplyDelete
  13. Yup, I've been using that from K&R days. I still use and it every once in a while someone will point out that it's a bug. Sigh. It *is* very helpful for reordering elements, which is why is was put in by K&R iirc.

    ReplyDelete
  14. You can use the same trailing comma syntax for array initializers in both C and C#.



    const Point corners[4] = {

    {1, 1},

    {2, 1},

    {2, 2},

    {1, 2},

    };

    ReplyDelete
  15. This is intential. It was part of the ANSI C standard, and carried over to C#. I was on the working group that discussed and approved this, the rationale for allowing the syntax was all of the reasons that Craig pointed out orginally.

    ReplyDelete
  16. While it's good for making the code easier, it's not part of the standards and therefore leads to unpredictables behavior.

    Don't take me wrong. I'm not exactly the guy who follows the rules but I have had nightmares when I got a bug on my code and you never expect that this little comma could be the reason of it.

    Greetings

    ReplyDelete