Best way to initialize different parameters based on the machine in which I run the code
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone,
I am currently in a stage where I would like to have my code modularized and following software-engineering techniques to make it reusable and understandable. In particular, I run my code either in my laptop and in an external server.
My goal is to have the main part of the code exactly the same in the laptop and the server, but different initialization parameters in the two case (in the server I will increase the # of iterations for instance and other variables). What is the common practice to do so, apart from clearly an if-else statement in the main?
I was thinking of an initialization file (like a JSON) in the laptop and the server, which is different and I just need to modify the values. Or a Matlab function which initializes the variables, but still, it would have an if-else statement.
Other suggestions? Keep in mind I might to want to extend the algorithmic part and introduce new parameters in the future.
Thanks
0 Kommentare
Antworten (1)
Jan
am 20 Mai 2022
Bearbeitet: Jan
am 20 Mai 2022
A drawback is, that these settings are hidden in the User folder. Do not store "too" specific parameters there, but implement "important" settings in code, which is shared with all versions of your software. Example:
function Settings = MachineDependencies(doSetup)
if nargin < 1
doSetup = false;
end
if ispref('ANatali', 'Prefs')
Settings = getpref('ANatali', 'Prefs');
else
Settings.isServer = [];
end
if isempty(Pref.isServer) || doSetup
% Open a question dialog to clarify, if this is a "server"
Settings.isServer = givenAnswer;
setpref('ANatali', 'Prefs', Settings);
end
end
And inside the code:
function YourCalculations
Settings = MachineDependencies();
if Settings.isServer
iterations = 100;
else
iterations = 10;
end
...
end
Then all relevant decisions are visible inside the code. If you store the number of iterations inside the preferences, reading the code does not reveal all relevant details.
A drawback ist that setpref stores the settings for a specific user. If you want a dependency to the machine, you need another option, e.g. the JSON file inside a specific folder. If you implement this, it would be straighter to store machine and user dependencies at the same location. Take a look into the code of setpref and prefutils to write your own implementation. Then you can implement an additional logic to decide if the user or the machine setting has the higher precedence.
I've written such a tool to manage preferences on user, machine, lab and global level, e.g. to switch the language or error messages and for default values for the physical setup. "Global" level means e.g. the version number of the newest version taken from a web service, even if it is not installed locally. The actual preferences are as lean as possibel (logical flags, name of the language, etc.) and when I rollout a new version, the preferences files are not shipped.
Siehe auch
Kategorien
Mehr zu Web Services finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!