Re: Newbie Question: implementing a stack
Re: Newbie Question: implementing a stack
- Subject: Re: Newbie Question: implementing a stack
- From: Graham Cox <email@hidden>
- Date: Sat, 20 Dec 2008 16:52:59 +1100
On 20 Dec 2008, at 3:15 pm, Steve Wetzel wrote:
Regarding memory management - does it make more sense to copy the
object to be pushed from within the Stack object rather then copying
it externally before the push call? I am thinking that it does
because then that object is encapsulated which is how a stack should
really work. If I do this I realize I will need to implement a copy
method within any object that I want to place on the stack if it
does not already have one.
My own view is that the stack object shouldn't copy the object.
In your particular case you seem to want to push a copy of the object
onto the stack, but in the general case, you wouldn't expect the stack
to do this. In the interest of developing reusable code where
possible, your stack object would be more reusable if it was dumber -
i.e. it retained its objects and didn't copy them. If I had a stack
object "black box", pushed an object then later when I popped it I got
back a different object (albeit one that was very similar) I'd be
surprised by that, especially if I'd mutated it on purpose - all my
changes would have evaporated. In your special case perform the copy
outside the stack object, thus keeping the special case out of an
otherwise reusable class.
That said, over-generalising code prematurely is almost as bad as
optimising code prematurely, so if you think you'll never have a use
for a stack anywhere else except this one case, then by all means do
the copy internally.
hth,
Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden