3 ms·
Come on, that's not what I meant. "Last line of a function" is honestly a very blatant and obvious way of encoding a returning point (and hence returning expre
by slver 5y ago
Come on, that's not what I meant.
"Last line of a function" is honestly a very blatant and obvious way of encoding a returning point (and hence returning expression in an expr-oriented language).
You literally can't miss which line is the last line, UNLESS you're simply habituated into seeing the keyword "return".
You said it's about communicating intent, not about matching current habits. Those are completely different things.
For example, someone who knows Russian and is learning English, so they make mistakes in English. That doesn't mean English is a poor language for communication, does it?
Case in point, do you actually type "return" in say Java, for a void method, when that return is your very last line of the method? Yes, no?
If no, then you didn't need to communicate "it's returning here" with greater intent. If yes, then I'll accept your point about you finding no return ambiguous. But still, most people don't.
- user-the-name 5y agoBut it's not the last line of a function, though, is it? Put an if statement in there, and it can be literally any line of the function. Sure, you can still work out which one, but it gets more difficult. And, even if it were the last line of the function, you are still not communicating clearly whether the function is meant to return a value at all or not. This both hurst readability, and makes it easier to accidentally introduce bugs.
- slver 5y agoIt's just inescapable that your argument in fact requires explicit returns in mainstream languages whether you return value or not. Because the "does it return value or not" question doesn't sit in expression languages. They always return value.
- user-the-name 5y agoWhy do you say that as if it is not a problem in itself?
- PoignardAzur 5y agoYeah, in practice I have this problem with Rust sometimes. If the returned value is a single line, it's easy to see what is being returned. If it's a 10-lines if/else, and you don't remember the function's return type, and you're not sure how many layers of control flow you're at, etc, it gets a little ambiguous. You can mitigate that with good coding practice, though. There could maybe even be turned into lints.