{"id":13501875,"url":"https://github.com/mschwager/cohesion","last_synced_at":"2025-08-24T13:13:21.872Z","repository":{"id":60721979,"uuid":"65501724","full_name":"mschwager/cohesion","owner":"mschwager","description":"A tool for measuring Python class cohesion.","archived":false,"fork":false,"pushed_at":"2024-12-09T15:09:55.000Z","size":112,"stargazers_count":239,"open_issues_count":7,"forks_count":5,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-31T23:33:59.589Z","etag":null,"topics":["class","code","cohesion","flake8","flake8-plugin","lint","linter-plugin","measure","module","oop","python","quality"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mschwager.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-11T21:09:32.000Z","updated_at":"2025-03-31T22:10:03.000Z","dependencies_parsed_at":"2022-10-03T20:31:20.146Z","dependency_job_id":"250db079-51ee-4273-96eb-c8d0313617b4","html_url":"https://github.com/mschwager/cohesion","commit_stats":{"total_commits":84,"total_committers":4,"mean_commits":21.0,"dds":0.0357142857142857,"last_synced_commit":"321408dc532b8cd5b6ce0d059743140faf630e20"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschwager%2Fcohesion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschwager%2Fcohesion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschwager%2Fcohesion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mschwager%2Fcohesion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mschwager","download_url":"https://codeload.github.com/mschwager/cohesion/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247755560,"owners_count":20990620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["class","code","cohesion","flake8","flake8-plugin","lint","linter-plugin","measure","module","oop","python","quality"],"created_at":"2024-07-31T22:01:53.935Z","updated_at":"2025-04-08T00:36:58.831Z","avatar_url":"https://github.com/mschwager.png","language":"Python","readme":"# Cohesion\n\n[![Python Versions](https://img.shields.io/pypi/pyversions/cohesion.svg)](https://img.shields.io/pypi/pyversions/cohesion.svg)\n[![PyPI Version](https://img.shields.io/pypi/v/cohesion.svg)](https://img.shields.io/pypi/v/cohesion.svg)\n\nCohesion is a tool for measuring Python class cohesion.\n\n\u003e In computer programming, cohesion refers to the degree to which the elements\n\u003e of a module belong together. Thus, cohesion measures the strength of\n\u003e relationship between pieces of functionality within a given module. For\n\u003e example, in highly cohesive systems functionality is strongly related.\n\u003e - [Wikipedia](https://en.wikipedia.org/wiki/Cohesion_(computer_science))\n\n\u003e When cohesion is high, it means that the methods and variables of the class\n\u003e are co-dependent and hang together as a logical whole.\n\u003e - Clean Code pg. 140\n\nSome of the advantages of high cohesion, also by Wikipedia:\n\n* Reduced module complexity (they are simpler, having fewer operations).\n* Increased system maintainability, because logical changes in the domain\n  affect fewer modules, and because changes in one module require fewer\n  changes in other modules.\n* Increased module reusability, because application developers will find\n  the component they need more easily among the cohesive set of operations\n  provided by the module.\n\n# Installing\n\n```\n$ python -m pip install cohesion\n$ cohesion -h\n```\n\n# Using\n\nCohesion measures class and instance variable usage across the methods\nof that class.\n\n```\n$ cat example.py\nclass ExampleClass1(object):\n    class_variable1 = 5\n    class_variable2 = 6\n\n    def func1(self):\n        self.instance_variable = 6\n\n        def inner_func(b):\n            return b + 5\n\n        local_variable = self.class_variable1\n\n        return local_variable\n\n    def func2(self):\n        print(self.class_variable2)\n\n    @staticmethod\n    def func3(variable):\n        return variable + 7\n\nclass ExampleClass2(object):\n    def func1(self):\n        self.instance_variable1 = 7\n```\n\n```\n$ cohesion --files example.py --verbose\nFile: example.py\n  Class: ExampleClass1 (1:0)\n    Function: func1 2/3 66.67%\n      Variable: class_variable1 True\n      Variable: class_variable2 False\n      Variable: instance_variable True\n    Function: func2 1/3 33.33%\n      Variable: class_variable1 False\n      Variable: class_variable2 True\n      Variable: instance_variable False\n    Function: func3 0/3 0.00%\n      Variable: class_variable1 False\n      Variable: class_variable2 False\n      Variable: instance_variable False\n    Total: 33.33%\n  Class: ExampleClass2 (23:0)\n    Function: func1 1/1 100.00%\n      Variable: instance_variable1 True\n    Total: 100.00%\n```\n\nThe `--below` and `--above` flags can be specified to only show classes with\na cohesion value below or above the specified percentage, respectively.\n\n## Flake8 Support\n\nCohesion supports being run by `flake8`. First, ensure your installation has\nregistered `cohesion`:\n\n```\n$ flake8 --version\n3.2.1 (pyflakes: 1.0.0, cohesion: 1.2.0, pycodestyle: 2.2.0, mccabe: 0.5.3) CPython 2.7.12 on Linux\n```\n\nAnd now use `flake8` to lint your file:\n\n```\n$ flake8 example.py\nexample.py:1:1: H601 class has low cohesion\n```\n\n# Developing\n\nFirst, install development packages:\n\n```\n$ python -m poetry install --with=dev\n```\n\n## Testing\n\n```\n$ python -m poetry run pytest\n```\n\n## Linting\n\n```\n$ python -m poetry run flake8\n```\n\n## Coverage\n\n```\n$ python -m poetry run pytest --cov\n```\n","funding_links":[],"categories":["Python","Programming Languages","Code Metrics \u0026 Complexity","Complexity"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmschwager%2Fcohesion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmschwager%2Fcohesion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmschwager%2Fcohesion/lists"}