[Pitch] Allow trailing comma in tuples, arguments and if/guard/while conditions

I see the tides are in favor, but to be fair...

I would optimize for reading. To me a trailing comma reads as an error, unless it is in a lineated list/array literal. It stops my visual scan while I consider whether it is correct. If this changes, we'll have to live with that little hiccup in all the code we read. We easily read 100-1000 lines for every line changed.

I agree it can be awkward to edit, review, or produce, but those are one-time operations.

This change introduces an optional/choice/format alternative, which makes for inconsistency in code bases. (I would have made trailing commas a requirement in lineated list literals when the trailing delimiter is not on the same line.) swift-format has to change to permit commas, but it also has to decide whether to add the comma when converting from inline to lineated (and remove when reversing?). Then developers have to decide whether to reformat to add trailing commas for consistency. There's no way for swift-format to discover code conventions, so one or the other has to be imposed globally (restricted to changed lines only?). (Unclear what should happen when comments are after the comma, or even before.)

If trailing commas are approved, I would start only with the high-traffic cases:

  1. if and guards, because they are long and commas-as-conjunction are a Swifty thing anyway
  2. Parameter lists (both declaration and call) because they're often so long

These are distinguished as often composing complex expressions, where the visual-scan rate is lower anyway.

If I could, I would restrict it to cases without delimiting parentheses on the same line.

As for other cases, I see little motivation and lots of confusion with trailing commas in tuples or type lists (or enum cases) (and parameter packs have little traffic and lots of confusion already). I can see adopting a swift-format rule imposing trailing commas on lineated conditions and parameter lists, but not on type lists or tuples. Type lists are relatively rarely written or changed.

However, tuples are used a lot in evolving API, gaining new members until they become real struct's, so they're higher-traffic both in read and in edit. They're less likely to have complex atoms, but often multiple name/type atoms don't fit on a line. Hmm - borderline?

In favor of using trailing commas beyond enum/list literals:

  • simplify manual edits
  • simplify implementation for code-generators, esp. macros
  • reduce false-positives in git diffs and hence work in code review
  • Emerging availability in other languages reflecting developer expectations
    • Perl, Ruby, JavaScript, CoffeeScript?
    • Not C, cpp, java, python?

Against:

  • Comma signifies continuation. No spoken language uses it as an ending delimiter
  • Trailing comma inline with trailing delimiter reads poorly and makes zero sense
  • Since optional, it becomes a point of formatting convention battles and policy
  • Support in other languages is (like Swift today) limited to array/list/enum literals
    • Liberal support is mostly restricted to less-safe languages
  • Generalizing instead of considering each case
    • conditions: if-let, guard
    • method/function parameter declaration/call
    • Type lists
    • Tuples
    • Parameter packs
    • enum cases
    • ...

For reference:
https://forums.swift.org/t/trailing-commas-in-all-expression-lists/19527

4 Likes