-
program design question
I have this program, dealing with sockets and threading (which is not of concern). There is a main loop inside this program. Currently, there is a lot of messy code inside here. I'd like to sort this out into a few functions, declared in a header file. The problems are:
1. The functions I've wrote use lots of variables from the main program (is there a way to fix this, save for passing 3-6 arguements into each function? most of the variables are already global in the main program)
2. The functions have certain circumstances when they need to break or continue the original main loop from the main program. .. Heh.. Any ideas?
I guess I COULD just make a long, ugly, unorganized, cryptic, single-filed program... Or include ALL the function declarations inside that source (to fix the variable problem).
Are there any tips/tricks to get around situations like this? Thanks for any input...
-
-
echo major,
designing with classes will give you several advantages: clear code, portability, security.
-
1. You can pass-in a pointer to a structure (or class) that contains the needed variables (or accessor functions). This is quite common when you need to pass-in several variables, especially when the same variables are used by many different functions.
2. I'm not sure I understand, but maybe you can use the return value from the function. i.e. if 0 is returned, continue, if 1 is returned, break, etc. But, I suppose some of the functions already have a return value used for something else. You could use a pointer or reference to some status variable.
Unfortionately, the above approaches might requre you to restructure and rewrite the whole program! :(
-
Thanks for the ideas.. I think I'll get it running. Classes don't really seem to fit the problem, however I certainly will think about it more.
DougdBug - The structure idea is a very good one. I've already done both clauses in #2; both worked/didn't work. It's hard to say; I'm going for another rewrite tomorrow.
Thanks for the help!