When a global variable is justified
Afficher commentaires plus anciens
I use few global vars in my app. The Code Analyzer suggests to get rid of them, but this would involve--in my view--more complicated coding. I'd like to learn the best programming practice in this regard and maybe a fix is simpler than I think.
I use global vars for two main reasons.
First, I need a debug flag in all my functions. This is to spit out debug info if app is run with a debug flag (on the command line). To fix this, I would need to intorduce an additional input variable in all definitions and calls. To me, this is more ugly compared to a line of global definition inside each function. But in general this is easy to fix (introducing addional input argument).
Second, this is more difficult, I think. One function logit() writes log info in both GUI text box and in the file on disk. Thus, it needs FID of the file, and a text box handler, plus another flag which tells if the app runs in batch mode (w/o GUI) or not. For the latter, probably there is a way to find out batch or not via a GUI ask, as well as it shall be possible to find the text box handler via a GUI ask, i.e. w/o explicit handler in a varable. So, I guess it is possible to fix my logit(), where it would have more logic to find out: batch or not, find the needed handler for the text box, and have the file FID in a persistent local variable, maybe (if other funcs do not write to this file).
Thanks
5 commentaires
Rules are there for breaking... once you understand why that rule exists :)
Debug mode and logging meta-data seem like reasonable uses of globals: you have justified why they should be used, and presumably this fits into your overall design requirements, etc. Unreasonable uses would be passing actual data around, etc. Some people would suggest that you write a small function or class to hold such global meta-parameters, but that really boils down to much the same thing, although it has the benefit that perhaps you can perhaps more easily trace who calls or changes it.
But you mention that you are using an app: why not just use the app properties? That might be a suitable solution if most of the functions are called from the app itself. Not sure how that would work in batch mode w/o GUI though...
Otherwise write a comment in the code saying why globals are used, then right-click and hide that Mlint message.
Rik
le 4 Juil 2023
In cases like this, I am generally sharing a data structure anyway, so it is fairly trivial to add a field to that struct. No complicated coding required.
Once you are talking about an app, that implies a GUI. That means you can use guidata or AppDesigner properties. Again, no globals required. For a CLI-mode the solution is a bit more tricky, but what implementation would work best, depends on what you can assume to be available.
Can you assume there will only ever be a single instance of either the GUI or the CLI? In that case I would suggest using a persistent variable. You can load the persistent variable and use the type to deal with GUI vs CLI: if the persistent is a struct, return it (or store it), if the persistent is a (ui)figure handle, load and return the guidata.
Valtar
le 4 Juil 2023
Valtar
le 4 Juil 2023
Stephen23
le 4 Juil 2023
" if something wrong happens to it, nobody will get hurt"
The important question is: if something goes wrong with the global, can it in any way (silently) affect the data being processed? If yes, then using globals is a risk. Otherwise... you could consider the global benign.
Réponses (3)
To fix this, I would need to intorduce an additional input variable in all definitions and calls.
No, you could add the debug flag as an app property. Then, it would get passed around to all the callbacks and class methods as part of the app object that gets passed to them automatically.
9 commentaires
Valtar
le 4 Juil 2023
Matt J
le 4 Juil 2023
Are you using appdesigner? If you're using appdesigner, you're using OOP. You can add a property in the Code Browser, which is available in the Code View.

Valtar
le 4 Juil 2023
Valtar
le 4 Juil 2023
Matt J
le 4 Juil 2023
If your callbacks are nested, then just make the debug flag an externally scoped variable in their parent workspace. As for logit(), convert it to a nested function as well, and it will have access to everything the callbacks do.
Valtar
le 4 Juil 2023
You can make wrapper for logit which will use the externally scoped variables. You can use the wrapper within your GUI as a local, specialized version of the general function.
function guiMain
debugFlag=true;
...
function logitLocal(varargin) %wrapper
if debugFlag
logit(something)
else
logit(something_else)
end
end
end
Image Analyst
le 4 Juil 2023
0 votes
@Valtar by not using app designer because you think it gives you better control, well, I take issue with that. I'd like to see some example of that. But by doing all the tedious things of setting up callbacks, etc., I think you're actually creating more work for yourself, or you just end up with a simple, more basic app than you could otherwise if you'd have used App Designer.
See the FAQ on globals: https://matlab.fandom.com/wiki/FAQ#Are_global_variables_bad?
Globals create variables in the global workspace, which can stay there until you shutdown MATLAB or call "clear globals". This can cause problems if you end up using global variables with old, no longer appropriate, values in your new runs. With app designer the globals are all internal and don't live beyond the running of your app.
However you get the effect of globals with app designer by creating and using a property of the app object. Any function in there can automatically see app.varName (or whatever you call it) without you having to explicitly say "global varName" inside every function that needs to use that variable.
Jan Kappen
le 3 Sep 2024
0 votes
What about a global logger class? You set select the log-levels at creation time.
4 commentaires
Rik
le 4 Sep 2024
Why don't the other anwers work for a logger? I don't really see why they wouldn't.
Jan Kappen
le 4 Sep 2024
Modifié(e) : Jan Kappen
le 4 Sep 2024
Because there are logging classes existing - isn't it easier to just use an existing, tested, and capsuled module instead of messing around with global variables?
Rik
le 4 Sep 2024
Ah, I thought you meant people could use a global to store an instance of a logger class, not that an instance of a logger class could replace the use of a global.
Jan Kappen
le 4 Sep 2024
Ah understood. Sorry that was misleading. I was thinking of a singelton logger class, similar to logging — Logging facility for Python — Python 3.12.5 documentation, or Log4Matlab - File Exchange - MATLAB Central (mathworks.com), or mathworks/advanced-logger: Configurable and extensible logging for MATLAB applications (github.com)
Catégories
En savoir plus sur Large Files and Big Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!