5

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 Answer 1

2

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.

6
  • I would think so too, but the original definitions do mention only public ones..or rather "visible ones" - aivosto.com/project/help/pm-oo-cohesion.html#TCC_LCC.
    – John V
    Feb 4, 2018 at 16:54
  • Ah, just found the very original paper: webcache.googleusercontent.com/…
    – John V
    Feb 4, 2018 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 4, 2018 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 4, 2018 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 4, 2018 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.