views:

157

answers:

7

This situation probably is not entirely uncommon to some of you: you have some functionality to put in a class but the perfect name (*) for that class is taken by one of the classes in the System namespace or other namespace/class that's not yours but you're using/importing.

(*) By perfect I mean small, concise and clear names.

For instance I have an Utils class that has a Diagnostics (mostly debug utils) class and a Drawing class. I could:

  1. have a DrawingUtils class and a DiagnosticsUtils class, but that just smells like bad structure.
  2. pick a thesaurus and be done with an worse, longer or awkward name that's casually still not taken.
  3. Write class names in my native language instead of English.
  4. Ask the smart guys at StackOverflow.

I think options 1-3 aren't promising :(

EDIT:

Since my chosen answer doesn't address the problem definitively (neither I do), what I'd recommend for people facing the same situation is to ask yourselves: Will you frequently use the conflicting BCL class/namespace? If no, then let your name conflict (as I did with Diagnostics). If yes, add a word that limits the possibilities of your class/namespace.

In practice, this means:
"Drawing": Something that draws.
"MyCustomControlDrawing": Something that draws only on MyCustomControl. e.g.: "WidgetDrawing".

EDIT2:

Another solution to take a look next time: Extension Methods (courtesy of Lawnmower).

+3  A: 

To me, it really isn't worth the hassle of purposefully writing conflicting class names. You'll confuse other developers who aren't familiar with your codebase, because they will be expecting to use BCL classes but end up with yours instead (or vice versa). Then, you just waste their time when they have to write specific using aliases.

Honestly, coming up meaningful identifier names is a useful skill, but it isn't worth delaying your development. If you can't come up with something good quickly, settle for something mediocre and move on. There is little value in toiling over the names. I dare say there are more productive things you could be doing.

EDIT: I also don't believe that "small" is a component of a "perfect" identifier. Concise and clear, for sure, but if it takes a longer name to convey the purpose of a particular construct, so be it. We have intellisense, after all.

JoshJordan
Especially considering how easy it is to rename things in modern dev environments. Sometimes the perfect name for something doesn't hit you until you've been working with it for a while.
Matt Greer
Interesting post to downvote. Care to explain?
JoshJordan
^ Not sure, I got downvoted too...?
Matthew Abbott
Don't know why the downvote but for me small is a component of concise. I hate tiny names, but terribly long lines are almost as bad for readability.
Camilo Martin
Small and concise are distinct terms. Concise accurately describes a term that conveys a significant amount of information while retaining brevity. I would caution against using a "small" term just for the sake of having a tiny name to work with. There is certainly a balance to strike there with readability.
JoshJordan
I said small ***and*** concise ***and*** clear, besides brief=small. And of course it should be long *if* needed.
Camilo Martin
Well, not exactly. You said small is a component of concise. This is true, and is the reason for the clarification. The distinction is important. Being concise is desirable, but brevity for brevity's sake is not.
JoshJordan
+8  A: 

I don't see any issue with keeping the names Drawing, Diagnostics etc. That's one of the purposes of namespaces, to resolve naming conflicts.

Matthew Abbott
Well, that's what I think about `Diagnostics` but I'd be using two different "`Drawing`" classes extensively and close to each other.
Camilo Martin
+6  A: 

The beauty of namespaces is that they allow you to create classes with identical names. You can assign an alias to a namespace when you import it into your file with a using statement.

using MyAlias = My.Custom.Namespace;

this will keep your classes separate from Microsoft's.

you can then reference your classes as

MyAlias.Diagnostics

or you could alternatively assign an alias to Microsoft's namespace, but I wouldn't recommend this because it would confuse other developers.

Scott M.
This is exactly how I go about using class names that Microsoft tries to bogart.
Jim Schubert
This is annoying in practice. I always forget to add the line. VS doesn't do it automatically with the normal shortcut that adds all the other using lines. If reading code that uses aliases its easy to get confused. And should you ever need both classes from both namespaces your code is cluttered with Mycompany.myproject.mysubproject.mymodule.MyClass. No thanks. I'd rather have a name that's a little less perfect.
Lawnmower
I actually ran into this problem with a WPF application that uses both the WPF Toolkit datagrid and the built in datagrid control. I had to alias Microsoft.Windows.Controls because it had many of the names that System.Windows.Controls has. I agree that it is a bit annoying, but the same thing applies to appending "Utils" to every class name.
Scott M.
+4  A: 

Use namespaces to disambiguate your classes from the classes in other namespaces. Either use fully qualified names or a using statement that tells the compile what you need:

using Type = MyReallyCoolCustomReflector.Type;

Now if you want to still use the Type class from the System namespace:

System.Type sysType = anObject.GetType();

Generally I try to avoid name duplicates but this doesn't always work out that way. I also like simple, readable and maintainable code. So as often it is a trade-off decision.

John
Answer choosen becuse you've put it so clearly: "it's a trade-off".
Camilo Martin
+1  A: 

Well, if you want to avoid a namespace collision there are a couple of things you can do:


  • Don't collide, instead choose a unique name.

Example:

If you are creating a Math class you can name yours CamiloMartin.MathHelper


  • Use the long namespace to distinguish between collissions.

Example:

public class MyClass
{
    public int SomeCalculation(int a, int b)
    {
        return MyNamespace.Math.SomeFunc(a, b);
    }
}

  • Using an alias to differentiate.

Example:

using System.Math;
using SuperMath = MyNamespace.Math;

namespace MyNamespace
{
    public class MyClass
    {
        public int SomeCalc(int a, int b)
        {
             int result = Math.abs(a);
             result = SuperMath::SomeFunc(a, b);

             return result;
        }
    }
}
myermian
+1  A: 

Just for the record: .NET framework doesn't have neither Utils nor Diagnostics class. (But does have System.Diagnostics namespace.)

Personally I don't like general-purpose classes like Utils because their methods are not very discoverable (and usually either too general or too specific), therefore I would justify their use only as for internal classes.

As for the rest -- I agree with others on that namespaces are convenient. (Although I would thought twice to name the class if there is already a class in System with the same name, not because of name conflicts, but rather because the reason why I can't use 'original' class could mean that the class I'm about to create is semantically different.)

Regent
I never use something like "Utils" from a different assembly, it's inernal stuff.
Camilo Martin
PS: did I ever say `Diagnostics` *namespace*?
Camilo Martin
@Camilo Martin, no you didn't. I just subconsciously recalled why one [shouldn't name a class as its namespace](http://blogs.msdn.com/b/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx)... But it's not relevant for your case anyway.
Regent
+1  A: 

Often its possible to choose a more specific name. Take Utils for example. Absolutely everything can be called a utilitiy. For the reader of your code this classname is worthless.

Often utility classes are a collection of methods that didn't really fit anywhere else. Try to place them where they belong, or group them by some criteria, then use the group as a classname. Such grouping is in my experience always possible.

In general:

  1. That's what we are doing (hey, we can refactor it later)

  2. Used it once or twice but only on important classes. Especially useful if you don't know the 'perfect' name yet.

  3. don't even think about this...

Using namespace aliases is no fun. So I avoid it if I can.

Lawnmower
One of my "Utils.Drawing" is a handy lockbits wrapper for fast setPixel(). Another one is a RGB<>HSL util. These should IMHO be in the System.Drawing namespace.
Camilo Martin
Depending on the datatypes they work on you might want to consider extension methods. Then the class name has no influence anymore. I'd put RGB<>HSL into a separate ColorSpaceConverter class. Sooner or later you'll need more colorspaces anyway :)
Lawnmower
Hm, thanks for the recommendation. I'll take a look on extension methods! :)
Camilo Martin