title: LCOM4 interrogation about way to calculate
body: <p>Recently, I encountered a sementic issue in the way of calculating the LCOM4, a metric used to find how the methods and the properties of a class are cohersive.</p>
  
  <h1>Introduction</h1>
  
  <p>LCOM4 is "the 4th method of calculating the Lack in Cohesion Of Methods", and has been described by Hitz and Montazeri (<a href="http://www.isys.uni-klu.ac.at/PDF/1995-0043-MHBM.pdf" rel="noreferrer">http://www.isys.uni-klu.ac.at/PDF/1995-0043-MHBM.pdf</a>) and is currently the best way to define how many responsabilities a class own.</p>
  
  <p>I'll try to not use a specific development language because my question is for all OOP languages.</p>
  
  <p>Let me basically explain how it works to people who doesn't know, with a default algorithm:</p>
  
  <pre><code>Class Foo {
      property a,b
  
      function f1() { this.a = 1 }
      function f2() { this.f1() }
      function f3() { this.b = 3 }
  }
  </code></pre>
  
  <p>This class have two flows:</p>
  
  <ul>
  <li>attribute a is shared by f1() and f2()</li>
  <li>attribute b is shared by f3()</li>
  </ul>
  
  <p>So the LCOM4 of Foo is 2.</p>
  
  <p>Let's change for example the function f2() to also share the property b.</p>
  
  <pre><code>Class Foo {
      property a,b
  
      function f1() { this.a = 1 }
      function f2() { this.f1(); this.b = 1 }
      function f3() { this.b = 3 }
  }
  </code></pre>
  
  <p>Now this class only have one flow:</p>
  
  <ul>
  <li>both attributes a and b are shared by f1(), f2() and f3().</li>
  </ul>
  
  <p>This means the LCOM4 of Foo is now 1.</p>
  
  <p>LCOM4 = 0 or LCOM4 = 1 means the class has none or only 1 responsability, which is what every developer must want for their classes, as they respect the <strong>S</strong> of the <strong>S</strong>OLID good practices.</p>
  
  <p>You can find more information with graphs here: <a href="http://www.aivosto.com/project/help/pm-oo-cohesion.html#LCOM4" rel="noreferrer">http://www.aivosto.com/project/help/pm-oo-cohesion.html#LCOM4</a></p>
  
  <h1>My question</h1>
  
  <p>Let's imagine you wrote a class like this one:</p>
  
  <pre><code>Class Bar {
      property a
  
      static function build() { return new self }
      function Bar() { this.a = 5 }
  }
  </code></pre>
  
  <p>...and of course, when doing <code>new self</code>, I create a new instance of Bar built using the method <code>Bar</code> declared.</p>
  
  <p>Based on the Hitz and Montazeri works, what is the LCOM4 of my class <code>Bar</code>?</p>
  
  <p>Lots of metrics tools I use are saying LCOM4=2, but to me, the class only have 1 responsability and so its LCOM4 must be 1.
  Also, even if it's not really explicit, both methods <code>build()</code> and <code>Bar()</code> must belongs to the same graph of functions as <code>build()</code> is calling <code>Bar()</code> (well, I know, it's calling upon another instance, but even if it's not the same object, it's the same class).</p>
  
  <p>What is your opinion on this?</p>
  
  <p>Does anyone have the answer about how to deal with this kind of classes? (I read lots of documents by Hitz and Montazeri but I probably miss some)</p>
  
  <p>If there's no answer about it, can we improve the way of calculating the LCOM4 to make it closer to the number of responsabilities of a class?</p>
  
  <p>BTW, my case about this is in PHP, but I think this issue concerns also all others OOP languages.</p>
  
  <p>Thank you guys,</p>