Class Hierarchy design problems...

M

Mark Broadbent

I agree, I'd thought about the possibility of throwing a custom Exception,
and it is probably the most sensible way to go with this design.

Cheers,

Mark.


Bill Butler said:
So really it makes sense what is happening because obviously a null
reference cannot have a Remove method, but the compiler does not know a
Remove method is being called on a null reference till runtime.

You really need to decide how to handle this in a way that makes the most
sense for your application.
You could check for a null Item before calling Remove()
You could add a try block

Returning an empty Item is probably not a good idea unless you check for
it.
Other wise you can have this:
Item item = basket.Items["spannerintheworks"]; //returns the empty
item;
Func(item); //use it like it was perfectly good
This is probably a bad idea.

perhaps an exception in the case of
basket.Items["spannerintheworks"].Remove();
is a good idea, since you don't actually have the item object.

If you don't actually have the object
basket.Items.Remove(["spannerintheworks");
makes more sense AND it won't throw an exception.
Thanks for the time you took on this code and your comments.

No problem

Bill
 
J

Joanna Carter \(TeamB\)

For instance why, if we have a reference to an item should we then be forced
to use it's container to do the lookup and remove for said item? Surely we
should just be able to tell our item to "go home" to the great garbage
collector in the sky directly to it's face (it then notifies it's container
that it wishes to leave)?

This all comes down to the concept of Responsibility Driven Design.

Whose responsibility is the management of items in a collection, is it the
item or the collection ?

If you ask for an item from the collection and it returns null, then how do
you call the Remove() method ?
Taking the analogy of people in a room. I have to ask myself why couldn't a
person initiate leaving a room themselves rather than a person being told to
leave a room from the loudspeaker on the wall of the room? In this instance
I see scope to having both objects being able to initiate the action (even
though Room is a container of people).

Because most items in a system do not have the same autonomy that a human
being does. Apples are really not known for their intellectual independence
:)

Business systems are usually modelled around the jobs that people used to do
manipulating documents and goods; an Invoice cannot remove itself from a
filing cabinet, it has to be removed by an Accounts clerk.
The problem (regardless of improper design or not) being experienced here is
simply and purely being caused by the use of the indexer and it's return of
null (as desired) if the item specified in the indexer has not been found
(as I shall demonstate in my reply to Bill's example).

I think you are trying to justify a design on the basis of a language
feature :)

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top