Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In C# projects, we frequently group small and tightly related classes into the same .cs file. This practice reduces the friction of dealing with a myriad of files containing close to no actual code. Yet, is there an established practice to name the file that contains multiple classes?

share|improve this question

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

migrated from stackoverflow.com Nov 23 '11 at 19:33

This question came from our site for professional and enthusiast programmers.

11  
frankly I prefer one file one class even for very small classes –  Felice Pollano Nov 23 '11 at 8:44
    
You can choose to name it as per the group these tightly related classes fall under. –  V4Vendetta Nov 23 '11 at 8:47
    
Maybe those classes ARE too small? Personally I only group multiple enums in one file (sometimes with the class, sometimes all enums together, depending on the context). –  Konrad Morawski Nov 23 '11 at 8:55
    
@Felice: look at MSpec :) –  insta Apr 20 '12 at 16:45

5 Answers 5

My advice: avoid files containing multiple classes and name files==classes even if you mean there are too many files. Try to organize them in folders. In very special cases you might have nested classes. In this case it makes sense separating the main class and a nested class into different files having one partial class. In this case I use following naming convention.

MyEnumerable.cs
MyEnumerable.MyEnumerator.cs

Probably you can also use multipart file names in your case.

share|improve this answer
2  
One more idea. You can group multiple .cs files inside Visual Studio Project so that they all appear under the same node in tree (like .Designer.cs files). This will signal you that classes tightly belong together. –  achitaka-san Nov 23 '11 at 8:58

We use common sense to decide:

  • If there is one main class and one or two tiny accompanying classes (such as just properties, no logic etc), we name the file after the main class.

  • If there are multiple big classes we split them into their own file.

Therefore, the file name always corresponds to a class name.

share|improve this answer

Try to avoid having more than one class in a file, not more than one public class atleast. While naming class files with multiple classes, choose a descriptive filename.

Refrences:

share|improve this answer
    
+1 for references –  Konrad Morawski Nov 23 '11 at 10:16

I used to be all for having multiple classes in one file, but ever since I started actually working as a programmer (rather than being on my own), I have found that it can be a maintenance nightmare with many classes in a single file. Although, admittedly, Visual Studio can help a lot by using F12 (go to definition).

What I have started doing is using a naming convention as follows: namespace.classname.cs That way I know exactly what is in each file and it also provides some sort of overall context of the file. If the class is going to be in the default namespace, then I go ahead and just use the classname.cs (similar to Java).

share|improve this answer

Consider that by asking a question about how to name such files, you have already identified a code smell that is problematic for you, and this is a problem that will not go away easily.

Regardless of the reasoning, you'll find that it is difficult to choose sensible names when you have more than one class in a file, because even if the files are related in some way, there will always be the temptation to throw in something that doesn't quite fit the group name that you assign to a set of classes.

The short answer therefore is that there is no recognized or established standard for naming these sorts of files. My suggestion would be to sit down with your team and look to the organization of your code, and apply a bit of a refactoring effort to tidy things up a bit.

However, to make things a little easier to begin with I'd ask if the additional classes are nested, or independent. If independent, then these classes can be more easily split out into their own files, with each file named for the single class contained within. If nested, then further decisions may need to be made about how such classes should be organised, however the nested classes may not need to be removed immediately, and the files would therefore be named for the outermost class. As far as nesting is concerned, there are lots of arguments against, and a few reasonable ones for nesting under some circumstances, and my advice would be to ask if sensible namespacing and creating a more organized source code structure might be a better way to tackle the problem.

Realistically however, you will find that by the end of the lifetime of your project, you will have wished your files all had contained only a single class and a nice directory/namespace structure to go with it.

Cheers,

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.