E
Erik Johnson
There are arguments of preference to be made on both sides. I think the
question largely comes down to what is "workable" and "maintainable". To
answer the original question, I think it is not necessarily a bad idea to
have one class per file. But if your classes are small, or certain classes
are logically related to other classes (e.g., one class is a subclass of
another, or one class is implemented in terms of another, or the two classes
work together to accomplish some task and it wouldn't make much sense to be
using just one of the classes), it makes a lot of sense to keep them
together in the same file. If your classes are only a few dozen lines,
making lots of files may be more of a pain than having the code together in
one file.
Where I work, we started with a set of classes that provided a layer of
abstraction and encapsulation to accessing our database tables. An object
was basically just a loaded DB record and generally didn't provide a lot
else. As time went on, we started to code more and more logic into these
classes that provided various functions to make it easy to load records, do
certain kinds of computations and filtering with them, to generate the SQL
to do the insert for the record, etc.
The file has now grown into a 6800 line beast (including docstring,
whitespace, and CVS history). Pretty much any time we implement some new
functionality, there are at least a few changes in that file. When you have
multiple developers working on different projects, and they all need to be
making changes to that file, the chances for someone making a merge error
goes up. So, we are obviously at a point where that file needs to be split
up, but there are lots of other files that import and use the one file, so
it is a task that has been put off. In retrospect, I wish I has started
things under a one class per file strategy, but at the time, it didn't make
a lot of sense to split those things up and I didn't anticipate the code
getting that big.
So... there's no magic "one size fits all" answer here - do what makes
sense.
As Carl points out, decent editors should be able to handle dispaying
different files. I use vim and know I can split the window (both
horizontally and vertically), editing either different sections of the same
file or different files and can cut and paste between the two windows. In
practice, I usually just jump between places in the same file under a single
window, or I textually cut and paste between two separate vim sessions, but
if that's something you need to do a lot, you might want to invest a bit in
learning how to use split windows in your editor. Some of the documentation
for doing this under vim can be found here:
http://vimdoc.sourceforge.net/htmldoc/windows.html#:split and here:
http://vimdoc.sourceforge.net/htmldoc/usr_08.html
(I'm sure emacs can do this as well, but I don't know emacs.) If your
editor can't handle similar tasks, you might want to consider getting a
better editor.
HTH,
-ej
question largely comes down to what is "workable" and "maintainable". To
answer the original question, I think it is not necessarily a bad idea to
have one class per file. But if your classes are small, or certain classes
are logically related to other classes (e.g., one class is a subclass of
another, or one class is implemented in terms of another, or the two classes
work together to accomplish some task and it wouldn't make much sense to be
using just one of the classes), it makes a lot of sense to keep them
together in the same file. If your classes are only a few dozen lines,
making lots of files may be more of a pain than having the code together in
one file.
Where I work, we started with a set of classes that provided a layer of
abstraction and encapsulation to accessing our database tables. An object
was basically just a loaded DB record and generally didn't provide a lot
else. As time went on, we started to code more and more logic into these
classes that provided various functions to make it easy to load records, do
certain kinds of computations and filtering with them, to generate the SQL
to do the insert for the record, etc.
The file has now grown into a 6800 line beast (including docstring,
whitespace, and CVS history). Pretty much any time we implement some new
functionality, there are at least a few changes in that file. When you have
multiple developers working on different projects, and they all need to be
making changes to that file, the chances for someone making a merge error
goes up. So, we are obviously at a point where that file needs to be split
up, but there are lots of other files that import and use the one file, so
it is a task that has been put off. In retrospect, I wish I has started
things under a one class per file strategy, but at the time, it didn't make
a lot of sense to split those things up and I didn't anticipate the code
getting that big.
So... there's no magic "one size fits all" answer here - do what makes
sense.
As Carl points out, decent editors should be able to handle dispaying
different files. I use vim and know I can split the window (both
horizontally and vertically), editing either different sections of the same
file or different files and can cut and paste between the two windows. In
practice, I usually just jump between places in the same file under a single
window, or I textually cut and paste between two separate vim sessions, but
if that's something you need to do a lot, you might want to invest a bit in
learning how to use split windows in your editor. Some of the documentation
for doing this under vim can be found here:
http://vimdoc.sourceforge.net/htmldoc/windows.html#:split and here:
http://vimdoc.sourceforge.net/htmldoc/usr_08.html
(I'm sure emacs can do this as well, but I don't know emacs.) If your
editor can't handle similar tasks, you might want to consider getting a
better editor.
HTH,
-ej