[ENH] Empty return expression

Doug Way dway at mat.net
Mon Sep 27 17:05:05 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.

On the contrary, I'd say it's more clear as to the intention of the
return, which does add value.

Basically, it says that you want to exit the method at a certain point,
and that there is no return value to be used.

It's the equivalent of an implicit return, except earlier in the method.
Kent Beck and others recommend using implicit returns when the method is
not intending to return anything, because it makes the intention more
clear (and I agree).  (In other words, with an implicit return, the
calling method should not use the returned value.)

So, this seems like a good equivalent of an implicit return, except that
it is done earlier in the method, because you need to break out of the
method.  Returning "^self" is misleading, because it implies that you
might want to specifically return "self" to be used by the caller.  (True,
most Smalltalkers recognize "^self" as a generic return, but it's not
always that way.)

I have to admit I thought it looked a little funny for a minute, but it
seems like a good idea.  (unless there's some other potential problem I'm
missing)

- Doug Way
  EAI/Transom Technologies, Ann Arbor, MI
  http://www.transom.com
  dway at mat.net, @eai.com



> If you want to answer self in this situation, then type
> 
> 	^self
> 
> This requires four keystrokes, and it's clear.  Besides, if the method is
> going to answer 'nothing returned' shouldn't it answer nil instead of self?
> 
> Bob Jarvis
> The Timken Company
> 
> > -----Original Message-----
> > From:	Bert Freudenberg [SMTP:bert at isgnw.CS.Uni-Magdeburg.De]
> > Sent:	Monday, September 27, 1999 4:36 AM
> > To:	squeak at cs.uiuc.edu
> > Subject:	[ENH] Empty return expression
> > 
> > Parser patch to allow empty returns that indicate 'nothing returned' like 
> > 	foo ifTrue: [self doSomething. ^]
> > instead of the currently often (mis-)used
> > 	foo ifTrue: [^self doSomething]
> > as short form of
> > 	foo ifTrue: [self doSomething. ^self]
> > 
> > Discussion: 
> > This syntax matches the implicit return of self at the end of
> > a method. It makes the "void" return type of a method explicit, which is
> > good for self-documenting code. It's backwards compatible. It's a minor
> > change in the parser. So why not?
> > 
> > It's not compatible to other Smalltalks - but then, the brace constructs
> > are even more different.
> > 
> >   /bert << File:  >> 
> 
> 





More information about the Squeak-dev mailing list