I often want to run a particular function only once, but I need to call it from another function that is called repeatedly. For example, taking a picture of something for later use. I usually do this by setting a global boolean.
I wonder if the way I do this is actually the best way?
I seem to remember that global variables are bad, and global Boolean variables are even worse!
In any case, this is how I usually run a single method only once:
In my original set of variables ...
private var myStatus:Boolean = false;
Then in a function that is often called ...
if (!myStatus) { doMyFunction(); myStatus = true; }
It seems pretty logical to me, but is that right?
UPDATE Based on what I learned from your answers, instead of checking the global boolean variable, I now first check if the XML node exists (I store the images within the XML before any write to the disk occurs), and if it not so, i will add a new node with base64 encoded data. I still set the boolean flag so that I can later overwrite the blank image with custom edited image data if necessary. It works great. Thank you all for your help!
Now I also feel more comfortable using this particular (unsafe) system in certain situations.
language-agnostic loops boolean
defmeta
source share