Do you always add a trailing comma?

EG:

  defp deps do
    [
      {:phoenix, "~> 1.4.1"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
    ]
  end

Or do you prefer not to?

  defp deps do
    [
      {:phoenix, "~> 1.4.1"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"}
    ]
  end
3 Likes

Looks ugly, but I do it so that code diffs better reflect the actual change after an addition…

5 Likes

I would add them, but formatter removes them after that. So while I would prefer to have trailing commas (but only in multiline expressions) I stopped using them as that is what code formatter expects.

So there unfortunately is no option for me in this survey :frowning:

14 Likes

Oh I didn’t know that :lol:

I actually prefer them without (think I might have slight OCD, lol) but I’m sure I read in one of my Ruby books once that it was best to add them.

The main problem in Elixir is that trailing comma isn’t always possible, ex.:

def foo(a, b), do: {a, b}

# Legal
foo(10,
    20
)

# Legal
foo(10,
    a: 20
)

# Legal
foo(10
    a: 20,
)

# Illegal
# foo(10,
#     20,
# )
6 Likes

Exactly in the camp where @hauleth is: I always add them for consistency and smaller diffs but the formatter always removes them.

8 Likes

I don’t add them unless the formatter adds them for me. That’s the case for the Javascript stuff I work on and not for the Elixir stuff. When I had to manually added them, I actually felt like I ended up doing more work. Now that formatters add/remove them for me, I don’t care that much. The smaller diff is only slightly compelling to me.

1 Like

Another +1 to adding trailing commas on multi-line lists/maps/etc… I always prefer them, diff’s, moving, etc… are always cleaner, but the formatter for some reason thinks more noisy diffs are more useful for reasons that I don’t understand… ^.^;

11 Likes

I much prefer them but yeah, formatter. Our eslint config requires them for javascript.

6 Likes

I can’t do trailing commas. I think it’s an OCD thing for me or something.

2 Likes

I imagine Elm’s leading commas would drive you to distraction …

7 Likes

I’ve seen C code written that way–I hate it…

3 Likes

Funnily enough, in Elm the formatter is the one who usually rewrites my trailing commas to leading commas.

Actually, it would be nice if the formatter could get an option to keep trailing commas alone (and potentially add them to lists that span multiple lines automatically), although personally I do not have a very strong opinion about this issue at all.

1 Like

I hate trailing commas

You want me to write foo(a,b,) for a function call?

No I want to write foo(a,b) - like I was taught way back in tiny school

:slight_smile:

7 Likes

As long as the call, list, map, whatever fits on a single line, yes, you are totally right.

But if it gets that long, that we break over multiple lines, then I prefer to have a trailing comma.

6 Likes

In case of the single line functions it really doesn’t make sense to write foo(a, b,) over foo(a, b), but in multiline function calls it became quite handful, as:

foo(
  a,
  b,
)

IMHO looks more consistent than:

foo(
  a,
  b
)

And also it result s in cleaner in diffs in tools like Git and is easier for tools like Darcs/Pijul (which operates on on diffs/graggles).

6 Likes

I like not having an extra thing in mind while editing configs/deps or whatnot so personally I find trailing commas lovely. Sadly, the formatter disagrees.

4 Likes

No trailing comma’s on single lines, it’s for multiple lines only, like this is fine:

[1, 2, 3, 4]

But this would be weird:

[1, 2, 3, 4, ]

However for multiple lines:

[
  1,
  2,
  3,
  4,
]

That means that you can add and remove (potentially long) elements with a minimal git diff, you can rearrange without worrying about adding/removing comma’s, etc… etc… It all ‘just works’. :slight_smile:

6 Likes

Why commas at all?

[1 2 3 4]
[
  1
  2
  3
  4
]

foo(a b) # Although the paranthesis might want to move to the left (foo a b)

Lol, well in ML languages no commas is Application, so that would be ambiguous (How would you handle [blah 1 2 3 4], where do the arguments to blah end and the list begin or do more things apply to the return value of blah/N or…?), in Elixir it would be more possible on an initial though but I don’t doubt there are ambiguous things around that too. ^.^