From: Ron Jeffries Date: 2002-03-16T02:56:25+09:00 Subject: Re: style question On Fri, 15 Mar 2002 16:35:20 GMT, Dave Thomas wrote: >Ron Jeffries writes: > >> How do we feel about methods that return more than one result? It it OK? Is it a >> code smell indicating that some kind of object is trying to be born? > >Sometimes the return value is a true tuple, so a tuple is appropriate. > >Sometimes it's a set of values that meaningfully belong together, in >which case a class or a Struct seems like a good idea. > >Sometimes its a status and a value. For me, this is always the hardest >call. But when I find it happening in unencapsulated methods, I tend >to take it as an indication that I really should be wrapping the method >in a class and having the class keep the state I need. OK, here's my case. A "Set" has a couple of instance variables that let it do what it does: a RecordMap, describing the records, and a SetData, containing the bytes. A given set operation involves two bits: computing the RecordMap for the new set, and thrashing the bytes around to create it. These seem both to be functions that belong either in the RecordMap or in the SetData. Creation of the map must precede creating the data, and creating the data needs the map. So the code looks like this: class Set def createNewSet copymap = @recordmap.select_map(field_list) new_data = copymap.move_records(@data) new_map = copymap.compress return Set.mapped_data(new_map, new_data) end end I'm wanting to fold the creation of the copymap, the moving of the data, and the creation of the new map, all into one, something like def createNewSet new_map, new_data = @recordmap.move_fields(field_list, @data) return Set.mapped_data(new_map, new_data) end which, if I think it's communicative enough, could become return Set.mapped_data( * @recordmap.move_fields(field_list, @data) ) Either way, to my jaded eye, it's a bit iffy. But the RecordMap really does have the responsibility to do both things and the user needs both things. (I don't let the RecordMap create the Set directly only because a Set has-a RecordMap and I don't like RecordMap knowing what kind of class owns it.) I guess I'll just do it and see how it looks. My question is whether the style of returning a little vector of related stuff is OK. I agree with Andy that indexing into the array is a smell big time. Thanks, Ronald E Jeffries http://www.XProgramming.com http://www.objectmentor.com I'm giving the best advice I have. You get to decide whether it's true for you.