« Return to Thread: Why Martin hates string interpolation (Was: Re: Formatting Questions Summary)
Hi all,
I agree with the formatting consensus, except for spaces around string
+. I am the one who proposed no spaces around + in strings. Let me
quickly explain my reasons (btw, I got this originally from Enno
Runne, several years ago).
1. When laying out strings, you want to be visually close to the
result you are getting. Compare:
"test results: "+result1+", "+result2
with
"test results: " + result1 + ", " + result2
Now, quickly: tell me where the spaces go: Is there a space before or
after the comma that gets printed? In the first version, this is
obvious, because the only spaces that are given are in the strings
themselves. And superfluous or missing spaces are one of
the most frequent source of errors in output.
2. With the new convention, the need for string substitution (often
put forward on Scala lists) all but disappears. Compare:
"test results: ${result1}, ${result2}"
with the first version above. You have saved one character per
substitution, hardly worth a new syntax convention. In other words,
Scala's string substitution syntax is written
"+...+"
instead of
${...}
or some other similar proposal. On the other hand, if you insist on
spaces around the +, the story becomes much less convincing.
3. String + is not the same as numeric +, and this often leads to
errors. A classical trap is:
println("the sum is "+ x + y)
With the new convention, you'd tend to write:
println("the sum is "+x + y)
This gives you a visual clue that something is wrong. So you'd
normally correct to:
println("the sum is "+(x + y))
without having to run the program.
For all three reasons, I think an exception to the rule: ``put spaces
around all operators'' is warranted. Or otherwise said: + is
problematic as an operator on strings. Let's try not to write it as
one.
Cheers
-- Martin
« Return to Thread: Why Martin hates string interpolation (Was: Re: Formatting Questions Summary)
| Free embeddable forum powered by Nabble | Forum Help |