Can we please change the IDE code style mechanism ...
# intellij
p
Can we please change the IDE code style mechanism to enforce “no trailing comma”? Adding ugly syntax to the language is one thing but not allowing us to easily avoid it is worse.
e
well, you could set the language version to 1.3, that should disable trailing commas
(surprising to hear how differently people see it, though. in my view every argument in a multiline function calls should have a comma for consistency)
11
in any case, you could probably fork https://github.com/pinterest/ktlint/issues/709#issuecomment-858633157 to enforce no trailing commas
a
c
Trailing commas are wonderful for using multiple cursors to edit many lines at once, or for duplicating the last line to add a new parameter. Having options around this would be nice though. Personally, I’m on the other side where I’d rather auto-formatting add the trailing comma in a multi-line parameter list
💯 2
3
p
I’d rather optimize the syntax for reading not for editing. Reading is what I do most of the time. The extra comma is distracting for my eyes and makes it hard to distinguish where the end of the list is. We already have sophisticated tools (IntelliJ?) that can help us easily edit elements in a list without worrying about the commas. “Move statement” action handles this fine for example. No need to change the grammar of a language for such a little thing.
e
to me, with Kotlin's code style (closing paren unindented on following line), having a comma after every argument is easier to read. I don't understand your position (but as long as it doesn't impact my usage, fine)
p
It’s objectively typographically wrong to end a list with a comma. That’s all. Nothing hard to understand.
You don’t write: “Please buy: eggs, milk, bread,”
c
For a single line, yes that makes sense and should not have the trailing parameter. But when writing a list on multiple lines, you wouldn’t use commas at all, you’d use a bulleted list, and you want all those bullets to have the same formatting. You wouldn’t do
Copy code
Please buy:
- eggs
* milk
1. bread
You’d do
Copy code
Please buy:
- eggs
- milk
- bread
A bulleted list very clearly is not grammatically correct, and yet it still makes sense. For Kotlin, it’s about turning the inline “sentence” of parameters into a “bulleted list” of parameters. Just like it makes more sense to break parameters down by putting the first one on the next line, it also makes more sense to include a trailing comma in a multi-line list to keep all “bullets” looking the same
Copy code
// bad
doAThingWithManyParameters("one",
    "two",
    "three")
// even worse
doAThingWithManyParameters("one",
    "two",
    "three"
)
// better
doAThingWithManyParameters(
    "one",
    "two",
    "three"
)
// best
doAThingWithManyParameters(
    "one",
    "two",
    "three",
)
Not to mention, diffs are a lot easier to read when changes are made to that list, too. Without a trailing comma, a single-line change (adding 1 parameter) shows a diff affecting 2 lines
1