Cohesion
Cohesion is a tool for measuring Python class cohesion.
In computer programming, cohesion refers to the degree to which the elements of a module belong together. Thus, cohesion measures the strength of relationship between pieces of functionality within a given module. For example, in highly cohesive systems functionality is strongly related.
When cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole.
- Clean Code pg. 140
Some of the advantages of high cohesion, also by Wikipedia:
- Reduced module complexity (they are simpler, having fewer operations).
- Increased system maintainability, because logical changes in the domain affect fewer modules, and because changes in one module require fewer changes in other modules.
- Increased module reusability, because application developers will find the component they need more easily among the cohesive set of operations provided by the module.
Installing
$ python -m pip install cohesion
$ cohesion -h
Using
Cohesion measures class and instance variable usage across the methods of that class.
$ cat example.py
class ExampleClass1(object):
class_variable1 = 5
class_variable2 = 6
def func1(self):
self.instance_variable = 6
def inner_func(b):
return b + 5
local_variable = self.class_variable1
return local_variable
def func2(self):
print(self.class_variable2)
@staticmethod
def func3(variable):
return variable + 7
class ExampleClass2(object):
def func1(self):
self.instance_variable1 = 7
$ cohesion --files example.py --verbose
File: example.py
Class: ExampleClass1 (1:0)
Function: func1 2/3 66.67%
Variable: class_variable1 True
Variable: class_variable2 False
Variable: instance_variable True
Function: func2 1/3 33.33%
Variable: class_variable1 False
Variable: class_variable2 True
Variable: instance_variable False
Function: func3 0/3 0.00%
Variable: class_variable1 False
Variable: class_variable2 False
Variable: instance_variable False
Total: 33.33%
Class: ExampleClass2 (23:0)
Function: func1 1/1 100.00%
Variable: instance_variable1 True
Total: 100.00%
The --below and --above flags can be specified to only show classes with
a cohesion value below or above the specified percentage, respectively.
Flake8 Support
Cohesion supports being run by flake8. First, ensure your installation has
registered cohesion:
$ flake8 --version
3.2.1 (pyflakes: 1.0.0, cohesion: 1.2.0, pycodestyle: 2.2.0, mccabe: 0.5.3) CPython 2.7.12 on Linux
And now use flake8 to lint your file:
$ flake8 example.py
example.py:1:1: H601 class has low cohesion
Developing
First, install development packages:
$ python -m poetry install --with=dev
Testing
$ python -m poetry run pytest
Linting
$ python -m poetry run flake8
Coverage
$ python -m poetry run pytest --cov
Metadata
Release files for cohesion 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cohesion-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for cohesion-1.2.0-py3-none-any.whl |
Release files / cohesion-1.2.0-py3-none-any.whl
| Download URL | cohesion-1.2.0-py3-none-any.whl |
|---|---|
| Size | 20.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d4a4c06a83022fcc56bf6af220247df9d6de7707a7a16442e98e7ad2ea152f79
|
|
BLAKE2b-256 checksum How to use checksums |
48967e5d3528227b4743ca2b718ef60ff1f58c2ed143fff3c6a7c8b1c0147858
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.2 Linux/6.1.0-28-amd64
|
Security for cohesion 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.