How to organize code well in root class?
So, I've been writing this software with C++ for some time. It has gui and
a gui has access to master class object, that object is responsible for
whatever task is made by the gui (when user clicks something, gui just
calls a method of that object).
Now, I don't know if this is the best way. I've never worked in any coding
company in my life, but I've came into some problems.
Actually, master class doesn't do anything. It's mostly a wrapper for
other objects located inside that class. So, for instance:
class Master {
public:
void writeSomethingToFile(const char *filename,std::string& text);
...
}
under the hood:
class Master {
...
private:
FileWriter *_writer;
}
void Master::writeSomethingToFile(const char *filename,std::string& text) {
_writer->write(filename,text);
}
So, the master class doesn't do the writing itself, it just throws the
task for writer class which should do the job.
The master class has a lot of objects like writer class, so, as far as gui
is concerned, master class is capable of ANYTHING it ever needs in
program.
However, code get's kind of clunky in master class, since it contains all
these wrapper methods it has VERY MANY METHODS.
Since it uses all these other classes, whenever I change any header of the
class master class uses the .cpp file has to be recompiled too (not too
bad for i7 processor, but I'd rather avoid it).
What now I've been using is very primitive, and by no means I defend this
way:
class Master {
public:
// FILE CHANNEL
void writeToFile(...);
void deleteFile(...);
// FILE CHANNEL
// ARITHMETIC CHANNEL
void addNumbers(...);
void multiplyNumbers(...);
// ARITHMETIC CHANNEL
...
}
I would literally, separate things I call "channels" with comments so I
could understand what belongs to what. Now, for gui, this may not be that
bad, since everything is grouped. However, when I go further developing
this class adding new inner classes to it and wrapping more methods, stuff
becomes clunky and what most bothers me, it's not rock solid. I mean, If I
would take one method from commented "channel" and put it in the other,
would I really see the difference?
I thought about couple of solutions. I've never tried it but I may create
memberspaces:
class Master {
public:
namespace file {
void writeToFile(...);
void deleteFile(...);
}
namespace arithmetic {
void addNumbers(...);
void multiplyNumbers(...);
}
...
}
This solves problem from my side, as a developer of a class, but, for gui
they now have to call methods like
master->arithmetic.addNumbers(...); // My syntax could be wrong here
// never used memberspace, corrections are appreciated
And now, as my project is 'kinda' swinging at the moment, that would mean
modifying A LOT OF CODE.
Another solution I thought about was constant inheritance from class to
class where master class in file focuses on one channel:
class Master_fileChannel {
FileHandler *_fileHandler;
void writeToFile(...);
void deleteFile(...);
}
...
class Master_arithmeticChannel : public Master_fileChannel {
ArithmeticUnit *_arithmeticUnit;
void addNumbers(...);
void multiplyNumbers(...);
}
and so on, until I inherit every "channel". This would be more solid than
the original state, and make files a lot shorter than current .cpp file
for this class. But, I would still have issue that I may use duplicate
method names and would have to make up more and more clunky names for my
methods (i.e. addNumbers(..); addThreadNumbers(..);
addThreadNumbersSeparately(..);
So, what would you suggest me to do here? I can build up that master class
to infinity, but I'm sure there's a better way. How these things are dealt
with in real decent sized code bases? Can I refactor code quickly across
all project to do these drastic changes without a sweat?
No comments:
Post a Comment