[java-dev] Re: Re: perils of referencing
jbmaxwell
jbmaxwell at rubato-music.com
Thu Feb 7 18:03:35 MST 2008
- Previous message: [java-dev] Re: perils of referencing
- Next message: [java-dev] perils of referencing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Okay, sounds good. I'll try to track down the book. It's interesting that the callee would be a better place for the copy. I would have thought that such an accessor would be best left really simple in the callee. But it does make sense to do the copying there, as it is "safer", preventing callers from mucking about with the data - as you pointed out (and as I experienced!). I suppose I could have both the simple accessor getThings() and a "safer" one, getCopyOfThings(). But maybe there's not much point... Anyway, good to know... I really appreciate when a post helps me *think* about something from a different angle. It's nice to be given some insight into the reason for a particular practice, not just a "do this" reply - though I realize it takes more time to explain. So, thanks for that. cheers, J. Quote: maxmspjit wrote on Thu, 07 February 2008 23:27 ---------------------------------------------------- > You should grab Effective Java book that Oliver mentioned. It has some > good tips in this area, including defensive copying. Its a good habit > to let the callee do the copying, as you can't always control who the > caller is (for example if you're writing a library that provides your > Things to third party clients, they may accidentally do something > malicious to it (as you have perhaps discovered)). > > Worry about efficiency when it becomes a problem, make sure it's > correct first =) > > r. > > On 7-Feb-08, at 1342, jbmaxwell wrote: > > > > > Oh yeah, jeepers... You're right. A deep copy would copy all the > > Things in the ArrayList, as well, wouldn't it! I just want a copy of > > the ArrayList itself. Good point. > > > > I don't know if I'll change my accessor method, but I get the idea. > > > > Is it possible to do something like: > > > > ArrayList theseThings = bigThing.getThings().clone(); > > > > That would leave my accessor method getThings() untouched, and a bit > > more efficient for most situations. > > > > J. > > > > -------------- > > > > Thanks Ollie. > > > > It does make sense to me, actually, but I like to get confirmation > > on things before I commit them to the "learned" pile... Passing > > references around is generally really handy, so I suppose it's not > > fair of me to complain about it now! ;-) > > > > cheers. > > > > > > ----------------------------------------------------
- Previous message: [java-dev] Re: perils of referencing
- Next message: [java-dev] perils of referencing
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
