Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!rpi!uupsi!mstr!mstr!jcm From: jcm@mstr.hgc.edu (James McKim) Newsgroups: comp.lang.eiffel Subject: Functions without side effects (was Old confusion) Message-ID: <1991May30.141218.3446@mstr.hgc.edu> Date: 30 May 91 14:12:18 GMT Sender: Usenet@mstr.hgc.edu (Action News Central) Reply-To: jcm@mstr.hgc.edu (James McKim) Organization: The Hartford Graduate Center, Hartford CT. Lines: 79 Nntp-Posting-Host: sc3.hgc.edu Sorry if the headers you receive look strange. At the moment the only way I can post news is by follow-ups, and in this case I'm trying to follow up an expired article! In an earlier post Craig Chambers writes >2. Do you actually believe that functions should be side-effect-free? Yup. At least as far as the abstract state of the class is concerned. This is really just an application of separation of concerns. Procedures change the state, functions query the state. >My view is that side-effecting procedures (even of the abstract state) >that return a result are too useful to forgo. For example, when I >call Stack.Pop I want to get back the element popped off the stack; I >don't want to have to call Stack.Top, save the result somewhere, and >then call Stack.Pop to remove it from the stack. Well, perhaps a couple of examples might convince you that it's at least as useful to separate Pop and Top as to combine them. Suppose I have stack.pop which removes the top item and stack.top which returns the top item. Meanwhile you have stack.pop_top which does both. Now suppose I want to print the top item on the stack if it's bigger than 10. I write if stack.top > 10 then print stack.top end You write temp := stack.pop_top if temp > 10 then print temp else stack.push( temp ) end That's assuming you don't fall into the following trap if stack.pop_top > 10 then print stack.pop_top end Now suppose I want to empty the stack. The body of my loop consists of stack.pop The body of yours will be temp := stack.pop_top unless of course you're working in C or some other assembly language.:-) The point is pop and top are independently useful and so deserve to be separated. And this independent usefulness comes about precisely because one changes the state and the other merely queries. > Also, I believe that >somewhere in OOSC (I can't find it now) is described a technique >whereby instead of writing a side-effecting function (a procedure that >returns a result) the programmer writes a procedure that stores its >result in some instance variable which can then be accessed by a >corresponding function. This approach seems silly; it's more verbose >for the programmer (both the class implementor and the client) and >less efficient since I assume an extra word of storage needs to be >allocated in every such object. I think you're probably thinking of the matrix equation example on pp. 201-2. The technique you described is used to prevent the need for executing essentially the same complicated algorithm twice. The tradeoff is substantial runtime savings for one word of storage. Anyway, that's the point of view of at least one of the 'Eiffel converted'. What do you think? -- 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.