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: <1991Jun13.135213.28643@mstr.hgc.edu> Date: 13 Jun 91 13:52:13 GMT References: <1991May30.141218.3446@mstr.hgc.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: 61 Nntp-Posting-Host: sc3.hgc.edu In article kers@hplb.hpl.hp.com (Chris Dollin) writes: > > >My vision of Stack is one in which ``every time you need to make a decision >based on the current top'' is a non-issue; you stuff things on the stack, later >you get them back. Why would you want to ``make a decision'' based on the ``top >element''? (All the code I've done with stacks just pops the element when it >wants it. Repeated access to that element - while it's still on the stack - >just doesn't seem to be one of the desired properties. No, this isn't an >artifact of using imperative pop!) > >Perhaps we're talking about two different data-types both called ``Stack''? >-- > >Regards, Kers. | "You're better off not dreaming of the things to come; >Caravan: | Dreams are always ending far too soon." Well, this thread began as a question of whether functions should have side effects in general. The stack has just been a convenient example. I take the position that in general functions should not have side effects unless there is a _compelling_ reason (the most common being efficiency) for it. Thus I argue for a side effect free 'top' and a return free, parameter free 'pop'. Others have argued that a side effect free 'top' is good, but let 'pop' return the item it popped as well. (I call this 'pop_top'). In fact some have argued that procedures should return values just like functions do whenever there is some reason to believe the value may be useful. Still others have agreed in principle that side effect free functions are a good thing, but cite exceptions to the rule. And finally, one person has argued that 'pop_top' should be the default because it saves replicating the top object on the stack, which may well be an expensive operation. This has resulted in a spinoff thread on the merits of reference semantics vs value semantics. As to your specific question, examples have been posted that show some value in keeping pop and top separate and I could post more. But let me try appealing to authority instead. Consider D.L. Parnas, "A Technique for Software Module Specification with Examples" CACM, May, 1972. Daniel Hoffman, "On Criteria for Module Interfaces", IEEE TSE, May, 1990 Grady Booch, OOD with Applications, Benjamin Cummings, 1991 (p. 82) and of course Bertrand Meyer, OOSC, Prentice-Hall, 1988 All but Booch specifically discuss stacks. Booch uses a queue. Food for thought. -- 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.