tags:

views:

418

answers:

8

I want to be able to distinguish between a generic and regular (non-generic) version of a class. Much like the .NET framework does with it's generic and non-generic versions of several of it's interfaces and collection classes. (Queue, Queue(T))

I generally like to follow the convention of one class per file (as in Java). Is there a common convention for naming files containing a single generic class? I'm mostly interested in Windows (NTFS specifically) but it seems like a good convention would be (at least a little) portable.

+1  A: 

How about:

Type.cs

and

TypeGeneric.cs

Whenever I have done this in the past I have always put both types in one file with the non-generic type as the file name. I think that this makes things pretty clear as .NET has no conventions/restrictions on one type per file like Java does.

But if you must then I would suggest something like I have above, and using a suffix will make the files show up together in any alphabetized list (Solution Explorer, Windows Explorer, etc.).

Here is another idea:

Type`1.cs

This would allow you to break out different generic types by the number of generic type parameters they accepted. Its just a thought though as I still think it would be simpler to just put all the types in one file.

Andrew Hare
A: 

I'd probably have two folders in the project, something like Gereric, NonGeneric or something like that. They can still be in the same namespace, and then they can both have the same file name. Just a thought...

BFree
I personally try to avoid classes in the same namespace in different folders. I like the folder hierarchy to match the namespace hierarchy, for the benefit of tomorrow's developers.
Joe
Fair enough. I probably would have them in a separate namesapce anyway. Something like MyProj. and MyProj.Generic.
BFree
A: 

All new Microsoft classes use generics. The Queue and ArrayList were there before generics came out. Generics is the way forward.

The convention for one-class-per-single file is to name the filename after the class name (whether generic of not). For MyClass, you'll have MyClas.cs. For every new namespace you'll need to create a new folder. This is how Visual Studio also works.

taoufik
+1  A: 

I would probably put them in folders and use the namespace mechanism instead. You can compare with System.Collections vs. System.Collections.Generic. On the other hand, if it's more common than not that the classes use generics, perhaps it's better to point out those that are not. That is if you really want to separate the generic classes from other classes. Personally I usually don't bother to do that, since I don't really see a practical benefit from it.

Fredrik Mörk
+1  A: 

From the responses so far it seems there isn't a consensus.

Using the same filename in a sub-namespace (and sub-folder) "Generics" (like System.Collecctions.Generics) is an option. But it's not always desirable to create a new namespace.

For example, in an existing namespace with non-generic classes that are maintained for backwards compatibility, but marked with ObsoleteAttribute, it's probably better to keep the generic versions in the same namespace.

I think a suffix is a reasonable way to go. I've adopted a convention of using the type parameters as a suffix (so: MyClassT for MyClass<T>, or MyDictionaryKV for MyDictionary<K,V>.

Joe
+8  A: 

At Microsoft, they use ClassNameOfT.cs.

Mark Cidade
Like this, pretty clear. Although I would probably still name the class the same as the non-generic version.
Darren Clark
I guess I'd wonder how they would do something like a dictionary. DictionaryOfKT ?
sixlettervariables
In the ASP.NET MVC 1.0 source code they use the Dictionary`2.cs convention.
McKAMEY
+3  A: 

Just found this question after looking for what conventions other people use for generic class filenames.

Lately I've been using ClassName[T].cs. I really like this convention, and I think it's superior to the others for the following reasons:

  • The type parameters jump out at you a little more than they do with the Microsoft convention (e.g., ClassNameOfT.cs).
  • It allows you to have multiple type parameters without too much confusion: Dictionary[TKey, TValue].cs
  • It doesn't require you to create any special folders, or to have your generic classes in a special namespace. If you only have a few generic classes, having a special namespace dedicated to them just isn't practical.

I borrowed this convention from Boo's generic syntax, albeit slightly modified (Boo uses ClassName[of T]).

Some developers seem to have a phobia of filenames that contain anything but letters and underscores, but once you can get past that this convention seems to work extremely well.

Nick Aceves
A: 

Don't use the grave accent ` in your generic file names if you're running Visual Studio 2008. There's a known issue with them that causes breakpoints to fail:

http://connect.microsoft.com/VisualStudio/feedback/details/343042/grave-accent-in-filename-causes-failure-to-recognize-target-language-breakpoints-fail

anon