Skip to content

Shouldn't we expand calls to class methods when evaluating cohesion? #8

@portusato

Description

@portusato

Suppose you have this module:

class Data(object):
    def __init__(self, data):
        if not isinstance(data, list):
            raise ValueError('Data: Input arg must be list')
        self.data = data

    def get_value_at_start(self):
        return self.get_value_at_index(0)

    def get_value_at_index(self, index):
        return self.data[index]

data = Data([1, 3, 5, 7])
print 'Data 0: {}'.format(data.get_value_at_start())
print 'Data 2: {}'.format(data.get_value_at_index(2))

cohesion gives 0.00% for Function: get_value_at_start. IMO this is incorrect. By the definition you have in your documentation (extracted from wikipedia): "[...] cohesion refers to the degree to which the elements of a module belong together". Getting a cohesion of 0% would mean that the method does not belong to the class. However, it does; it's only that we're avoiding code repetition. If the function did 'return self.data[0]' we'd get cohesion score=100%, but this would go against good practices, because if later we change something in how we access data points, we need to change it in two places.
What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions