Sorry if this is a silly question :-)
Background
I have legacy code that looks like this:
struct {
int field1;
int field2;
int field3;
int field4;
... many many fields
} myStruct;
while (something) {
initialzationFunction(&myStruct);
// ...change fields of myStruct and do stuff.
}
Each iteration of the while loop needs that myStruct will be initialized to something, lets say zero. initialzationFunction initializes all the fields of myStruct to zero.
The question
Is it good to keep initialzationFunction inside the while loop, or is it better to call it once before the loop, and let the programmers initialize what they need "by hand" if they happen to change this code.
edit: Unfortunately myStruct is a global variable, so making it an automatic variable isn't an option, unless I want to pass it as a parameter to tons of legacy functions that use it.
What I think
- just calling initialzationFunction() will prevent bugs in case someone modifies the code and forgets to later initialize myStruct.
- It might be more informative to see what specific fields are initialized.
- If only a few fields are modified later in the while loop, calling initialzationFunction() that inits all of the fields is redundant.
What would you do?