PDA

View Full Version : One File == One Class or what ??



terotech
8th September 2003, 20:06
A long time ago I used to develop in C++. (Okay, not that long ago :D ) and used to follow the 'One Class, One file' development mindframe (and still do). Today I had a discussion with another contractor with whom I work on a day-to-day basis, he has placed all of his classes in a single file (with some weird file-naming convention) instead of having the files named the same as the contained class. The question is this: what is the norm/standard/preferred method of organising your project ?? I know that it is really a matter of choice, but I feel quite strongly that the one file per class is the 'right' way to do things, not least when working on a project that has multiple developers working on the same source code 99% of the time...

discuss.....

Mark Snowdon
8th September 2003, 20:22
I have never been a fan of monolithic code. OK perhaps in the days of yore, but any language that supports splitting an application into multiple files is doing a good thing.

It doesnt need to be OO to be a good idea, in plain C I make my code into libraries.

Isnt modular programming seen as a good thing these days ?

I would be worried about the competence of a developer of OO code that put everything in a single source file.
What does he do about change control ?

terotech
8th September 2003, 20:29
Thats exactly what I said when I joined the project, so far he has been working alone so it hasn't bothered him. He has roughly 3 or 4 classes per file at present, and he's adding to these files all of the time. I know what I 'think' we should be doing and how we should be doing it, I just can't seem to find any evidence of this on the web (even on the Microsoft site !!). Any useful URLs etc. much appreciated......

D

AtW
8th September 2003, 20:38
i'd create new file for every class thats either:
a) not relevant
b) bigger than X lines
c) total length of file is greater than Y lines

X could differ for different people but for me 1000 would be a good number.

Y would be 5,000

My personal record is file 120kb big :)

OrangeHopper
9th September 2003, 09:50
If he has 3 to 4 classes per file then presumably he has only the one header file for the class definitions. This would be regarded by many people, including Stroustrup, to be bad for a number of reasons - organisation, encapsulation, compilation dependencies.

The only case where I would regard this arrangement as valid is if only one class is publically available and the others are private helpers to that class.

reynolds
9th September 2003, 18:35
i use inner classes only when they're small - say <100 lines. they are usually variants of API classes as well.

AtW
9th September 2003, 19:44
Stroustrup is watching Java and biting his nails considering to retire - he wont blame ya for not doing your C++ using correct number of files cuz he would be happy enough to know someone else is using his soon to be Latin-like language ;)

AtW
C++ user since 1991

terotech
10th September 2003, 07:54
So it would seem from the current consensus that the one class per file arrangement is the method of choice ?? - We know that there are reasons when you *may* wish to put more than a single class in a source file, but there don't seem to be any valid reasons (or mandatory reasons for that matter) for doing so.

I have since spoken to the guy involved and let him know of several reasons for following this approach, I still can't seem to find any decent 'guidelines' documents on the web that specifiy this though ....

reynolds
11th September 2003, 18:21
its just sometimes convenient to have them in one file i suppose.

OrangeHopper
11th September 2003, 19:21
Ah, our resident expert - concise and straight to the heart of things.

reynolds
11th September 2003, 20:24
yes that's right OrangeHopper.

I thought about perhaps making a song and dance out the question but decided against this proposition due to the fact that it isn't warranted and would be unjustifiable.

SupremeSpod
19th September 2003, 12:37
A header file for the class definition/documentation.

A Source file for the implementation.

When some other fecker wants to use the class, give him the header file and a library.

Spod - In "Fcuking Amateurs" mode!

darrenez
19th September 2003, 21:17
Since I use Java / C++ / Javascripy and C#
I have found that it makes sense in terms of the size of .obj
to have a C++ header and a C++ source.

Now being that I love Microsoft Visual C++
I have found some really wierd and wonderful
circumstances where you need to move aspects of the
body to the header if you go from vc6 to vc7.

When using the document/view architecture of that
particular compiler I often want view parameters
at the start up of the view
rather than in say onUpdate()
So I often end up with more than one class per file pair.

Clearly if you plan to create a re-usable DLL
it makes no sense to put all the source in one or two files
as upgrades become a right 'mare.