2 ms·
Well, I'm not sure I'm on one side or the other of the statements-or-no-statements debate, but in general I think the argument in favor of separating statements
by kragen 5y ago
Well, I'm not sure I'm on one side or the other of the statements-or-no-statements debate, but in general I think the argument in favor of separating statements from expressions is that it adds redundancy to your programs, which makes them easier to read and enables the compiler to produce better error messages when you have a syntax error. Sometimes you can trade this off against other aspects of syntax you'd like to improve; Lua, for example, omits semicolons and isn't white-space sensitive, but the compiler can still emit reasonable syntax error messages because of its strict separation of statements and expressions.
I think you're overstating the case with
return if(sth) {
foo = SomeProcessing();
Transform(foo);
} else if(sthelse) {
Transform(someDefault);
} else {
someErrorDefault;
};
You can transform this to the following in even the most limited ALGOL-like languages, which is less clear but not nearly as heinous as the alternatives you mention:
var rv: SomeType; -- return value
rv = someErrorDefault;
if(sth) {
foo = SomeProcessing();
rv = Transform(foo);
} else if(sthelse) {
rv = Transform(someDefault);
}
return rv;
The more popular ALGOL-derived programming languages like C, Java, and post-walrus Python have conditional expressions and assignments inside expressions, which means that in this case you don't need to resort to declaring a variable. In Golang and Pascal you can just assign to the named return value rather than declaring it as a normal variable and then explicitly returning it.
The argument against separating expressions from statements is also that it adds redundancy to your programs, as in the above example.
I wrote an essay about this tradeoff in general, not limited to expressions vs. statements, at http://www.paulgraham.com/redund.html http://www.paulgraham.com/redund.html.