Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!tut.cis.ohio-state.edu!elephant.cis.ohio-state.edu!weide From: weide@elephant.cis.ohio-state.edu (Bruce Weide) Newsgroups: comp.lang.eiffel Subject: Re: Functions without side effects (was Old confusion) Message-ID: <131640@tut.cis.ohio-state.edu> Date: 7 Jun 91 18:51:56 GMT References: <1991Jun3.145924.28406@mstr.hgc.edu> <130242@tut.cis.ohio-state.edu> <1177@tetrauk.UUCP> Sender: news@tut.cis.ohio-state.edu Organization: The Ohio State University, Department of Computer and Information Science Lines: 44 In article <1177@tetrauk.UUCP> rick@tetrauk.UUCP (Rick Jones) writes: > >You are presuming a system with value semantics. OO systems often work >very effectively with reference semantics (in Eiffel reference semantics are >the default unless you specify "expanded" variables or classes). In this case >the Top operation simply returns a pointer to the object on the top of the >stack, which is highly efficient. > >In fact I would suggest that a system in which large objects are used is best >handled using reference semantics, otherwise there will be a lot of >inefficiencies elsewhere resulting from copying. The programmer must of course >be aware of this, and use "clone" operations where required. > For discussion of a paradigm of programming that uses value semantics without the inefficiency of copying, see IEEE Transactions on Software Engineering, May 1991, which should be on your local newsstand very soon :-). The key idea is to replace most copying with swapping, since most uses of copying aren't really necessary anyway. Swapping has two big advantages: it can be implemented efficiently by the language system (by swapping references) even while the programmer sees only value semantics; and it never introduces aliasing. This, as Bill Ogden has pointed out, is very important if you want to produce programs about which it is easy to reason. Doing things like "copying a pointer to the object on the top of the stack" is just asking for trouble, and this has been well-known and repeatedly pointed out by such people as Hoare and Kieburtz for nearly 20 years. As Bill also was saying, copying references is itself not all bad. It is great for efficiency. The problems arise when aliasing produces surprising behavioral side-effects because variables/objects that aren't even mentioned in a piece of code get changed behind your back. This is related to the question of whether functions should have side effects. For example, I'm surprised that some people seem to feel functions should not have side effects ON THEIR ARGUMENTS, but seem perfectly willing to settle for programs with arbitrary aliasing where both functions and procedures may have side effects ON VIRTUALLY ANY OTHER VARIABLES/OBJECTS, even ones not explicitly mentioned in the code in question. This is exactly the situation that arises if you define "Top" as copying a pointer to the object on top of the stack. -Bruce