I am researching cohesion topic and found out that some claim TCC metric should only include public methods, some other sources claim all methods. Would it be wrong to use either approach? Why private methods should be excluded, as some suggest?
1 Answers
I would think you need to consider both the public methods and the private methods as follows:
The metric is based on evaluating the cohesion of among the public methods — the interface to the class as seen by external clients, and doesn't report the cohesion of the private internals as part the interface that a client consumes (as clearly they are not part of that).
However, during the computation of the cohesion among the public methods, you must consider cohesion among the instance variables that are accessed not just directly from the public methods, but also by private methods invoked by the public methods.
Imagine we have a class whose public method simply calls a private method:
class Foo {
private bool _state;
private void _setState ( bool choice ) { _state = choice; }
public void Set() { _setState ( true ); }
public void Reset () { _setState ( false ); }
}
Now, TCC concerns itself with the cohesion among Set
& Reset
, but not _setState
.
However, in considering Set
and Reset
we need to take into account that they call _setState
. If we don't consider the private methods in the static call graph for the public methods, then they will appear as failing to have cohesion with the each other, when in fact, they are not lacking cohesion: they interact over the _state
instance variable.

- 33,282
- 5
- 57
- 91
-
I would think so too, but the original definitions do mention only public ones..or rather "visible ones" - http://www.aivosto.com/project/help/pm-oo-cohesion.html#TCC_LCC. – John V Feb 04 '18 at 16:54
-
Ah, just found the very original paper: https://webcache.googleusercontent.com/search?q=cache:3ejuHLmuoUMJ:https://pdfs.semanticscholar.org/672e/de6e3e600eafd84036a0b983b88e481ac626.pdf+&cd=1&hl=cs&ct=clnk&gl=EN – John V Feb 04 '18 at 16:58
-
It does mention call trees, though, and that should be taken to include private methods: "For the call trees we consider all procedures inside the class, including Private procedures. If a call goes outside the class, we stop following that call branch." – Erik Eidt Feb 04 '18 at 16:59
-
But only in the other one, LCC. The paper is a bit more specific in section 2.2: "A client of a class can access only visible components ofthe class. Class cohesion refers the relatedness of vis-ible components of the class which represent its func-tionality. Class cohesion is the degree that those com-ponents are related. In our model of class cohesion,invisible components of a class are included only indi-rectly through the visible components. Therefore classcohesion is modeled as the MIV relations among all vis-ible methods (not including constructor or destructorfunctions) in the class." – John V Feb 04 '18 at 17:04
-
Seems consistent in saying that we consider the public interface as per cohesion, but we must also consider effects of implementation details (however indirect, e.g. via private methods) when we analyze the elements of the public interface. – Erik Eidt Feb 04 '18 at 17:10
-
To me it is still a bit uclear (I guess being a non-native speaker contributes to that). Also, TCC considers only public ones and LCC is extended by those indirect, i.e. including those that goes from public to private, for example. But call private-private is out of scope. – John V Feb 04 '18 at 17:12