One module per class, bad idea?

Carl Banks pavlovevidence at gmail.com
Tue Dec 12 18:09:28 EST 2006


Carl J. Van Arsdall wrote:
> Isaac Rodriguez wrote:
> >> Yes, it would be a bad idea. =)
> >>
> >
> > Saying it is a bad idea and not explaining why will not help anyone. I
> > would like you to elaborate on why it is a bad idea to have one file
> > per class.
> >
> A module per class makes a lot of sense in some cases, or rather, make
> your module your class (look at the singleton pattern).  I actually like
> to structure all of my code like this, it helps me keep things organized
> and separated.

I don't understand.  Are you saying you organize your code to be a
bunch of modules masquerading as singleton classes?  (When I was a
young'n, we called that "procedure oriented programming" :)


> I guess i'm not sure why it would ever be a really bad
> idea, maybe if you had really small classes?

Or maybe you have really big classes.

The potential problem with one module per class is one of missed
opportunity: namely the missed opportunity for a higher-level
organization.

Classes are rarely, probably never, a good way to organize code at the
highest levels.  It's better to organize code into subsystems of some
sort.  It's very rare that each class is an independent subsystem unto
itself; most high-level subsystems would encompass many classes (as
well as other code).  When you strictly obey a one class per module
rule, you lose the possibility of using the module as a means of
organizing subsystems (and subsubsystems, and so on).

Now, I think this is the best way to use modules, but you don't need to
use modules to do get higher-level organization; you could use packages
instead.  It's a pain if you're working on two different classes in the
same system you have to keep switching files; but I guess some people
prefer to switch files rather than to scroll for some reason.

I'd say as long as you use package system, and not just a flat
modulespace, it's not fundamentally disorganized to use a one class per
module rule.  In the end, it probably comes down to what you prefer,
but I think most people would prefer not to obey a one class per module
rule.


Carl Banks




More information about the Python-list mailing list