I’ve been thinking about the use of commas in code for a long time now, and am curious to see what observations others can make on the matter. The idea is this; commas are mostly (technically) unneeded in various programming languages since spaces, newlines, etc are sufficient to parse the code.
For example,
function my_contrived_example(str::String Alpha Beta Gama)
a b c = something_interesting(...) # Do something interesting...
println(str " A:" a " B:" b " C:" c) # Granted, string interpolation would be prettier!
return a b c
end
A B C = my_contrived_example("Look, No commas!" 1 2 3)
… is both parse-able and quite understandable. It also has the advantage that it is terse.
Compare
function my_contrived_example(str::String, Alpha, Beta, Gama)
a, b, c = something_interesting(...) # Do something interesting...
println(str, " A:", a, " B:", b, " C:", c) # Granted, string interpolation would be prettier!
return a, b, c
end
A, B, C = my_contrived_example("Look, using commas!", 1, 2, 3)
Which one is prettier would certainly be debatable (I prefer the one without commas in this case). We are all use to seeing commas all over the place so many may prefer seeing them just because…
I didn’t like omitting semicolons a few years back and thought the code looked ugly without them but am now very use to (and prefer) omitting them unless I feel one is necessary. Could we similarly be addicted to using commas just because everyone always has? …a sort of self-perpetuating habit?
So, could a convincing case be made for making commas optional?
Pros:
- Takes less space, bringing more code into viewable screen area (I hate H-scrolling).
- Arguably, cleaner look, easier to read.
- A few saved keystrokes and slightly smaller files.
Cons:
- In a few cases, may lead to less readable code.
- Most people are accustomed to using commas.
One solution to the problem of having a feature, or not, is to make it optional. This could provide the best of both worlds. Omit commas accept where they serve for clarity or serve some function.
Some randomly copied code:
using Dates: Year Month Week Day Hour Minute Second Millisecond
# vs
using Dates: Year, Month, Week, Day, Hour, Minute, Second, Millisecond
sub2ind_gen((3 5) 1 2)
# vs
sub2ind_gen((3, 5), 1, 2)
Well, those are a few of my thoughts on the matter but I’m curious of what others have to say about it. What are some pros and cons that I haven’t mentioned?