All Projects → mschwager → Cohesion

mschwager / Cohesion

Licence: gpl-3.0
A tool for measuring Python class cohesion.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Cohesion

Seriouscode
This header file enforces Clang warnings to bu turned-on for specific flags (almost everyone, at least each one I was able to find).
Stars: ✭ 68 (-47.29%)
Mutual labels:  quality, code
python-pyfields
Define fields in python classes. Easily.
Stars: ✭ 39 (-69.77%)
Mutual labels:  oop, class
Object Oriented Programming Using Python
Python is a multi-paradigm programming language. Meaning, it supports different programming approach. One of the popular approach to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).
Stars: ✭ 183 (+41.86%)
Mutual labels:  oop, class
hulks
Olist custom linting hooks 💚 👽
Stars: ✭ 25 (-80.62%)
Mutual labels:  lint, quality
Stampit
OOP is better with stamps: Composable object factories.
Stars: ✭ 3,021 (+2241.86%)
Mutual labels:  oop, class
BashClass
BashClass is an Object Oriented Programming language that compiles to BASH 4.4
Stars: ✭ 40 (-68.99%)
Mutual labels:  oop, class
LMPHP
Multi-language management and support on the site.
Stars: ✭ 19 (-85.27%)
Mutual labels:  oop, class
Phpinsights
🔰 Instant PHP quality checks from your console
Stars: ✭ 4,442 (+3343.41%)
Mutual labels:  quality, code
Python And Oop
Object-Oriented Programming concepts in Python
Stars: ✭ 123 (-4.65%)
Mutual labels:  oop, class
Sgf
This is a Smart Game Foundation (Not Framework)
Stars: ✭ 122 (-5.43%)
Mutual labels:  module
Zpa
A parser and source code analyzer for PL/SQL and Oracle SQL.
Stars: ✭ 124 (-3.88%)
Mutual labels:  quality
Imagezipper
An image compresssion library in android.
Stars: ✭ 121 (-6.2%)
Mutual labels:  quality
Styleflow
StyleFlow: Attribute-conditioned Exploration of StyleGAN-generated Images using Conditional Continuous Normalizing Flows (ACM TOG 2021)
Stars: ✭ 1,982 (+1436.43%)
Mutual labels:  quality
Low Level Design Primer
Dedicated Resources for the Low-Level System Design. Learn how to design and implement large-scale systems. Prep for the system design interview.
Stars: ✭ 2,706 (+1997.67%)
Mutual labels:  oop
Nest Raven
Sentry Raven Module for Nest.js Framework
Stars: ✭ 123 (-4.65%)
Mutual labels:  module
Njsscan
njsscan is a semantic aware SAST tool that can find insecure code patterns in your Node.js applications.
Stars: ✭ 128 (-0.78%)
Mutual labels:  lint
Container Query
A PostCSS plugin and Javascript runtime combination, which allows you to write container queries in your CSS the same way you would write media queries.
Stars: ✭ 119 (-7.75%)
Mutual labels:  module
White
The Black code formatter, but brighter (PEP8–inspired).
Stars: ✭ 120 (-6.98%)
Mutual labels:  code
Interview Series
iOS从入门到进阶 - 技术合集
Stars: ✭ 129 (+0%)
Mutual labels:  class
Tempy
Python Object Oriented Html Templating System
Stars: ✭ 126 (-2.33%)
Mutual labels:  oop

Cohesion

Build Status Build Status Coverage Status Python Versions PyPI Version

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

OR

$ git clone https://github.com/mschwager/cohesion.git
$ cd cohesion
$ PYTHONPATH=lib/ python -m 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: 0.8.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

Python Versions

If you would like to simultaneously run Cohesion on Python 2 and Python 3 code then you will have to install it for both versions. I.e.

$ python2 -m pip install cohesion
$ python3 -m pip install cohesion

Then use the corresponding version to run the module:

$ python2 -m cohesion --files python2_file.py
$ python3 -m cohesion --files python3_file.py

Developing

First, install development packages:

$ python -m pip install -r requirements-dev.txt

Testing

$ nose2

Linting

$ flake8

Coverage

$ nose2 --with-coverage
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].