title: Is a bad practice to declare static variables into functions/member functions?
body: <p>Recently a fellow worker showed to me a code like this:</p>
  
  <pre><code>void SomeClass::function()
  {
      static bool init = false;
  
      if (!init)
      {
          // hundreds of lines of ugly code
      }
  
      init = true;
  }
  </code></pre>
  
  <p>He wants to check if <code>SomeClass</code> is initialized in order to execute some piece of code once per <code>Someclass</code> instance but the fact is that only one instance of <code>SomeClass</code> will exist in all the lifetime of the program.</p>
  
  <p>His question were about the <code>init</code> static variable, about when it's initialized. I've answered that the initialization occurs once, so the value will be <code>false</code> at first call and <code>true</code> the rest of its lifetime. After answering I've added that such use of static variables is bad practice but I haven't been able to explain why.</p>
  
  <p>The reasons that I've been thinking so far are the following:</p>
  
  <ul>
  <li>The behaviour of <code>static bool init</code> into <code>SomeClass::function</code> could be achieved with a non-static member variable.</li>
  <li>Other functions in <code>SomeClass</code> couldn't check the <code>static bool init</code> value because it's visibility is limited to the <code>void SomeClass::function()</code> scope.</li>
  <li>The static variables aren't <em>OOPish</em> because they define a global state instead of a object state.</li>
  </ul>
  
  <p>This reasons looks poor, unclever and not very concrete to me so I'm asking for more reasons to explain why the use of static variables in function and member-function space are a bad practice.</p>
  
  <p>Thanks!</p>