Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some snarks are boojums: list of boojums, or is_boojum property on all snarks?

The problem domain features a large population of named snarks. Some of the snarks are boojums.

There are at least two ways to model this:

// as a property: 
    class Snark { 
      string name; 
      bool is_boojum; 
    };  

// as a list:
    class Snark { 
      typedef long Id;
      Id id;
      string name;
    };  

    tree<Snark::Id> boojums;

It seems intuitive that if we determined that snarks come in male and female, we would add a "sex" property to the snark class definition; and if we determined that all but five snarks were vanquished subjects, we would make a list of royals.

Are there principles one can apply, or is it a matter of architectural preference?

like image 664
Thomas L Holaday Avatar asked Dec 22 '22 13:12

Thomas L Holaday


2 Answers

What problem are you trying to solve?

If the purpose of recording the royalty of the snarks is to display a crown on their heads in the GUI, then it makes sense for it to merely be an attribute. (Alternatively, there could be a RoyalSnark subclass, with an overridden Render method.)

If the purpose is to quickly find all the royal snarks, then a list is more appropriate - otherwise you would need to look at every snark and check its attribute.

like image 116
Oddthinking Avatar answered Jan 10 '23 20:01

Oddthinking


I believe that the information entropy associated with the classification can be a guide to which method to use. Low-entropy classifications (i.e. most of the objects have the same value) suggest a list implementation tracking the exceptional cases, while high-entropy classifications (you cannot make any very good predictions about which classification an object will have) suggest a property implementation.

like image 42
chaos Avatar answered Jan 10 '23 19:01

chaos