Namespace and class with the same name?

前端 未结 9 1730
终归单人心
终归单人心 2020-11-29 03:38

I\'m organizing a library project and I have a central manager class named Scenegraph and a whole bunch of other classes that live in the Scenegraph namespace.<

9条回答
  •  无人及你
    2020-11-29 04:18

    As others have said, it's a good practice to avoid naming a class the same as its namespace.

    Here are some additional naming suggestions from an answer by svick to a related question "Same class and namespace name" on the Software Engineering Stack Exchange:

    You're right that you shouldn't name the namespace the same as a type it contains. I think there are several approaches you can use:

    • Pluralize: Model.DataSources.DataSource

    This works especially well if the primary purpose of the namespace is to contain types that inherit from the same base type or implement the same interface.

    • Shorten: Model.QueryStorage

    If a namespace contains only a small number of types, maybe you don't need that namespace at all.

    • Make enterprisey: Model.ProjectSystem.Project

    This can work especially for features that are important part of your product, so they deserve their own name.

    (Note that the above answer uses Model.DataSource.DataSource, Model.QueryStorage.QueryStorage., and Model.Project.Project as examples rather than MyLib.Scenegraph.Scenegraph.)

    (I also found the other naming suggestions in the other answers here helpful.)

提交回复
热议问题