Hierarchy is not a property

There are many cases where an item contains a property that defines its location inside a hierarchy:

This is an antipattern. The location in a hierarchy is not a property of the contained object. Instead, the location in a hierarchy is simply defined by the location in a hierarchy.

If the parent-child relationship is defined only by an attribute in the child object, this is not a problem. One-to-many relationships in SQL are typically defined like this. (Technically references are often stored in both ends of a relationship, especially in volatile run-time objects, but this is just an optimization, hidden under the hood.)

The problem in all the examples above is that the hierarchy is defined both by an attribute of the child and the location of the child in relation to other objects. This redundancy causes problems when the structure needs to be changed: the object needs to be both moved and changed.

The article language I wrote in ngrease is an abstraction of HTML and Latex, but in it sections are defined as a nested hierarchy. This makes writing and restructuring painless, especially if each section is defined in a separate file, included by its parent: Only one layer thinking is needed when writing a section, and the article can be restructured simply by moving section references (the "include" expressions), without any need to touch the referenced section definitions.

Originally published on 2008-02-11 at http://www.jroller.com/wipu/entry/hierarchy_is_not_a_property under category Software Architecture