Skip to content

A Cognitive Code Complexity Analysis Tool. Cognitive complexity measures how hard it is for a human to understand the code, while cyclomatic complexity measures how hard your code is to test. It also provides Halstead metrics.

License

Notifications You must be signed in to change notification settings

Phauthentic/cognitive-code-analysis

Repository files navigation

Cognitive Complexity Analysis

Cognitive Code Analysis is an approach to understanding and improving code by focusing on how human cognition interacts with code. It emphasizes making code more readable, understandable, and maintainable by considering the cognitive processes of the developers who write and work with the code.

"Human short-term or working memory was estimated to be limited to 7 ± 2 variables in the 1950s. A more current estimate is 4 ± 1 constructs. Decision quality generally becomes degraded once this limit of four constructs is exceeded."

Source: Human Cognitive Limitations. Broad, Consistent, Clinical Application of Physiological Principles Will Require Decision Support

Running it

Cognitive Complexity Analysis

php analyse.php metrics:cognitive <path-to-folder>

Halstead Complexity Analysis

php analyse.php metrics:halstead <path-to-folder>

Documentation

Resources

These pages and papers provide more information on cognitive limitations and readability and the impact on the business.

Examples

Cognitive Metrics

Wordpress WP_Debug_Data

Class: \WP_Debug_Data
┌───────────────────┬──────────────┬───────────┬─────────┬─────────────┬────────────┬────────────┬────────────┬────────────┬────────────┐
│ Method Name       │ Lines        │ Arguments │ Returns │ Variables   │ Property   │ If         │ If Nesting │ Else       │ Cognitive  │
│                   │              │           │         │             │ Accesses   │            │ Level      │            │ Complexity │
├───────────────────┼──────────────┼───────────┼─────────┼─────────────┼────────────┼────────────┼────────────┼────────────┼────────────┤
│ check_for_updates │ 6 (0)        │ 0 (0)     │ 0 (0)   │ 0 (0)       │ 0 (0)      │ 0 (0)      │ 0 (0)      │ 0 (0)      │ 0          │
│ debug_data        │ 1230 (6.373) │ 0 (0)     │ 1 (0)   │ 105 (3.073) │ 20 (0.788) │ 58 (4.025) │ 3 (1.099)  │ 33 (3.497) │ 18.855     │
│ get_wp_constants  │ 144 (3.761)  │ 0 (0)     │ 1 (0)   │ 9 (0.875)   │ 0 (0)      │ 5 (1.099)  │ 1 (0)      │ 5 (1.609)  │ 7.345      │
│ get_wp_filesystem │ 60 (0)       │ 0 (0)     │ 1 (0)   │ 9 (0.875)   │ 0 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0.875      │
│ get_mysql_var     │ 15 (0)       │ 1 (0)     │ 2 (0)   │ 2 (0)       │ 0 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0          │
│ format            │ 60 (0)       │ 2 (0)     │ 1 (0)   │ 11 (1.03)   │ 0 (0)      │ 5 (1.099)  │ 1 (0)      │ 5 (1.609)  │ 3.738      │
│ get_database_size │ 14 (0)       │ 0 (0)     │ 1 (0)   │ 4 (0.336)   │ 1 (0)      │ 1 (0)      │ 1 (0)      │ 0 (0)      │ 0.336      │
│ get_sizes         │ 125 (3.512)  │ 0 (0)     │ 1 (0)   │ 14 (1.224)  │ 0 (0)      │ 9 (1.946)  │ 2 (0.693)  │ 5 (1.609)  │ 8.984      │
└───────────────────┴──────────────┴───────────┴─────────┴─────────────┴────────────┴────────────┴────────────┴────────────┴────────────┘

Doctrine Paginator

Class: Doctrine\ORM\Tools\Pagination\Paginator
┌───────────────────────────────────────────┬────────┬───────────┬─────────┬───────────┬──────────┬───────┬────────────┬───────────┬────────────┐
│ Method Name                               │ Lines  │ Arguments │ Returns │ Variables │ Property │ If    │ If Nesting │ Else      │ Cognitive  │
│                                           │        │           │         │           │ Accesses │       │ Level      │           │ Complexity │
├───────────────────────────────────────────┼────────┼───────────┼─────────┼───────────┼──────────┼───────┼────────────┼───────────┼────────────┤
│ __construct                               │ 10 (0) │ 2 (0)     │ 0 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getQuery                                  │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ getFetchJoinCollection                    │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ getUseOutputWalkers                       │ 4 (0)  │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ setUseOutputWalkers                       │ 6 (0)  │ 1 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0          │
│ count                                     │ 12 (0) │ 0 (0)     │ 1 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getIterator                               │ 46 (0) │ 0 (0)     │ 2 (0)   │ 9 (0.875) │ 2 (0)    │ 3 (0) │ 2 (0.693)  │ 2 (0.693) │ 2.262      │
│ cloneQuery                                │ 13 (0) │ 1 (0)     │ 1 (0)   │ 3 (0.182) │ 0 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0.182      │
│ useOutputWalker                           │ 8 (0)  │ 1 (0)     │ 2 (0)   │ 1 (0)     │ 1 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ appendTreeWalker                          │ 11 (0) │ 2 (0)     │ 0 (0)   │ 1 (0)     │ 0 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0          │
│ getCountQuery                             │ 25 (0) │ 0 (0)     │ 1 (0)   │ 4 (0.336) │ 1 (0)    │ 2 (0) │ 1 (0)      │ 1 (0)     │ 0.336      │
│ unbindUnusedQueryParams                   │ 17 (0) │ 1 (0)     │ 0 (0)   │ 6 (0.588) │ 0 (0)    │ 1 (0) │ 1 (0)      │ 0 (0)     │ 0.588      │
│ convertWhereInIdentifiersToDatabaseValues │ 11 (0) │ 1 (0)     │ 1 (0)   │ 5 (0.47)  │ 1 (0)    │ 0 (0) │ 0 (0)      │ 0 (0)     │ 0.47       │
└───────────────────────────────────────────┴────────┴───────────┴─────────┴───────────┴──────────┴───────┴────────────┴───────────┴────────────┘

Halstead Metrics

Class: WP_Debug_Data
File: /home/florian/projects/cognitive-code-checker/tests/TestCode/FileWithTwoClasses.php
┌─────────────┬─────────────┬───────────┬──────────┬─────────┬────────────┬──────────┬────────────┬───────────┬──────────┐
│ n1 Distinct │ n2 Distinct │ Total     │ Total    │ Program │ Program    │ Volume   │ Difficulty │ Effort    │ Possible │
│ Operators   │ Operands    │ Operators │ Operands │ Length  │ Vocabulary │          │            │           │ Bugs     │
├─────────────┼─────────────┼───────────┼──────────┼─────────┼────────────┼──────────┼────────────┼───────────┼──────────┤
│ 13          │ 563         │ 869       │ 2123     │ 2992    │ 576        │ 27436.42 │ 24.51      │ 672484.58 │ 9.15     │
└─────────────┴─────────────┴───────────┴──────────┴─────────┴────────────┴──────────┴────────────┴───────────┴──────────┘

Class: Doctrine\ORM\Tools\Pagination\Paginator
File: /home/florian/projects/cognitive-code-checker/tests/TestCode/FileWithTwoClasses.php
┌─────────────┬─────────────┬───────────┬──────────┬─────────┬────────────┬─────────┬────────────┬──────────┬──────────┐
│ n1 Distinct │ n2 Distinct │ Total     │ Total    │ Program │ Program    │ Volume  │ Difficulty │ Effort   │ Possible │
│ Operators   │ Operands    │ Operators │ Operands │ Length  │ Vocabulary │         │            │          │ Bugs     │
├─────────────┼─────────────┼───────────┼──────────┼─────────┼────────────┼─────────┼────────────┼──────────┼──────────┤
│ 7           │ 36          │ 108       │ 161      │ 269     │ 43         │ 1459.67 │ 15.65      │ 22847.82 │ 0.49     │
└─────────────┴─────────────┴───────────┴──────────┴─────────┴────────────┴─────────┴────────────┴──────────┴──────────┘

License

Copyright Florian Krämer

Licensed under the GPL3 license.

About

A Cognitive Code Complexity Analysis Tool. Cognitive complexity measures how hard it is for a human to understand the code, while cyclomatic complexity measures how hard your code is to test. It also provides Halstead metrics.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages