I am fairly agnostic about the indentation feature at this point, as I haven’t used it yet, nor have I used Python or other languages with significant indentation (except I had some limited exposure to CoffeeScript, see below). But I’d like to keep an open mind and I have followed this proposed feature’s saga since the beginning with interest.
First, looking over the examples pointed by Martin, one aspect that I think is a definite plus is the ability to name end blocks, like:
end PatternValDef
You can, of course, do this with comments right now, like:
} // end PatternValDef
But having native syntax (which tools can easily deal with when refactoring/renaming) seems like a plus for readability.
Second, I find that, in our Scala 2 codebase, we already tend to prefer not using brackets when we can. For example our if
/else
containing a single expression per branch look like this:
if (expr)
42
else
43
Similarly, many short function/method definitions look like this:
def foo: Int =
42
And combined:
def foo: Int =
if (expr)
42
else
43
So we have already way fewer braces than, say, in a Java or C codebase. I think that this supports the idea that Scala is already its own thing, syntactically (while 10 years ago Scala was still being sold as much closer to Java), and that going one step further is not going to be a big(ger) issue for newcomers and veteran Scala programmers alike.
Now, of course, the approach above has limits with Scala 2, and my example above has to be changed if you add just one line to the expressions:
def foo: Int =
if (expr) {
val result = 21 * 2
result
} else { // I don't like asymmetric if/else either so brackets here too
43
}
This is something that I find annoying today (and Scala shares this with many languages). It’s my understanding that (sometimes) significant indentation would allow for the following:
def foo: Int =
if (expr)
val result = 21 * 2
result
else
43
For me, it would help solve the “should I use braces or not” dilemma at least in these scenarios, and I think that I would lean towards not using braces in such cases. To me, in this simple example at least, the code is not less readable than the version with braces. On the contrary, I would say that it feels a little more readable at a glance. How does this scale to a large codebase, I don’t know.
(To be fair, tools like IntelliJ now can help you add the braces automatically when you want to enter a new line in a single-expression if/else branch.)
Overall, I am probably 60-75% convinced that the proposed syntax might be a plus once warts are taken care of.
Some of my remaining uncertainty comes from a small negative experience with CoffeeScript, which removes so much syntax that CoffeeScript code appears to me like a sea of tokens. In such a context, it is very hard to tell apart variable names, function names, and function parameters. I think that going too far in removing syntactic symbols reduces legibility of the code. For this reason, I am all for keeping parentheses around parameters lists, for example (except maybe when writing DSL-style code). Similarly, arrows in pattern matching visually help.