Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!caen!uwm.edu!linac!att!pacbell.com!tandem!netcomsv!jls From: jls@netcom.COM (Jim Showalter) Newsgroups: comp.object Subject: Re: Functions without side effects (was Old confusion) Keywords: functionality Message-ID: <1991Jun8.203821.9108@netcom.COM> Date: 8 Jun 91 20:38:21 GMT References: <1991May30.141218.3446@mstr.hgc.edu> <327@alfrat.uucp> <1991Jun3.145924.28406@mstr.hgc.edu> <130242@tut.cis.ohio-state.edu> <4888@osc.COM> Organization: Netcom - Online Communication Services UNIX System {408 241-9760 guest} Lines: 47 Good points! I have a few comments: >`widget.get_part()' is a method call which returns the part, That's fine if this is a procedural method. However, if you want to get the part from the widget and assign it into something, you wind up with: the_part = widget.get_part(); which doesn't seem to me to be as clear (English-like) as: the_part = widget.part(); An alternative I can live with is to write get_part to return its value as an argument: widget.get_part(the_part); but this still doesn't seem as clear. >Sometimes it may be a good idea to provide a method which combines the >functionality of two simpler methods. But there has to be a good reason, more >than just the fact that the original two methods may be called sequentially. >For example, there may be some good reason you can make the combined method >significantly more efficient than calling the original methods separately. Agreed. Generally the criterion I tell my students to use is whether or not the method they're providing is something that depends on visibility to private data for efficiency. If so, then a client can never hope to implement the operation as efficently from outside the class as from inside it. The example I usually give is something to count the number of elements in a list. From outside the class the only way to do this (if the class has been properly abstracted and the data properly encapsulated) is to set up a local counter and then perform N swap head/tail operations until the list has been counted. That this is very inefficient, plus which it modifies the state of the list (so you need to keep a duplicate list around to get back to the starting point), etc. Clearly a Count method that can simply walk a pointer to list elements down the list (because it has visibility to the private data) is much superior and should be supplied by the class (for even higher efficiency a count field can be part of the data for the list itself and this can be incremented/decremented as elements are added to/deleted from the list). -- **************** JIM SHOWALTER, jls@netcom.com, (408) 243-0630 **************** *Proven solutions to software problems. Consulting and training on all aspects* *of software development. Management/process/methodology. Architecture/design/* *reuse. Quality/productivity. Risk reduction. EFFECTIVE OO usage. Ada/C++. *