Question

I'm currently designing the classes for an application I'm writing for my coursework, and I have two classes that sound as if they should be a base-derived class pair, and do indeed share two member variables, and my problem is that they each have seven member variables and no operations.

The reason for the structure of these classes is that I am building a RSS reader and I intend to have these two classes hold data on the feeds. The first one will hold the data on the feed itself, for example the source url, the location of the rss.xml file on local storage, when the feed was last updated, etc. The second will hold information on the articles contained within the feed such as the publication date/time and an integer index based on the publication date that will be used to chronologically sort the articles.

class feed
{
    string title;
    string description;
    string feed_url;
    string local_location;
    string channel;
    bool feed_is_changed; // This is a flag that will be raised and lowered
      // when the feeds are being refreshed
    double last_updated; // The last update date/time will be converted to a
      //standardised double value
}

class feed_item
{
    string title;
    string description;
    double pub_time;
    double pub_time_in_sec; // I'm separating the seconds so they can be used
      // for a 'sub-index' when there are multiple feeds with the same pubtime
      // (there are restrictions on the data types we are allowed to use
      // (concocting work-arounds will aid in understanding, etc))
    double pub_date;
    int pub_year;
    int order_in_list; // The index that will be calculated from pub_time,
      // pub_date, etc
}

The above code is not complete, I'm currently only identifying variables and functions, and the private/public bits will come once they're finalised. As you can see from the above code, the only two variables that are being shared are title and description.

I'm not sure if it's worth making them an entity-base pair and just deactivating the five irrelevant variables, if it's more efficient to just make them completely separate classes, or if this is an entirely situational concern, and that it can be argued either way. My concerns are that the code may become difficult to both maintain and scale, but that there may be execution overhead inherent in one method or the other. Any thoughts and advice on this would be most appreciated.

Was it helpful?

Solution

A feed_item isn't a feed, so it fails the Liskov substitution principle and shouldn't be a subclass. I should check your ears — this pair of classes absolutely doesn't sound like they should be subclasses.

Occasionally (very, very occasionally) implementation inheritance is a good idea, but it's usually better done by extracting shared parts into a separate class and using it in both implementations. Here, it's absolutely a terrible idea — there's no great sharing of code, so the benefits are at best vague. Keep your code simple!

OTHER TIPS

Just one derived class? Then almost certainly inheritance is the wrong design.

Inheritance is limiting, and those limits often don't appear until later making the decision even more expensive.

My rule of thumb is to avoid inheritance unless and until I can make a clear and compelling case to use it.

If you really wanted a base class:

struct NamedItem {  // or maybe just "Item"
  string title;
  string description;
};

struct Feed : NamedItem {/*...*/};
struct FeedItem : NamedItem {/*...*/};

Or, usually preferred and a better fit in this case, use containment:

struct ItemInfo {
  string title;
  string description;
};

struct Feed {
  ItemInfo info;
  //...
};
struct FeedItem {
  ItemInfo info;
  //...
};

In particular, if you have no idea how you'll use a "NamedItem" without knowing the most derived type, it doesn't make sense to use inheritance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top