https://kotlinlang.org logo
#intellij
Title
# intellij
p

poohbar

07/27/2021, 12:05 AM
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

ephemient

07/27/2021, 12:56 AM
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

Alexey Belkov [JB]

07/27/2021, 9:59 AM
c

Casey Brooks

07/27/2021, 3:31 PM
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

poohbar

07/28/2021, 4:37 PM
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

ephemient

07/28/2021, 8:00 PM
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

poohbar

07/29/2021, 2:52 PM
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

Casey Brooks

07/29/2021, 3:22 PM
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