Reputation: 11090
A quick question about class design. I'm fairly new to Software Development and wondered how people structure their classes.
By that I mean, for example, if you have an interface and a number of small classes that inherit that interface, would you put group them all into a single source file or split them and have one class per file?
Thanks.
Upvotes: 2
Views: 225
Reputation: 44804
For me it depends on the size of things. If each child class has a large complex implementation, then I'd put them in different source files. However, if the child classes are trivial and would typically all be used in the same code, it is really kind of a pain for everyone involved (coder and future maintenence programmers) to split them.
Its the cases in between those two extremes you have to make a judgement call on. Personally I like to err on the side of splitting them up.
If you do stick them all in the same source file, be careful with the naming so that your poor maintenence techs can find things. What I typically do is put them all in a namespace, and name the source file the same as the namespace. A good name to use is anything that you'd otherwise be tempted to put in the name of every single child class.
Upvotes: 1
Reputation: 2424
It's nice that you ask that kind of question, but I don't it really important, I think in the start you should invest some time in the design, but make sure you don't get stuck on those kind of question, first make your program run, and then ponder..
Upvotes: 1
Reputation: 28492
In general, it is a good practice to place such classes to one namespace, and distribution between different files depends on language. In Java you place them to one package (i.e. directory), but placing then into one file (as inner classes, for example) is a bad practice. In C# it is considered OK to put classes into one file if they are not too big and too complex. I guess C++ takes the same approach.
And, of course, don't forget about semantics of classes. If your classes have different meanings, put them to different files.
Upvotes: 3
Reputation: 4762
In .Net, I always put classes, interfaces, etc. in individual files (and this is a breeze with refactoring tools). It makes hunting them down in the project list easier (no classes embedded in odd misnamed files), and navigation within the IDE is cake.
Also, this way as classes expand or, in some cases, sprout into new classes, I don't end up with giant frankenfiles with smaller classes embedded alongside what now may be an unrelated one. Also makes looking at my changesets in source control easier since I can scan the names to see what was done vs having to dig through the files themselves.
And lastly, makes it easier to later break the classes/interfaces into separate assemblies for reuse without having to tear apart the class file.
Upvotes: 1
Reputation: 20840
I always go by one class per file, even if the classes are only small. It's likely these classes will grow larger over time, so it's an organic step towards keeping your code well organized.
Upvotes: 1
Reputation: 28693
Sometimes it depends on the language you use. For example, in Java, you can't put more than one public classes in one file (since files in Java are basically "classes"). If the language you allows to do it, you might want to consider file-level encapsulation (for example - modules in Python may contain many classes) and put many classes in one module. Size and complexity do matter in this case, because it is more convenient to put complex code in a separate file.
Upvotes: 1
Reputation: 7872
Depending on the language used, you may not be able to use a single file. This is the case in Java for instance.
Using one file per class is the proper way to do it, because it gives you a common point between your file organisation and your class design, which is helpful for managing the source code.
Dont forget to use a parent class is there is common source code between the classes.
Upvotes: 1
Reputation: 308763
I'd have them in one class per file. Java would demand such a thing if the classes implementing the interface all had public access. I'd have no choice. Only if they were all package private would I be able to put them all in the same file.
Upvotes: 1