[Tutor] class question

Alan Gauld alan.gauld at btinternet.com
Wed Jan 26 01:50:55 CET 2011


"Elwin Estle" <chrysalis_reborn at yahoo.com> wrote 

> Is it better to have one large sort of "do it all" class, 
> or break the larger class up into smaller classes? 

Usually the latter. But remember that classes model objects.
Objects usually have a direct relationship to some kind of 
"real world" entity - even if its an abstract conceptual entity.

So if the attributes and behaviours are all exhibited by the 
thing you are modelling then they should maybe stay together.
But uasually the big thing can be modelled as a composition 
of smaller things...

> Seems to me like the one large class would be clearer 
> in some ways. 

Very rarely is a big lump of code clearer than several small
single purpose lumps of code. And big lumps of code are 
nearly always harder to reuse - one of the other reasons 
to create classes...

> I have something I am trying to do that have somewhere 
> in the neighborhood of 20 attributes that all relate together, 
> however there are sort of "clumps" of attributes that have 
> a sub-relationship to each other 

Without specifics we can't be sure. But it sounds like you 
have a composite structure. OTOH 20 attributes is not 
enormous - 200 would be, but not 20. But even 20 is enough 
to challenge.

> I have only the foggiest notion of inheritance 

It doesn't sound like inheritance is an issue here. It sounds 
like we are talking about composition. But without specific 
details we can't be sure.

Remember that the thing that defines a class should be 
its operations not its data. Do these smaller "clumps of attributes" 
have common behaviour associated with them? The data 
should only be there to support the operations.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list