in reply to Perl modules hierarchy and composition

G'day tusker,

Welcome to the Monastery.

Using class names like A, B, C and D, doesn't make it easy to visualise your overall class structure.

In general terms, A::B::C doesn't imply that B inherits from A nor that C inherits from A::B; it does, however, imply that B is somehow related to A and C is somehow related to A::B.

Having said that, it often makes sense to use naming to logically separate concerns. For instance, A might compose B::C or B::D while both B::C and B::D inherit from B.

Consider these contrived classes:

Building Building::House Building::Shed Building::Garage Vehicle Vehicle::Car Vehicle::Train Vehicle::Motorcycle Door Door::Hinged Door::Sliding Door::Roller

The various Building::* and Vehicle::* classes could compose zero, one or more Door::* classes.

By sticking with your current class structure, you could end up with a huge number of classes such as this:

Building Building::House Building::House::Door Building::House::Door::Hinged Building::House::Door::Sliding Building::Shed Building::Shed::Door Building::Shed::Door::Hinged ... Vehicle Vehicle::Train Vehicle::Train::Door Vehicle::Train::Door::Hinged Vehicle::Train::Door::Sliding ...

This quickly starts to become unmanageable. Building::House::Door, Building::Shed::Door and Vehicle::Train::Door will probably have duplicated code; as will Building::House::Door::Hinged, Building::Shed::Door::Hinged, Vehicle::Train::Door::Hinged, and so on. Throw in a variety of Lock, Handle, etc. subclasses and you're facing a maintenance nightmare.

I'd recommend spending a bit more time on your class design before writing any code. Class names with duplicated portions will probably equate to duplicated code which you should endeavour to avoid.

[A note on nomenclature. You used the term "composition" in your question and I continued its use; however, in the strictest sense, that may be inappropriate in some cases. There are other types of relationships (e.g. "aggregation", "association", "delegation"): at times, it's important to be specific about which you're referring to. You may already know about these; if not, most introductory OO texts will describe them.]

— Ken

Replies are listed 'Best First'.
Re^2: Perl modules hierarchy and composition
by Anonymous Monk on Jan 07, 2017 at 10:16 UTC
    Thanks for your answer! It's good to read that module hierarchy does not have to be pure inheritance. Indeed I try to avoid having excessive depth in my modules hierarchy. I use it only to create compartments for autonomous code. I don't think I would end up with the last situation you described, as it would indeed lead to a high quantity of redundant code. I indeed know about the OO nomenclature and the objects I'm dealing with constitute really a composition. Thanks for the reminder!