11# code-cop
22
3- A code quality enforcement tool that analyzes code complexity metrics and helps maintain readable, maintainable code.
3+ A code quality enforcement tool that analyzes code complexity across a number of metrics and helps maintain readable, maintainable code.
44
55## What is code-cop?
66
@@ -12,10 +12,10 @@ Currently, code-cop supports Python code analysis with plans to add JavaScript a
1212
1313Complex code is harder to understand, test, and maintain. By enforcing limits on complexity metrics, you can:
1414
15- - Catch overly complex functions before they're merged
16- - Maintain consistent code quality standards across your team
17- - Identify refactoring opportunities
18- - Reduce technical debt over time
15+ - Catch overly complex functions before they're merged
16+ - Maintain consistent code quality standards across your team
17+ - Identify refactoring opportunities
18+ - Reduce technical debt over time
1919
2020## Installation
2121
@@ -36,8 +36,8 @@ pip install -e ".[dev]"
3636
3737### Requirements
3838
39- - Python 3.11 or higher
40- - Radon (automatically installed as a dependency)
39+ - Python 3.11 or higher
40+ - Radon (automatically installed as a dependency)
4141
4242## Basic Usage
4343
@@ -77,79 +77,80 @@ code-cop uses YAML configuration files. By default, it looks for `.code_cop.yaml
7777use_gitignore : true
7878
7979defaults :
80- max_cyclomatic_complexity : 10
81- min_maintainability_index : 50
82- max_halstead_volume : 1000
83- max_halstead_difficulty : 10
84- max_halstead_effort : 10000
85- max_cognitive_complexity : 15
80+ max_cyclomatic_complexity : 10
81+ min_maintainability_index : 50
82+ max_halstead_volume : 1000
83+ max_halstead_difficulty : 10
84+ max_halstead_effort : 10000
85+ max_cognitive_complexity : 15
8686
8787languages :
88- - name : python
89- extensions :
90- - .py
91- metrics :
92- - type : cyclomatic_complexity
93- threshold : 10
94- comparison : " <="
95- - type : maintainability_index
96- threshold : 50
97- comparison : " >="
98- - type : halstead_volume
99- threshold : 1000
100- comparison : " <="
101- - type : halstead_difficulty
102- threshold : 10
103- comparison : " <="
104- - type : halstead_effort
105- threshold : 10000
106- comparison : " <="
107- - type : cognitive_complexity
108- threshold : 15
109- comparison : " <="
110- enabled : false # Requires complexipy (not yet implemented)
88+ - name : python
89+ extensions :
90+ - .py
91+ metrics :
92+ - type : cyclomatic_complexity
93+ threshold : 10
94+ comparison : " <="
95+ - type : maintainability_index
96+ threshold : 50
97+ comparison : " >="
98+ - type : halstead_volume
99+ threshold : 1000
100+ comparison : " <="
101+ - type : halstead_difficulty
102+ threshold : 10
103+ comparison : " <="
104+ - type : halstead_effort
105+ threshold : 10000
106+ comparison : " <="
107+ - type : cognitive_complexity
108+ threshold : 15
109+ comparison : " <="
110+ enabled : false # Requires complexipy (not yet implemented)
111111
112112ignore_patterns :
113- - " **/test_*.py"
114- - " **/*_test.py"
115- - " **/tests/**"
116- - " **/__pycache__/**"
113+ - " **/test_*.py"
114+ - " **/*_test.py"
115+ - " **/tests/**"
116+ - " **/__pycache__/**"
117117` ` `
118118
119119### Configuration Structure
120120
121- - **use_gitignore**: Whether to automatically use patterns from ` .gitignore` (default: true)
122- - **defaults**: Default thresholds used when language-specific configuration is not provided
123- - **languages**: Language-specific configurations
124- - **name**: Language identifier (currently only "python" is supported)
125- - **extensions**: File extensions to associate with this language
126- - **metrics**: List of metrics to check
127- - **type**: The metric type (see Metrics section below)
128- - **threshold**: The threshold value
129- - **comparison**: How to compare the metric value with the threshold
130- - **enabled**: Whether to check this metric (default: true)
131- - **ignore_patterns**: Additional gitignore-style patterns for files to skip (combined with .gitignore if `use_gitignore` is true)
121+ - **use_gitignore**: Whether to automatically use patterns from ` .gitignore` (default: true)
122+ - **defaults**: Default thresholds used when language-specific configuration is not provided
123+ - **languages**: Language-specific configurations
124+ - **name**: Language identifier (currently only "python" is supported)
125+ - **extensions**: File extensions to associate with this language
126+ - **metrics**: List of metrics to check
127+ - **type**: The metric type (see Metrics section below)
128+ - **threshold**: The threshold value
129+ - **comparison**: How to compare the metric value with the threshold
130+ - **enabled**: Whether to check this metric (default: true)
131+ - **ignore_patterns**: Additional gitignore-style patterns for files to skip (combined with .gitignore if `use_gitignore` is true)
132132
133133# ## Comparison Operators
134134
135- - ` <=` - Metric value must be less than or equal to threshold
136- - ` <` - Metric value must be less than threshold
137- - ` >=` - Metric value must be greater than or equal to threshold
138- - ` >` - Metric value must be greater than threshold
139- - ` ==` - Metric value must equal threshold
140- - ` !=` - Metric value must not equal threshold
135+ - ` <=` - Metric value must be less than or equal to threshold
136+ - ` <` - Metric value must be less than threshold
137+ - ` >=` - Metric value must be greater than or equal to threshold
138+ - ` >` - Metric value must be greater than threshold
139+ - ` ==` - Metric value must equal threshold
140+ - ` !=` - Metric value must not equal threshold
141141
142142# # Metrics Explained
143143
144144# ## Cyclomatic Complexity
145145
146146Measures the number of linearly independent paths through a function. Higher values indicate more complex control flow.
147147
148- - **Good**: 1-10 (simple, easy to test)
149- - **Moderate**: 11-20 (more complex, harder to test)
150- - **High**: 21+ (very complex, consider refactoring)
148+ - **Good**: 1-10 (simple, easy to test)
149+ - **Moderate**: 11-20 (more complex, harder to test)
150+ - **High**: 21+ (very complex, consider refactoring)
151151
152152Example of high complexity :
153+
153154` ` ` python
154155def process_data(data, mode, validate, transform):
155156 if validate:
@@ -175,33 +176,33 @@ def process_data(data, mode, validate, transform):
175176
176177A composite metric (0-100) that considers cyclomatic complexity, lines of code, and Halstead volume. Higher values indicate more maintainable code.
177178
178- - **Good**: 50-100 (maintainable)
179- - **Moderate**: 20-49 (moderately maintainable)
180- - **Low**: 0-19 (difficult to maintain)
179+ - **Good**: 50-100 (maintainable)
180+ - **Moderate**: 20-49 (moderately maintainable)
181+ - **Low**: 0-19 (difficult to maintain)
181182
182183# ## Halstead Metrics
183184
184185Based on the number of operators and operands in code :
185186
186- - **Volume**: Program size based on the number of operations
187- - **Difficulty**: How hard the code is to understand
188- - **Effort**: Mental effort required to understand the code
189- - **Time**: Estimated time to implement (in seconds)
190- - **Bugs**: Estimated number of bugs (Volume / 3000)
187+ - **Volume**: Program size based on the number of operations
188+ - **Difficulty**: How hard the code is to understand
189+ - **Effort**: Mental effort required to understand the code
190+ - **Time**: Estimated time to implement (in seconds)
191+ - **Bugs**: Estimated number of bugs (Volume / 3000)
191192
192193# ## Lines of Code Metrics
193194
194- - **LOC**: Total lines of code
195- - **SLOC**: Source lines of code (excluding comments and blanks)
196- - **LLOC**: Logical lines of code
197- - **Comment Lines**: Number of comment lines
198- - **Blank Lines**: Number of blank lines
195+ - **LOC**: Total lines of code
196+ - **SLOC**: Source lines of code (excluding comments and blanks)
197+ - **LLOC**: Logical lines of code
198+ - **Comment Lines**: Number of comment lines
199+ - **Blank Lines**: Number of blank lines
199200
200201# # Exit Codes
201202
202- - **0**: All metrics pass their thresholds
203- - **1**: Error (invalid configuration, missing files, etc.)
204- - **2**: One or more metrics violate their thresholds
203+ - **0**: All metrics pass their thresholds
204+ - **1**: Error (invalid configuration, missing files, etc.)
205+ - **2**: One or more metrics violate their thresholds
205206
206207This makes code-cop suitable for CI/CD pipelines :
207208
@@ -302,13 +303,13 @@ code_cop/
302303
303304## Future Enhancements
304305
305- - JavaScript/TypeScript support via ts-complex
306- - Cognitive complexity for Python via complexipy
307- - Pre-commit hook integration
308- - Git hook support
309- - HTML report generation
310- - Baseline file support (ignore existing violations)
311- - Trend analysis over time
306+ - JavaScript/TypeScript support via ts-complex
307+ - Cognitive complexity for Python via complexipy
308+ - Pre-commit hook integration
309+ - Git hook support
310+ - HTML report generation
311+ - Baseline file support (ignore existing violations)
312+ - Trend analysis over time
312313
313314## Contributing
314315
@@ -326,5 +327,5 @@ Contributions are welcome! Please:
326327
327328## Acknowledgments
328329
329- - [ Radon] ( https://github.com/rubik/radon ) for Python code metrics
330- - Inspired by various code quality tools like ESLint, Pylint, and SonarQube
330+ - [ Radon] ( https://github.com/rubik/radon ) for Python code metrics
331+ - Inspired by various code quality tools like ESLint, Pylint, and SonarQube
0 commit comments