[ENH] Empty return expression
Bert Freudenberg
bert at isgnw.CS.Uni-Magdeburg.De
Mon Sep 27 18:17:55 UTC 1999
On Mon, 27 Sep 1999, Jarvis, Robert P. wrote:
> I don't like this because IMO it obfuscates what's going on without
> adding value.
If you do not end a method with ^something it returns self. But it's good
programming style to reveal this intention. If you have a method like
asSomething in class Something you'd write ^self to express "I return
self, someone else would return something different". OTOH, if a class
chooses to do nothing in a doSomething method it would just be empty.
(you can see lots of examples for this by evaluating
"Smalltalk browseAllSelect: [:m | m isReturnSelf]")
But if I want to return from within a "void" method I'm *forced* to give a
return value (usually self). You can look at methods explicitly returning
self in the middle by evaluating
| returnSelf |
Smalltalk browseAllSelect: [:m | returnSelf _ false.
(InstructionStream on: m) scanFor: [:instr | returnSelf
ifTrue: [true]
ifFalse: [returnSelf _ instr == 120. false]]]
These are almost a thousand methods most of which would benefit from an
empty return.
> Besides, if the method is going to answer 'nothing returned' shouldn't
> it answer nil instead of self?
Using nil for that purpose is much less common - something like 50 methods
(not counting primitive methods). And it wouldn't match the semantics of
a missing return statement.
I know it's always bad to change a language "just for fun" but I thought
the idea was too good to not be discussed at least ...
/bert
More information about the Squeak-dev
mailing list
|