views:

1399

answers:

13

I used to have one class for one file. For example car.cs has the class car. But as I program more classes, I would like to add them to the same file. For example car.cs has the class car and the door class, etc.

My question is good for Java, C#, PHP or any other language. Should I try not having multiple class in the same file or is it ok?

+10  A: 

I think you should try to keep 1 class by file.

I suggest you this because it will be more easy to find your class later. More, it will be more easy for your source control (if a file change, than you know that the class has changed).

The only time I think it's correct it's for internal class... but the class is inside the class so of course inside the same file. This is so related to the class that is fine.

Daok
+3  A: 

I've found that whenever I try to combine multiple types into a single file, I always end going back and separating them simply because it makes them easier to find. Whenever I combine, there is always ultimately a moment where I'm trying to figure out wtf I defined type x.

So now, my personal rule is that each individual type (except maybe for child classes, by which a mean a class inside a class, not an inherited class) gets its own file.

Daniel Schaffer
+3  A: 

It can be bad from the perspective of future development and maintainability. It is much easier to remember where the Car class is if you have a Car.cs class. Where would you look for the Widget class if Widget.cs does not exist? Is it a car widget? Is it an engine widget? Oh maybe it's a bagel widget.

Steven Behnke
+3  A: 

If you are working on a team, keeping classes in separate files make it easier to control the source and reduces chances of conflicts (multiple developers changing the same file at the same time). I think it makes it easier to find the code you are looking for as well.

Jim Anderson
+3  A: 

The only time I consider file locations is when I have to create new classes. Otherwise I never navigate by file structure. I Use "go to class" or "go to definition".

I know this is somewhat of a training issue; freeing yourself from the physical file structure of projects requires practice. It's very rewarding though ;)

If it feels good to put them in the same file, be my guest. Cant do that with public classes in java though ;)

krosenvold
+2  A: 

As a rule of thumb, one class/one file is the way to go. I often keep several interface definitions in one file, though. Several classes in one file? Only if they are very closely related somehow, and very small (< 5 methods and members)

Treb
+1  A: 

One class per file is simpler to maintain and much more clear for anyone else looking at your code. It is also mandatory, or very restricted in some languages.

In Java for instance, you cannot create multiple top level classes per file, they have to be in separate files where the classname and filename are the same.

Robin
+6  A: 

In Java, one public class per file is the way the language works. A group of Java files can be collected into a package.

In Python, however, files are "modules", and typically have a number of closely related classes. A Python package is a directory, just like a Java package.

This gives Python an extra level of grouping between class and package.

There is no one right answer that is language-agnostic. It varies with the language.

S.Lott
+3  A: 

The rule I always go by is to have one main class in a file with the same name. I may or may not include helper classes in that file depending on how tightly they're coupled with the file's main class. Are the support classes standalone, or are they useful on their own? For example, if a method in a class needs a special comparison for sorting some objects, it doesn't bother me a bit to bundle the comparison functor class into the same file as the method that uses it. I wouldn't expect to use it elsewhere and it doesn't make sense for it to be on its own.

Boojum
+5  A: 

One class per file is a good rule, but it's appropriate to make some exceptions. For instance, if I'm working in a project where most classes have associated collection types, often I'll keep the class and its collection in the same file, e.g.:

public class Customer { /* whatever */ }

public class CustomerCollection : List<Customer> { /* whatever */ }

The best rule of thumb is to keep one class per file except when that starts to make things harder rather than easier. Since Visual Studio's Find in Files is so effective, you probably won't have to spend much time looking through the file structure anyway.

Kyralessa
If you use an add-in like CodeRush, it's easy to create classes in the same file and move them to their own files later. It's just one click in CodeRush to move a class to its own file.
Kyralessa
+1  A: 

As is true so much of the time in programming, it depends greatly on the situation.

For instance, what is the cohesiveness of the classes in question? Are they tightly coupled? Are they completely orthogonal? Are they related in functionality?

It would not be out of line for a web framework to supply a general purpose widgets.whatever file containing BaseWidget, TextWidget, CharWidget, etc.

A user of the framework would not be out of line in defining a more_widgets file to contain the additional widgets they derive from the framework widgets for their specific domain space.

When the classes are orthogonal, and have nothing to do with each other, the grouping into a single file would indeed be artificial. Assume an application to manage a robotic factory that builds cars. A file called parts containing CarParts and RobotParts would be senseless... there is not likely to be much of a relation between the ordering of spare parts for maintenance and the parts that the factory manufactures. Such a joining would add no information or knowledge about the system you are designing.

Perhaps the best rule of thumb is don't constrain your choices by a rule of thumb. Rules of thumb are created for a first cut analysis, or to constrain the choices of those who are not capable of making good choices. I think most programmers would like to believe they are capable of making good decisions.

AmanKow
+1  A: 

No I don't think it's entirely bad practice. What I mean by that is in general it's best to have a separate file per class, but there are definitely good exception cases where it's better to have a bunch of classes in one file. A good example of this is a group of Exception classes, if you have a few dozen of these for a given group does it really make sense to have separate a separate file for each two liner class? I would argue not. In this case having a group of exceptions in one class is much less cumbersome and simple IMHO.

James
A: 

The Smalltalk answer is: you should not have files (for programming). They make versioning and navigation painful.

Stephan Eggermont