[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: expressions vs. statements
> > > If you want it to be
> > > the value of the block you should say so.
> > > (a) Why?
> >Because it could be that it just happened to be the last line in the
> >block. It's ambiguous whether it just ended up at the end or was
> >deliberately put there to trigger a return value.
> And the consequence is...?
The consequence is that your function returns some value, even if you
didn't really intend for it to do so. This means that whenever you
change the function, you need to either maintain the same return value
(possibly leading to wordier code, inefficiency, or in the worst case,
the inability to make code correct) or verify that nobody is using the
return value. This latter is somewhat of a pain when you're working
on a medium-sized or large program on your own, and much worse when
you're working on a library used by people you don't know.
This is obviously only meaningful if a significant proportion of the
functions you write have side effects. If your function doesn't have
any side effects, it needs to return a value to be useful, and
requiring an explicit 'return' is just pointless overhead.
> Should it be an error to call a value-returning function in a language
> like Java, without assigning the value to a variable?
Some people think so, but that's a different issue.
> >I just prefer to separate the two issues to avoid an "accidental" return
> Since any type-safe language will catch the "accidents" at compile time,
Please separate type-safety from static typing. Any statically-typed
language will catch some type errors at compile time. Most type-safe
languages can't catch type errors at compile time. (Statically-typed
languages have to implement onerous restrictions.)
But the kind of accidents we're talking about are not type errors
unless you have to declare types for your functions or it's illegal
(as suggested above) to produce a value and then ignore it. The kind
of accidents we're talking about are returning a value when you meant
to return no value.
I don't think this issue is terribly important in language design, but
it's obvious to me that the right answer is that most blocks of
sequential statements shouldn't return a meaningful value.