r/ProgrammingLanguages • Pikelet, Fathom • Jun 11 '26

Nontrailing separators do not spark joy

https://buttondown.com/hillelwayne/archive/nontrailing-separators-do-not-spark-joy/
57 Upvotes

68 comments sorted by

View all comments

8

u/Tasty_Replacement_29 Bau Jun 11 '26

This is a surprisingly complex problem. The article talks about JSON data, but I think the following problems are related:

  • Trailing commas yes or no?
  • Should comma separators be optional?
  • Should newlines act as separators?

Trailing commas solve a problem that only exists because commas are mandatory in the first place. The most elegant option is not having a comma:

{
    "a": 1
    "b": 2
}

Even here there are multiple options: If there is a newline, is the comma even allowed? And what if there is no newline, is the comma needed? For the case above, probably yes, because without comma it would be hard to read for a human.

{ "a": 1 "b": 2 }

Lisp does not have commas for simple values (e.g. a list of numbers), and command line don't have commas between arguments:

(defparameter *first-primes* '(2 3 5 7 11 13 17 19 23 29 31))
cp source.txt destination.txt
grep "error" file.txt

So, I think the best rule would be:

  • A newline can act as a comma.
  • Commas are optional if the arguments are "simple". Defining "simple" precisely is important; probably just values, variables and parenthesized expressions, to avoid ambiguities.

At least, that is what my programming language is doing. So you can write eg.

println('Hello ' name ' how are you?')

With this rule, I do not see much need for string interpolation: you only save one character in this case:

println('Hello ${name} how are you?')

If expressions are complex, then a comma or () are needed:

println('Time: ' (millis / 1000) ' seconds')

My language does not have an array initialization syntax, but instead uses a "array" function with varargs. Also here, commas are optional. To me, the second version is easier to scan:

smallPrimes : array(2, 3, 5, 7, 11, 13, 17, 19, 23, 
                    29, 31, 37, 41, 43, 47, 53, 59, 
                    61, 67, 71, 73, 79, 83, 89, 97)

smallPrimes : array(2 3 5 7 11 13 17 19 23 
                    29 31 37 41 43 47 53 59 
                    61 67 71 73 79 83 89 97)

The trade-off is that the language must define exactly when whitespace acts as a separator. But if those rules are simple, I think whitespace and newlines often produce cleaner, more succinct code than mandatory separators and trailing commas.

12

u/MadCervantes Jun 12 '26

Why are people always so obsessed with "saving characters"? Why on earth is that the metric used to judge anything at all? The purpose is readability, not saving characters.

1

u/Tasty_Replacement_29 Bau Jun 12 '26

Sure, best readability is somewhere between the extremes "code golfing" and "very verbose".

But I don't see evidence that extra punctuation improves readability.

3

u/MadCervantes Jun 12 '26

That's fair, more characters ain't inherently better either. Some of it may come down to what you're used to. Interpolation is easy for me but that's just my background.

2

u/busres Jun 16 '26

I agree. I use [ 2 3 5 7 11 ] and [ a=1 b=2 ], and find it both readable and easily extendable. (All whitespace is equivalent.)

1

u/renozyx Jun 11 '26

With this rule, I do not see much need for string interpolation: you only save one character in this case:

I disagree, Python's string interpolation is the benchmark on which I judge other language and I don't even use Python!

2

u/Tasty_Replacement_29 Bau Jun 11 '26 edited Jun 11 '26

Sure, I'm not saying string interpolation is bad... I should have been clearer: my language tries to be very simple, and so I'm trying hard to avoid all features that are not strictly necessary. And I came to the conclusion that in fact, string interpolation is not strictly needed, because you can achive almost the same using this approach (I think what I use is called "juxtaposition").

I know many languages use string interpolation. The approach I use is only used by Rexx, shell languages, and some Basic dialects I think. Update: Clojure, Fennel, Hy

say 'Hello' name '!'               <<= Rexx
echo "Hello" $NAME "how are you?"  <<= Shell
echo "Time:" $seconds "seconds"    <<= Shell
(println "Hello" name "!")         <<= Clojure, Fennel, Hy

I was also thinking about making the parenthesis optional for method calls (like early versions of Python), but then rejected this idea because it actually makes the syntax more complex.