>Am I write[sic] in thinking this?
Makes sense to me. Though structures are best used for closely related data rather than just a variable dump. If the relationships aren't there, you may need to consider how you're structuring your program and redesign if necessary to streamline the flow.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Don't flatter yourself with the program of 1000 lines: it is a small program (max 2 days on implementation ;)). So the true problem is not a number of variables per se.
It's not a common rule, but a well-designed function has 1-5 parameters. If it has 10 or more parameters then functional decomposition was made badly. There is the better way to do it. Another source of troubles in that case is a bad data (or object) model design.
For example, in a recent thread on this forum the author passed 10 arguments presented CPU emulator registers. In actual fact all these variables are corresponded to the only artifact - CPU register file. It's the only array variable. Moreover, we get much more clear and compact code with register file abstraction.
Never group variables into structures on formal basis (to reduce parameter/arguments lists). Every structure is a data abstraction. If you don't know what's this abstraction, don't group such variables.
So too many variables feeling is an alarm bell: it's a good time to come back for redesign and/or refactoring.
Don't worry: it's absolutely normal situation...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
>It's not a common rule, but a well-designed function has 1-5 parameters.
I'd say it's fairly common to restrict the number of parameters with a guideline. I usually recommend that you seriously reconsider what you're doing when you exceed three, and more than seven local variables suggests a need for refactoring.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
The number of parameters best practice depends on the problem domain. For example, there are more parameters in routines of scientific graphics or linear algebra packages (not only from early Fortran heritage ;)).
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
>The only thing I can think of in terms of redesign is
>to break the functions down into itty-bitty pieces
That's often no better than one monolithic function. To properly streamline your code, you may need to redesign the entire structure such that each function has a well-defined purpose without tasking your brain in terms of excess variables or tons of unnecessarily small functions. Without seeing your code, I can't really offer better advice.
>I've already made 15 functions in this 1000 lines of code (trying to impress you again).
In terms of size, I work with code bases that have millions of lines on a daily basis. 1000 lines won't impress me unless it's wicked cool code from beginning to end. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
My 5 second solution[1] is to encapsulate the event stuff in one structure and higher up, a list of events and the channel info in another structure. For example:
struct event {
int *wavelength;
int *seconds;
int *samplecount;
};
struct channel {
char *channel;
int selected;
int eventcount;
event *events;
};
The reserve parameter strikes me as sufficiently global to store as a global variable (make it static if you can so it's restricted to a single file). Doing all of this drops your parameter list to three (assuming my 5 second assumptions about your data are correct):
void InsertEvent(struct channel *channel, HWND EDC_Wav, HWND EDC_Sec);
You can further shrink it by encapsulating all of the Windows-specific data that's related in a structure as well:
void InsertEvent(struct channel *channel, struct EDC_Data *data);
[1] I spent five seconds thinking about it.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
You could use arrays as well.
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
Why would you add an unnecessary level of abstraction (creating the structures) if you don't need to? The point of a structure is to group related data, not to shove **** together in order to reduce # of parameters
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
I agree with BestJewSinceJC (also see my previous post ;) ).
IMHO it seems that you don't understand the meaning of Narue's improvisation (may be I'm wrong). It was an attempt to invent proper abstractions (event, channel) for totally unstructured low-level code with chaos of unrelated variables. Parameter lists reducing is an inevitable result but not the main goal of this approach.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348