On Thu, Aug 11, 2005 at 01:25:03PM +0100, Stuart Bailey wrote:
If you use a struct to contain the configuration you avoid many of the nasties of globals, however initialising the values is messy IMHO. You get stuff like:-
In a header file:-
struct ClockConfig { int xsize; int ysize; char bgcolor[COL_SIZE]; char fgcolor[COL_SIZE]; bool iconic; ... ... ... etc. };
and then, in the code somewhere:-
struct ClockConfig clockCfg = { 54, 54, "#123456", "#FFFFFF", FALSE, ... ... .. etc. };
Which, to my mind, is truly horrible and difficult to maintain because you have to be very careful to maintain the one-to-one correspondence between the initialisation values and the struct. Commenting will of course help but it again represents duplicated information which really shouldn't be necessary.
Yes, but as far as I can remember (and I may be verging on C++), you can have functions within a struct. Therefore, you could create an initialisation function, with well named parameters (with defaults), that will setup the struct for you.
Does this actually prevent the duplication? You have your struct, with the variables, and you have your function with a parameter list (with defaults) which simply duplicates the variables, same different!
Or have I misunderstood?
Although you will still have a long parameter list. Unfortunately, I can't remeber the syntax - and I can't find any of my C books to reference. But basically, a C++ class is essentially a structure whose members are by default private rather than public as in structures.
I'm quite happy to change to C++ but I can't see how it helps for this particular problem. Any class type approach that I can think of still duplicates everything effectively. Unless you can initialise variables like this:-
class Clock : public { public: int xsize = 54; int ysize = 54; char bgcolor[COL_SIZE] = "#123456"; char fgcolor[COL_SIZE] = "#FFFFFF"; bool iconic = false; }
I know this isn't really what OOP is about (I'm not hiding the data) but if I could do this it would be an excellent way to manage this particular problem.