473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help, my class is exploiding!

I work on a rather large C++ project. The design, so far, seems to be
fine. However, there is one class, where the number of methods (and less
dramtically the number of members, too) is growing and growing. I
thought of different strategies to divert the glut into 'different
functional units', like

* through inheritance
* using member objects to encapsulate the methods of the parent object
* split the class without a parent class
* using array of function pointers (ugly, yes)
* merging some methods

but none of this seems to fit well into the design. As already
mentioned, I feel that the design is okay, it is simple too hard to keep
the overview. I know, this does not sound like a good design.

Think of a neat little box of hardware, which can be used as a phone,
pda, fax machine, tv, bar code reader, mp3 player, .... etc. It is a
perfectly fine object, because all works fine, and it is easy to control
the different capabilites, and it's even possible to send the a tv
screen shot as a fax. So the box is fine, but it has so darn many
connectors (tv line, phone line, audio in, audio out, ... ). Actually, I
like all these connectors, but it is so hard to keep the overview.

Back to the code: One idea I had, was to seperate the class declaration
into different files, but I don't know how to do this in a nice way.

Any ideas?

-Sacha
Oct 25 '05 #1
9 1449
On Tue, 25 Oct 2005 10:20:55 +0200, Sacha <sa**********@unibas.ch>
wrote:
I work on a rather large C++ project. The design, so far, seems to be
fine. However, there is one class, where the number of methods (and less
dramtically the number of members, too) is growing and growing. I
thought of different strategies to divert the glut into 'different
functional units', like

* through inheritance
* using member objects to encapsulate the methods of the parent object
* split the class without a parent class
* using array of function pointers (ugly, yes)
* merging some methods

but none of this seems to fit well into the design. As already
mentioned, I feel that the design is okay, it is simple too hard to keep
the overview. I know, this does not sound like a good design.

Think of a neat little box of hardware, which can be used as a phone,
pda, fax machine, tv, bar code reader, mp3 player, .... etc. It is a
perfectly fine object, because all works fine, and it is easy to control
the different capabilites, and it's even possible to send the a tv
screen shot as a fax. So the box is fine, but it has so darn many
connectors (tv line, phone line, audio in, audio out, ... ). Actually, I
like all these connectors, but it is so hard to keep the overview.

Back to the code: One idea I had, was to seperate the class declaration
into different files, but I don't know how to do this in a nice way.

Any ideas?

-Sacha

Try to identify modular concepts and isolate them from others, thus
creating lots of lighter classes.

Break the class, and make it *only* a namespace. You may now break all
easily into different files. Once you have split the class into files,
you will find it is split more or less by concepts, each with its own
interface. Doesn't it sound like you may have lots of concrete
classes?

Best luck
Oct 25 '05 #2
>

Try to identify modular concepts and isolate them from others, thus
creating lots of lighter classes.

Break the class, and make it *only* a namespace. You may now break all
easily into different files. Once you have split the class into files,
you will find it is split more or less by concepts, each with its own
interface. Doesn't it sound like you may have lots of concrete
classes?


Thanks for you answer. I have difficulties to separate it into different
concrete classes. They would be so thigtly bound, that there would be a
lot of overhead to let them interact. Somehow I feel, that I rather need
different namespaces withing the class (is this possible, at all?)

-Sacha
Oct 25 '05 #3
TIT
Sacha sade:


Try to identify modular concepts and isolate them from others, thus
creating lots of lighter classes.

Break the class, and make it *only* a namespace. You may now break all
easily into different files. Once you have split the class into files,
you will find it is split more or less by concepts, each with its own
interface. Doesn't it sound like you may have lots of concrete
classes?

Thanks for you answer. I have difficulties to separate it into different
concrete classes. They would be so thigtly bound, that there would be a
lot of overhead to let them interact. Somehow I feel, that I rather need
different namespaces withing the class (is this possible, at all?)

-Sacha


Well, depends on the functionality you need from namespaces. Ordinary
namespaces can't be put in classes but you could construct a similar
concept by yourself, although not nearly as powerful as one could
wish for:

#define NAMESPACE struct

class A {
NAMESPACE Exception {
class B {};
class C {};
};
A() {
throw Exception::B();
}
};

TIT
Oct 25 '05 #4
Sacha wrote:
I work on a rather large C++ project. The design, so far, seems to be
fine. However, there is one class, where the number of methods (and less
dramtically the number of members, too) is growing and growing. I
thought of different strategies to divert the glut into 'different
functional units', like

* through inheritance
* using member objects to encapsulate the methods of the parent object
* split the class without a parent class
* using array of function pointers (ugly, yes)
* merging some methods

but none of this seems to fit well into the design. As already
mentioned, I feel that the design is okay, it is simple too hard to keep
the overview. I know, this does not sound like a good design.

Think of a neat little box of hardware, which can be used as a phone,
pda, fax machine, tv, bar code reader, mp3 player, .... etc. It is a
perfectly fine object, because all works fine, and it is easy to control
the different capabilites, and it's even possible to send the a tv
screen shot as a fax. So the box is fine, but it has so darn many
connectors (tv line, phone line, audio in, audio out, ... ). Actually, I
like all these connectors, but it is so hard to keep the overview.

Back to the code: One idea I had, was to seperate the class declaration
into different files, but I don't know how to do this in a nice way.

Any ideas?


Did you go through the Design Patterns book, and checked if any is
applicable to your specific situation? That often helps.

I'm thinking of splitting it in multiple, unrelated classes, and use
some sort of "glue" class to communicate. Pretty much like an iterator.
Only I can't tell if this is possible in your design, or even makes
sense... Perhaps give each partial 'box' a concept it models, and use
generic programming. When you speak of connectors, it makes sense to
have a seperate class for each of those subobjects having a connection
point. That way you can glue them together later on in any configuration
you'd like. Like Fax.in(Camera.out()); or something similar. I vaguely
recollect a design pattern doing just this, unfortunately I don't have
the book here atm.
--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 25 '05 #5
ben
Sacha wrote:
I work on a rather large C++ project. The design, so far, seems to be
fine. However, there is one class, where the number of methods (and less
dramtically the number of members, too) is growing and growing. I
thought of different strategies to divert the glut into 'different
functional units', like

* through inheritance
* using member objects to encapsulate the methods of the parent object
* split the class without a parent class
* using array of function pointers (ugly, yes)
* merging some methods
The first step is to examine each non virtual member function (for
virtualy functions, question the virtuality) and ask:

1. Does this particular function have to access the class's private part?
2. Does this particular function conceptually reside in the class's
encapsulation boundary?

If the answer to 1 is no, consider making the function a non member, non
friend, standalone function, taking an extra parameter originally
occupied by the this pointer.

If the answer to both 1 and 2 is yes, then consider making the function
a non member, friend function, again, taking an extra parameter
originally occupied by the this pointer.

Ideally, you should select a smallest possible set of functions to be
member functions such that all other operations can be implemented
effectively and efficiently.

If after all these the member functions are still of large numbers, you
should consider if that class is taking too many responsibility.

but none of this seems to fit well into the design. As already
mentioned, I feel that the design is okay, it is simple too hard to keep
the overview. I know, this does not sound like a good design.

Think of a neat little box of hardware, which can be used as a phone,
pda, fax machine, tv, bar code reader, mp3 player, .... etc. It is a
perfectly fine object, because all works fine, and it is easy to control
the different capabilites, and it's even possible to send the a tv
screen shot as a fax. So the box is fine, but it has so darn many
connectors (tv line, phone line, audio in, audio out, ... ). Actually, I
like all these connectors, but it is so hard to keep the overview.
Have you considered multiple inheritance? The facets you gave (phone,
pda, fax, tv, etc) have individual functions with very little
overlaping, and this is exactly what multiple inheritance is designed
for. Again, bearing too many responsibilities is not a healthy idea.

Back to the code: One idea I had, was to seperate the class declaration
into different files, but I don't know how to do this in a nice way.
There are indeed many ways. If you have succeeded in making member
functions non members, you can now group these functions and put in
elsewhere than where the class is. An example of this approach is the
standard C++ library, take a look.

If you have succeeded in separating the class into multiple smaller
classes, it is also easy to put these smaller classes in logical forms.

Any ideas?

-Sacha

Ben
Oct 25 '05 #6
On Tue, 25 Oct 2005 11:09:03 +0200, Sacha <sa**********@unibas.ch>
wrote:


Try to identify modular concepts and isolate them from others, thus
creating lots of lighter classes.

Break the class, and make it *only* a namespace. You may now break all
easily into different files. Once you have split the class into files,
you will find it is split more or less by concepts, each with its own
interface. Doesn't it sound like you may have lots of concrete
classes?


Thanks for you answer. I have difficulties to separate it into different
concrete classes. They would be so thigtly bound, that there would be a
lot of overhead to let them interact. Somehow I feel, that I rather need
different namespaces withing the class (is this possible, at all?)

-Sacha


The only way to have different "namespaces" within a class is to have
private classes.

I should try first to disassemble the class, into separate data and
functions. Then group data with the classes that use them.

After that, a cautios analysis may help group data again in lighter
classes. Of course, it may be impossible.

But I feel that a class with a fat interface shoudl not be a class at
all, but just an interface within a namespace and a lot of data within
local anonymous namespaces.

Oct 25 '05 #7
>
But I feel that a class with a fat interface shoudl not be a class at
all, but just an interface within a namespace and a lot of data within
local anonymous namespaces.


But I get difficulties if I need several objects (of the whole thing)
then, right?

-Sacha
Oct 25 '05 #8
* Sacha:
I work on a rather large C++ project. The design, so far, seems to be
fine. However, there is one class, where the number of methods (and less
dramtically the number of members, too) is growing and growing.


There are a number of common anti-patterns that produce this effect, but which
one(s) you're up against is difficult to say.

It might be that the class has too many responsibilities: try to analyze what
it's responsible for, and whether responsibilities can be grouped in other
ways than one big heap. Then delegate/move groups of responsibilities.

It might be that the class is a symptom of procedural design. In that case, a
solution is to go object-oriented. First and foremost that means that where
you have functions that know what to do with a lot of types, they should be
replaced by objects that know what functions to invoke for their own types.

It might be that the class is the dreaded "spider" that sits in the middle of
everything. In that case it should be ditched. Small classes on the
periphery of the spider's net might be (partially) rescued, or not.

It might be that the problem is at the managerial level, e.g. a constant
stream of new requirements, or interaction with end-users/client forbidden.
Changing the behavior of bad management is futile. So in that case there's
little or nothing that you, as a project member, can do for this project.

Any way I don't think it is C++ related, i.e., it's off-topic in [clc++].

Follow-ups set to [comp.programming] because I'm too lazy to find an OO or
architectural or patterns group for you -- you should have done that.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 25 '05 #9
On Tue, 25 Oct 2005 12:03:17 +0200, Sacha <sa**********@unibas.ch>
wrote:

But I feel that a class with a fat interface shoudl not be a class at
all, but just an interface within a namespace and a lot of data within
local anonymous namespaces.


But I get difficulties if I need several objects (of the whole thing)
then, right?

-Sacha

Of course, if you need various objects of the same class, then you
can´t do it that way.

Still, my feeling is that no class should have a fat interface, so
this class should be disintegrated into atomic classes and after that,
reintegrated into a whole hierarchy. Probably you don´t need all
interface in all instances of the class, so you should look to create
various leaf classes to do the business.

Of course, all this is pure divagation, having no details of the fat
class.
Oct 25 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mario Rosario | last post by:
Hi, I am getting an error which I don't understand why. I have an object Item which I serialize within a servlet and send it across to an applet but I get this run time error...
2
by: vincent delft | last post by:
Sorry if my question is stupid. I've missed something with classes. Can you explain the following ? class test: var1=1 var2=2 res=var1+var2 t=test() print t.res
6
by: Andreas Palsgård | last post by:
Hey.. i want to write a class object containing both strings and integers, to a file on disk, and be able to read it properly again at another time. I made the code below... it doesn't work, but...
1
by: David Goodyear | last post by:
At the moment im experimenting with ideas in C++ and would really like to solve the following, please please help. Sorry i dont even know what the subject is this would come under? :( Sorry if...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
8
by: Ares Lagae | last post by:
When adopting the coding style of the standard C++ library, you often run into naming problems because class names are lower case, and member functions do not have get/set prefixes. For example:...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
1
by: elsa | last post by:
hi everyone, i have a qustion that i cant solve in drwaing a uml class diagram and this is the case: ------------------------------------------------------------------------------------- In a...
0
by: Academia | last post by:
The Help Class example requires only a simple copy, paste into a form and run. Which I did. For it to relate to what I am trying to find out I set the "Help Navigator) combobox to "Topic" I...
3
by: fortysixfish | last post by:
Hi I've been learning more and more on C/C++ as time goes on. Today I wanted to play around by creating a class that makes it easier to use Windows Tokens. I get stumped though when trying to pass a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.