Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Product trademark name begs for class/namespace with same name

Tags:

c#

.net

Author post-edit: Chosen solution

(Original question remains below this box)

SUMMARY: You SHOULD NOT name a class the same as its namespace. Therefore should a product name be used for the namespace or the main class?

Chosen solution: I decided to apply the product name to the namespace and add a suffix to the main class name (e.g., __Module).

Rationale: Visual Studio defaults to using the project name for the namespace, assembly name, as well as the actual deliverable .exe or .dll -- These are the most visible items so I think it makes sense to name the namespace after my product name, then do as Jon Skeet recommends in his answer and name the main class as ___Main or __Program or __Module. No one right answer I suppose.


Original question:

I COMPLETELY GET IT-- DON'T NAME A CLASS THE SAME AS ITS NAMESPACE!

This is almost a duplicate question... except I've spent hours reading articles (see below) and can't find or think of a solution that seems right.

Say I have a product called the ACME Foobarinator. It has a couple related types (e.g., settings, enums) but not enough to call for any sort of namespace hierarchy.

It makes sense to create a single product namespace and throw everything into it:

namespace Acme.Web.Foobarinator
{
    public class Foobarinator { } // BAD! Same name as namespace!
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

BAD!!! Both namespace and class name are the same!!

But, I'd also like the main class to be Foobarinator because it's the trademark product name and I'd like consumers to use it by trademark name: var fb = new Foobarinator();

Option 1: Eliminate the product namespace, promoting all types to the parent namespace. But this pollutes the parent namespace with types that are specific to the product (and not necessarily all public types). As the product evolves, the pollution stands to increase!

namespace Acme.Web
{
    public class Foobarinator { }
    public class FoobarinatorInfo { }
    public enum FoobarinatorMode { Disabled, Enabled }
    // More pollution in future...
}

Option 2: Add needless suffix to the main class. But this obfuscates the product trademark name!

namespace Acme.Web.Foobarinator
{
    public class FoobarinatorMain { } // Not the name of the product!
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

Option 3: Use child classes/types for the related types. However, encapsulating everything in one class eventually violates separation of concerns as the product evolves:

namespace Acme.Web
{
    public class Foobarinator
    {
        public class FoobarinatorInfo { }
        public enum Mode { Disabled, Enabled }
    }
}

YOUR THOUGHTS?

Where do I compromise?

Related questions and articles:

How to avoid having the same name for a class and it's namespace, such as Technology.Technology?

Should a class have the same name as the namespace?

Namespace with same name as a class name

Should a class have the same name as the namespace?

http://blogs.msdn.com/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx

like image 867
Kevin P. Rice Avatar asked Oct 08 '22 23:10

Kevin P. Rice


1 Answers

I would use:

namespace Acme.Web.Foobarinator
{
    public class Program { } // The entry point
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

or even:

namespace Acme.Web.Foobarinator
{
    public class EntryPoint { }
    public class FoobarinatorInfo { }
    public enum Mode { Disabled, Enabled }
}

The first is the Visual Studio default (at least for some project types) and the second makes it crystal clear what the role of the class is.

like image 192
Jon Skeet Avatar answered Oct 23 '22 05:10

Jon Skeet