Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Due to test-driven development, one ends up with many classes doing just one thing. It is quite a headache just to see where such classes would be placed inside the folder structure.

First of all, is it considered a bad-pattern, to put related classes in the same file?

Any suggestion on what one uses in actual-production environments?

share|improve this question

migrated from Feb 15 '13 at 15:10

This question came from our site for peer programmer code reviews.

Hi Karl, unfortunately your question is offtopic here according to FAQ – almaz Feb 15 '13 at 14:49
And by the way, "doing just one thing" is a good approach unless you start trying to split a single business function into several classes. Think of List class - it has a single function but does multiple things (adds, deletes items etc). I don't see issues in putting small related classes into one file as long as resulting file is of manageable size – almaz Feb 15 '13 at 14:54
Are you asking about Test Classes or functional classes? These are two different concerns. – Michael Brown Feb 15 '13 at 15:38
What would be the naming pattern for files with multiple classes inside? – Den Feb 15 '13 at 17:19
Also it is not because of TDD. It's because of following SOLID principles (too) well. – Den Feb 15 '13 at 17:21
up vote 2 down vote accepted

Generally, you should put one class in one file. That way it is easy to find the pertinent code when making changes. I think that it would be more of a headache to have to try to remember how the classes were organized when looking for the code that I am trying follow. You aren't creating these files for the computer but for the future developer (it might be you) that is going to be trying to find some bug or add a new feature. And files make that easier to do because A.class is an easier to look for than Stuff.class.

If you create a directory structure that groups similar classes together, that can reduce some of your headache.

share|improve this answer

In languages that support it, why not use namespaces? That should make it clear what the organization is, regardless of the folder structure.

share|improve this answer

Your Answer


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.