Instinctively, I feel strongly against this pitch, but I need to think about it more in order to elaborate a proper response, I'm not quite grasping the actual reasons why I'm against this idea, because the examples provided that should show the problems generated by omitting the return
on the last expression don't seem too problematic to me.
But I'd like to at least quickly respond to this:
I disagree. To me, arbitrarily splitting a function because it's "too long" hurts code readability and clarity, forces developers to jump around the codebase to understand what a piece of code does, destroys local reasoning... it's just a bad idea.
The feature of omitting the return value from the last line in a returning function should stand on its own for arbitrarily long functions, but I think that, when reading a function, the type of the function, especially the return type, is something that the developer should keep in mind in general, so I'm not particularly worried about it. What worries me more is that seeing a bare expression (or just a value) at the last line without return
would just look "weird" and not quite right, and would not communicate in any way that the value is being returned.
In a single-line function it makes sense to omit the return
because a 6 character keyword plus space, in such function, doesn't really add anything to the meaning of the function code: it's obvious from the immediate context, and the return
would be just unnecessary noise.
But in a large function, the return
at the end actually adds something. Instinctively (again) I feel like omitting the return
would be perfectly fine for functions (or do
and other type of expressions) that are very short, like max 3 or 4 lines, but I wouldn't like it for longer ones: the utility of omitting return
from single-line functions is, to me, due to the fact that the function is really small, not that it's specifically only 1 line long.
I know that a linter could be used, to not allow omitting the return
for large functions, but I don't think the existence of linters should be considered an argument in favor of a language feature, if it happens to be undesirable in some cases. On the other end, the benefits of (optionally) omitting the return
at the last line could make this point about linters more acceptable, so I'm finding a little hard to rationally justify my dislike for this pitch.