class size

B

Brad Tilley

How large (lines of code) should a class be before one should split it
into smaller classes? Is there a general rule on this? How many
functions should a class contain? Are there limits (theoretical or
practical) that people learning classes should adhere to?
 
L

Larry Bates

I doubt there is a hard-and-fast rule. I think most
programmers tend to think of classes as representing
logical objects (things). If those things contain
other things then they most likely need their own
class.

Example:

I recently wrote a inbound fax forwarding program
that had things like:

Network Neighborhood
Fax Server (n-to a Neighborhood)
Groups (n-to a Server)
Rules (n-to a Group)
Routes (n-to a Rule)

Since each of these were distinct things, they
each got a separate class.

Then I had:

Fax Queue (1-to a Server)
Fax (n-to a Fax Queue)

Each of these got their own class. That way the
methods and attributes for each of these was
distinct and it made it much easier to isolate
and debug my code. By using iterators, I could
then write very straightforward code to loop over
each "thing".

Another reason to introduce a new class would be
if you want to abstract the interface to some
information. That is hide the mechanics of some
interface behind a class so that it is easier
to change or to break it into smaller units that
are easier to understand. I think this is what
you are referring to. I personally don't like
classes that have over a couple of hundred lines
(but sometimes I "break" that rule). The smaller
they are the easier they are to understand and
to debug.

Larry Bates
 
J

Jeremy Bowers

How large (lines of code) should a class be before one should split it
into smaller classes? Is there a general rule on this? How many
functions should a class contain? Are there limits (theoretical or
practical) that people learning classes should adhere to?

As long as the class is doing one thing (or as I prefer to think of it,
representing one concept), you're fine. I have classes that are 4 lines
long (and would probably be a one-liner in most other languages), and
classes 500 lines long... and that class has already had another couple
hundred lines cut out of it and put into other classes.
 
P

Peter Hansen

Brad said:
How large (lines of code) should a class be before one should split it
into smaller classes? Is there a general rule on this? How many
functions should a class contain? Are there limits (theoretical or
practical) that people learning classes should adhere to?

Discussing this issue quantitiatively is wrong. The real
concern should be about things like coupling, coherency,
readability, and maintainability. Learn about those concepts and
work on lots of code, yours and other people's, until you have
developed an ability to judge when, in a given case, there are
"too many" lines, "too many" classes, "too many" methods. There
are no strict rules that are useful.

On the flip side, if 99% of modules should have fewer than 100,000
lines of code, and putting 70 classes in a single module is
probably a bad idea. ;-)

-Peter
 
P

Peter Hansen

Peter said:
Discussing this issue quantitiatively is wrong. The real
concern should be about things like coupling, coherency,
readability, and maintainability. Learn about those concepts and

D'oh! Brain fart... I meant "cohesion", not "coherency".
I should have paid attention to my hesitation while typing
that word...

(Of course, code should probably be coherent, too, but it's
not typically seen as a concern... text files rarely break
apart spontaneously. ;-)

-Peter
 
J

John Lenton

D'oh! Brain fart... I meant "cohesion", not "coherency".
I should have paid attention to my hesitation while typing
that word...

(Of course, code should probably be coherent, too, but it's
not typically seen as a concern... text files rarely break
apart spontaneously. ;-)

actually, I think on of the 'code smells' should be coherence, in the
laser-is-coherent-light sense. Code has got to flow right.

--
John Lenton ([email protected]) -- Random fortune:
If you find a solution and become attached to it, the solution may become
your next problem.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBUIySgPqu395ykGsRAg9zAJ9wplPWlO00YZ3+dMNgBEAbzP0nFACfY+nF
LEC+emgGVlU3pfZ7c/sISqs=
=kbgs
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,678
Latest member
Aniruddha Das

Latest Threads

Top