1

I want a function to only run one time. I found this question here on SO and the highest voted examples redefine the function and then execute the code that is supposed to run once. I'm sure there is a good reason for doing that instead of running the code and then redefining the function but I can't figure out what that would be. My first instinct would be to only redefine the function after the code is run since that seems "safer" because the code would have to have run before being redefined.

Can someone explain why convention seems to be to redefine first?

ironstone13
  • 3,325
  • 18
  • 24
WD-40
  • 319
  • 2
  • 7
  • 18
  • Please add the relevant language tag to your question. – Oliver Charlesworth Dec 30 '17 at 20:03
  • If you are talking about `javascript` then there is no difference, because of there is no threading - only a single event loop, your function cannot be called at the same time from multiple threads, but only sequentially. Read more about event loop in js https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop – ironstone13 Dec 30 '17 at 20:27
  • So it's to prevent it from running multiple times in multi-threaded environments. Makes sense! Yes, I am using JavaScript but I thought it was a general question so I left that off. I've added the JS tag due its widespread use even though the question applies more broadly. Thanks for the reply. – WD-40 Dec 30 '17 at 20:48

2 Answers2

0

Basically if you want to avoid that the function is "called" twice, disabling the function immediately after the first call is the safest thing to do. When nothing else can happen between the call and the disabler, then nothing unexpected can happen either. If you were to run the code that should be executed only once first, various things might happen:

  • the code calls the function recursively, or through a callback - you should consider reentrancy
  • the code does throw an exception
  • the code returns early

In any of these cases, and possibly more, the disabling code is not reached and a second call could be made.

Notice also that replacing the function is not enough. A caller could easily have created an alias variable that still holds the original function, unaffected by setting some other variable to a noop function. You always need to combine this solution with the "static boolean variable" approach from the accepted answer, unless you control the calling code and know what it does.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
-1

Looks like the main reason is for multi-threaded languages. If a function is supposed to run only once and it gets called a second time before the first instance is complete, that would result in being called more than once. Redefining it first means that the function can't run twice.

WD-40
  • 319
  • 2
  • 7
  • 18
  • JS is not multithreaded. Also in a multithreaded language doing the disabling as soon as possible might decrease the chances of a race condition, but for a proper solution one would need a semaphore or atomic operations. – Bergi Dec 30 '17 at 22:09
  • I didn't say that JS was multi-threaded. As stated in the comment to the original question, for JS, the order wouldn't matter. I'm answering my own question so that anyone else finding this question will have the rationale for why redefining the function would be done first. – WD-40 Dec 30 '17 at 23:39