Why NOT only one class per file?
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Apr 5 15:47:19 EDT 2007
Sherm Pendley a écrit :
> Bruno Desthuilliers <bruno.42.desthuilliers at wtf.websiteburo.oops.com> writes:
>
>
>>Sherm Pendley a écrit :
>>
>>
>>>It's a stylistic thing, nothing more.
>>
>>A bit more than just 'stylistic' IMHO. It's a matter of
>>convenience. Having to manage hundreds of files each with a dozen
>>lines of code is a PITA.
>
>
> Yes, but the pain in that case comes from having hundreds of trivial
> classes,
The fact that a class is short doesn't imply it's trivial.
> not from the way in which the source for them is organized.
I have to disagree. Having several, closely related objects (ie:
classes, functions, etc) in a same file allow to "see the big picture",
and avoids the mental overhead of navigating thru files, directories,
opened windows etc.
Of course, it doesn't mean that one should store the whole program in a
single monster file - I usually start to rearrange things when the file
grows bigger than one thousand lines.
>>Having to retype the same import statements
>>in hundreds of files is a PITA - and a good way to waste time and
>>forget something when one has to fix these import statements (yes,
>>even with the appropriate tediting tools). I wouldn't call such
>>considerations "nothing more than stylistic".
>
> Neither would I. But then, I would describe the existence of all those
> files as just the symptom - the real problem being the design that needs
> all of those hundreds of trivial classes in the first place.
In my book, it's huge classes and methods that are usually a smell of a
design problem. Specially with languages like Python or Ruby. Factoring
out code duplication usually leads to lot of small objects - and also to
a significant reduction of the code base's size.
> Granted, I know very little of Python. It may be the case that Python
> encourages the use of hundreds of "little classes"
Python being very expressive, and having powerful metaprogramming
features, there's usually much less boilerplate (than in languages like
Java) for a same result. So yes, it's quite usual to have small classes
and functions.
> - and if that is the
> case, then I agree, storing each one in its own file would be rather
> absurd.
Indeed. That's the point (or at least part of it).
> I'm more accustomed to writing classes that tend to be larger - hundreds
> if not thousands of lines each,
It sometimes happens that a class or function needs to be much bigger
than the average. So be it. But if I found myself writing
thousands-lines classes in Python, I'd start to seriously consider
rethinking the whole stuff.... Heck, I have very few *modules* that are
more than one thousand lines long, and almost never a single class per
module.
> or more -
In Python ???
More information about the Python-list
mailing list