Many small classes vs. logical (but) intricate inheritance

I’m wondering what is better in terms of good OOP desing, clean code, flexibility and avoiding code smells in the future. Image situation, where you have a lot of very similar objects you need to represent as classes. These classes are without any specific functionality, just data classes and are different just by name (and context) Example:

Class A
{
  String name;
  string description;
}

Class B
{
  String name;
  String count;
  String description;
}

Class C
{
  String name;
  String count;
  String description;
  String imageUrl;
}

Class D
{
  String name;
  String count;
}

Class E
{
  String name;
  String count;
  String imageUrl;
  String age;
}

Would it be better to keep them in separate classes, to get “better” readability, but wit many code repetitions, or would it be better to use inheritance to be more DRY?

By using inheritance, you will end up with something like this, but class names will lost it contextual meaning (because of the is-a, not has-a):

Class A
{
  String name;
  String description;
}

Class B : A
{
  String count;
}

Class C : B
{
  String imageUrl;
}

Class D : C
{
  String age;
}

I know inheritance is not and should not be used for code reuse, but I don’t see any other possible way how to reduce code repetition in such case.

The general rule reads “Prefer delegation over inheritance”, not “avoid all inheritance”. If the objects have a logical relationship and a B can be used wherever an A is expected, it is good practice to use inheritance. However, if the objects just happen to have fields with the same name, and have no domain relationship, do not use inheritance.

Quite often, it pays off to ask the question of “reason of change” in the design process: If class A gets an extra field, would you expect that class B gets it as well? If that’s true, the objects share a relationship, and inheritance is a good idea. If not, suffer the little repetition to keep distinct concepts in distinct code.

5

By using inheritance, you will end up with something like this, but class names will lost it contextual meaning (because of the is-a, not has-a):

Exactly. Look at this, out of context:

Class D : C
{
    String age;
}

What properties does a D have? Can you remember? What if you complicate the hierarchy further:

Class E : B
{
    int numberOfWheels;
}

Class F : E
{
    String favouriteColour;
}

And then what if, horror upon horrors, you want to add an imageUrl field to F but not E? You cannot derive it from C and E.

I’ve seen an actual situation where a lot of different Component types all had a name, an ID and a Description. But this wasn’t by the nature of their being components, it was just a coincidence. Each had an ID because they were stored in a database. The name and description were for display.

Products also had an ID, Name and Description, for similar reasons. But they weren’t components, nor were components products. You would never see the two things together.

But, some developer had read about DRY and decided he’d remove every hint of duplication from the code. So he called everything a product and did away with the need to define those fields. They were defined in Product and everything that needed those fields could derive from Product.

I cannot say this strongly enough: This is not a good idea.

DRY is not about removing the need for common properties. If an object is not a Product, don’t call it a Product. If a Product doesn’t REQUIRE a Description, by the very nature of its being a Product, do not define Description as a Property of Product.

It happened, eventually, that we had Components without descriptions. So we started having null Descriptions in those objects. Not nullable. Null. Always. For any Product of type X … or later Y … and N.

This is an egregious breach of Liskov Substitution Principle.

DRY is about never putting yourself in a position where you might fix a bug in one place and forget to do it somewhere else. This isn’t going to happen because you have two objects with the same property. Never. So don’t worry about it.

If the context of the classes makes it obvious that you should, which will be very rare, then and only then should you consider a common parent class. But, if it’s properties, consider why you’re not using an interface instead.

0

Inheritance is way to achieve polymorphic behavior. If your classes don’t have any behavior, then inheritance is useless. In this case, I would not even consider them objects/classes, but structures instead.

If you want to be able to put different structures in different parts of your behavior, then consider interfaces with get/set methods or properties, like IHasName, IHasAge, etc..
This will make the design much more cleaner and allow better composition of these kind of hiearchies.

Isn’t this what interfaces are for? Unrelated classes with different functions but with a similar property.

IName = Interface(IInterface)
  function Getname : String;
  property Name : String read Getname
end;

Class Dog(InterfacedObject, IName)
private
  FName : String;
  Function GetName : String;
Public
  Name : Read Getname;
end;

Class Movie(InterfacedObject, IName)
private
  FCount;
  FName : String;
  Function GetName : String;
Public
  Name : String Read Getname;
  Count : read FCount; 
end;

You question seems to be framed in the context of a language that does not support multiple inheritance but does not specifically specify this. If you are working with a language that supports multiple inheritance, this becomes trivially easy to solve with only a single level of inheritance.

Here is an example of how this can be solved using multiple inheritance.

trait Nameable { String name; }
trait Describable { String description; }
trait Countable { Integer count; }
trait HasImageUrl { String imageUrl; }
trait Living { String age; }

class A : Nameable, Describable;
class B : Nameable, Describable, Countable;
class C : Nameable, Describable, Countable, HasImageUrl;
class D : Nameable, Countable;
class E : Nameable, Countable, HasImageUrl, Living;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật