Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!rice!uupsi!mstr!mstr!jcm From: jcm@mstr.hgc.edu (James McKim) Newsgroups: comp.lang.eiffel Subject: Re: Functions without side effects (was Old confusion) Message-ID: <1991May31.143320.1861@mstr.hgc.edu> Date: 31 May 91 14:33:20 GMT References: <1991May30.141218.3446@mstr.hgc.edu> <1991May30.195249.16103@leland.Stanford.EDU> Sender: Usenet@mstr.hgc.edu (Action News Central) Reply-To: jcm@mstr.hgc.edu (James McKim) Organization: The Hartford Graduate Center, Hartford CT. Lines: 43 Nntp-Posting-Host: sc3.hgc.edu In article <1991May30.195249.16103@leland.Stanford.EDU> craig@self.stanford.edu writes: >In article <1991May30.141218.3446@mstr.hgc.edu>, jcm@mstr.hgc.edu (James McKim) writes: >|> The point is pop and top are independently useful and so deserve to be separated. > >I agree; I never meant to suggest that Top shouldn't exist. > >|> And this independent usefulness comes about precisely because one changes >|> the state and the other merely queries. > >No, my Stack class would have a Top method that returned the top of >the stack and a Pop method that both removed the top element from the >stack *and returned this element to me*. Since I don't follow any >dogma stating that procedures should not return values, I make my >procedures be as helpful as possible, in this case by also returning >what the top of the stack was. > >-- Craig Chambers Well, I'm not sure how to define "helpful". As a user of a class I find it helpful to be able to assume functions have no side effects. I don't find it helpful to be in a situation where features A and B return the same value but B has a side effect (or is it A?). As the implementer/maintainer/designer of a class I find it helpful to be able to separate the concerns of changing state and reading state. A feature that does both just has to be more complex than a feature that does just one. (At least I can't think of a counterexample) On the other hand, if there's a really compelling reason, such as efficiency, I'll bite the bullet and design a procedure that returns a value. (Funny, the phrase 'procedure that returns a value' sounds so much better than 'function with side effects' :-)) So you lean toward adding return values to procedures whenever you think it makes sense and I lean, heavily, toward functions with no side effects. -- Jim *------------------------------------------------------------------------------* Jim McKim (203)-548-2458 | _Give_ people fish and they eat for a day. Internet: jcm@mstr.hgc.edu | _Teach_ people to fish and they eat for a lifetime.