Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meaning of result returned by Cohesion #2

Closed
Marlysson opened this issue Aug 20, 2016 · 1 comment
Closed

Meaning of result returned by Cohesion #2

Marlysson opened this issue Aug 20, 2016 · 1 comment

Comments

@Marlysson
Copy link

Marlysson commented Aug 20, 2016

I ran the lib cohesion over my code and return me the result.. what the lib analyze ?

 Function: __eq__ 1/3 33.33%
    Function: __init__ 1/3 33.33%
    Function: __repr__ 2/3 66.67%
    Function: change_bit 2/3 66.67%
    Function: is_valid 1/3 33.33%
    Total: 46.67%
  Class: Hamming
    Function: __init__ 1/13 7.69%
    Function: __repr__ 2/13 15.38%
    Function: bits_verified_by 3/13 23.08%
    Function: calculate_parity 2/13 15.38%
    Function: check 4/13 30.77%
    Function: divisors 1/13 7.69%
    Function: encode 8/13 61.54%
    Function: fix 3/13 23.08%
    Function: is_power 2/13 15.38%
    Function: wrong_position 4/13 30.77%
    Total: 23.08%
@mschwager
Copy link
Owner

You can read up on the concept of code cohesion here.

Basically, it measures how well a class is structured as a logical unit. This tool tells you what percentage of instance variables a class method uses. So if class Cls has instance variables self.instance1 and self.instance2, and member function func only uses self.instance1 then it will get 1/2 or 50% cohesion:

class Cls(object):
    def __init__(self):
        self.instance1 = 5
        self.instance2 = 6

    def func(self):
        return self.instance1 + 10

The rationale being that classes with member functions that use a large percentage of the instance variables available have strongly related functionality. Classes that have many instances variables that only get used in a few places would probably be better suited as multiple, different classes.

High cohesion is when you have a class that does a well defined job. Low cohesion is when a class does a lot of jobs that don't have much in common.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants