I am engaged in scientific image processing in MATLAB, which requires solving strange optimization problems with many parameters that are used by the "experiment" function, which has many different "auxiliary functions" that use various subsets of parameters. Passing these parameters around is a pain, and I would like to have an elegant extensible solution.
My preferred solution would be a "relative global" variable - common to the "main" workspace of functions, as well as any sub-functions that the main function calls, and which indicate that they want to use this variable. But a relative global variable will not exist outside the function declaring it, and will disappear after the return of the main function.
The code would look like this if there weren’t many more experiments, each of which used the parameters differently:
function y = experiment(p1,p2,p3,...) % declare relative global variables. % these will not change value within this experiment function, % but this experiment will be reused several times in the calling function, % each time with different parameter values. relative global p1, p2, p3 ... % load and process some data based on parameters ... % call the helper + return y = experiment_helper(y,z); end function y = experiment_helper(y,z) relative global p1, p3 %% do some stuff with y, z, possibly using p1 and p3, but not changing either of them. ... end
I understand that you can get the desired behavior in several other ways - you can pass parameters to subfunctions caused by the experiment, you can put parameters in the parameter structure and pass them to subfunctions, and so on. The first is terrible because I have to change all the arguments of the subfunctions every time I want to change the use of parameters. The second one is all right, but I have to include the structure prefix every time I want to use variables.
I believe that the “right” solution to my problem is to use parameter structures similar to what Matlab does in its own optimization code. I just wonder if there is a smoothing method that does not suggest that I type "paramStruct.value" every time I want to use the value parameter.
I understand that relative global variables can cause all kinds of nightmares for debugging, but I do not think that they would be necessarily worse than those that were caused by existing global variables. And they, at least, would have more localization than unskilled global ones.
So, how does a real programmer deal with this problem? Is there an elegant, easy to create and maintain design for this kind of problem? Can this be done in Matlab? If not, how would you do it in another language? (I always feel guilty for using Matlab, because it does not contribute to good design. I always want to switch to python, but I can not justify rethinking things and migrating the code base - unless it makes me much faster and better than a programmer for several weeks, and Matlab-specific tools, such as wavelet and optimization toolbars, can be easily or easily found or duplicated in python.)