Namespace by topic or by purpose? [closed]

The project I’m working on has quite a few classes. These classes can be grouped in two different ways. I’m trying to decide which grouping is best as for namespacing. Currently I a set of user facing classes that look like:

  • JamJar
  • HoneyJar
  • PeanutButterJar
  • etc…

These are all similar in that they all inherit from SpreadableJar, each spreadable prefix could be considered a ‘topic’ within in the project.

I also have a set of objects relating to these topics, each topic usually has a set of constant definitions and message classes associated with it so I end up with a bundle of files such as:

  • JamDefinitions
  • PeanutButterSpreadMessage
  • HoneyDefinitions
  • JamRemoveMessage
  • etc…

I can think of two ways to organise these files:

  1. By Topic
    First level is namespace, second is class/file name.

    • Jam
      • Jar
      • Definitions
      • RemoveMessage
      • etc…
    • Honey
      • Jar
      • Definitions
      • etc…
    • PeanutButter
      • Jar
      • Definitions
      • SpreadMessage
      • etc…

This seems tidy (removes duplicated names) and is useful to the user of the library but means that all the Jar files look the same in the project structure, and each Jar class no longer has a descriptive name. It also means that related objects are grouped together so they will not need to scope into each others namespaces

  1. By Purpose

    • Jars
      • JamJar
      • HoneyJar
      • PeanutButterJar
    • Definitions
      • JamDefinitions
      • HoneyDefinitions
      • PeanutButterDefinitions
    • Messages
      • JamRemoveMessage
      • etc…

This way is clear to the user if they know that they need to use Jars (the rest of the objects won’t be needed by external users) and it separates the large number of messages and the separate definitions files from the main functionality, but now the Jars will need to scope into the other namespaces for their own definitions and messages and the file naming becomes more awkward. The ‘Jar’ from JamJar can’t really be removed because the class is not just ‘Jam’.

What is the best approach here? Am I missing some obvious solution or error in my structure?

2

Which of the two is more likely:

  1. Somebody is working with a jam jar, and while doing so, they want to look at a jam definition
  2. Somebody is working with a jam jar, and while doing so, they want to look at a honey jar?

You’re best placed to answer this, given your knowledge of the project, but in general I would say that the first of these two seems far more common. When working on jam-related functionality, your work may well span several interdependent classes. It seems much less likely that you’re going to want to work on a lot of classes for unrelated functionality/features just because the purpose they serve is similar (though it may occasionally come up, e.g. when dealing with cross-cutting concerns).

Given this, I’d say the top level namespace should be topic, allowing easy grouping of related code.


As for:

(removes duplicated names) … but means that all the Jar files look the same in the project structure, and each Jar class no longer has a descriptive name.

This is a trade-off you’re choosing to inflict on yourself. There’s nothing stopping you from having Jam.JamJar, for example.

While I can see the argument for not wanting to duplicate names like that, overall removing the Jam part from the name seems like a poor trade-off. It doesn’t play well with namespace imports/usings at all. If you import a single namespace, your classes end up with less descriptive names. If you import multiple namespaces, you end up with ambiguities which are annoying to resolve.

You could perhaps import the base namespace then do Jam.Jar, etc., but this is atypical, people won’t really be used to doing it, and you may suddenly find yourself writing a new class that doesn’t fit well into this more rigid naming scheme.

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