Sign inSign up

kartdev03/complexity-analyzer

By kartdev03

•Updated about 1 year ago

Image
0

268

kartdev03/complexity-analyzer repository overview

⁠Complexity Analyzer

A Docker-based tool for analyzing code complexity across projects. This tool scans codebases and generates comprehensive complexity metrics including cyclomatic complexity, maintainability index, and code structure analysis.

⁠Overview

The Complexity Analyzer is a containerized Python application that helps developers and teams understand the structural complexity of their codebases. It produces detailed reports on code metrics that can inform refactoring decisions and code quality improvements.

⁠Features

  • Cyclomatic Complexity Analysis: Measures code complexity based on the number of independent paths through code
  • Maintainability Index: Calculates maintainability scores for easier identification of problematic code sections
  • Halstead Metrics: Volume, difficulty, effort, and estimated bug count
  • COCOMO Model: Effort estimation, timeline, team size, and cost projections
  • Data-flow Complexity: Reveals variable usage patterns and dependencies
  • Multi-project Support: Analyze and compare multiple projects
  • JSON Output: Results exported in multiple formats for easy integration
  • Comprehensive Reporting: Detailed breakdown by file, module, and function

⁠Quick Start

⁠Prerequisites
  • Docker installed on your system
  • Access to the codebase you want to analyze
⁠Installation

Pull the latest image from Docker Hub:

docker pull kartdev03/complexity-analyzer:latest
⁠Usage

Run the analyzer and output results to your current directory:

docker run -v $(pwd)/output:/app/output kartdev03/complexity-analyzer python analyze_projects.py

This command:

  • Mounts your local output directory to the container's /app/output directory
  • Executes the main analysis script
  • Writes results to ./output/ on your host machine

⁠Output

The analyzer generates multiple output files in the output directory:

  • analysis_[project].json: Detailed metrics for each analyzed project including cyclomatic complexity, Halstead metrics, COCOMO estimates, and file/class/module-level analysis
  • comparison.json: Side-by-side comparison of all analyzed projects with key metrics
  • analysis_report.txt: Executive summary with findings and metric usefulness analysis
⁠Output Structure
output/
├── analysis_flask.json
├── analysis_requests.json
├── analysis_rich.json
├── comparison.json
└── analysis_report.txt
⁠Sample Analysis Results

This tool has been successfully used to analyze three major Python projects:

ProjectFilesSLOCAvg ComplexityMax ComplexityEffort (person-months)
Flask233,0652.532110.52
Requests182,0053.36234.98
Rich7814,9363.235941.04

⁠Metrics Explained

⁠Cyclomatic Complexity (CC)

Measures the number of linearly independent paths through code. Higher values indicate more complex logic:

  • 1-5: Low complexity (good)
  • 6-10: Moderate complexity (acceptable)
  • 11-20: High complexity (consider refactoring)
  • 20+: Very high complexity (refactor recommended)

Real-world insight: Flask's maximum complexity of 21 in a single function represents a complex piece of logic that could benefit from refactoring.

⁠Halstead Metrics

Advanced software metrics that measure program complexity and code quality:

  • Volume: Amount of code in the program; measured in bits
  • Difficulty: How hard is the code to write and understand
  • Effort: Volume × Difficulty; time required to implement
  • Estimated Bugs: Predicted bugs based on the formula: volume / 3000

Example from Flask:

  • Volume: 107,102 bits
  • Difficulty: 70.04 (moderately high)
  • Estimated Bugs: 35.7 (expected bug count)
⁠COCOMO Model (Constructive Cost Model)

Provides effort estimation and project planning metrics:

  • Effort: Estimated person-months of work
  • Time: Calendar months needed (typically lower than effort due to parallelization)
  • Team Size: Recommended number of people
  • Cost: Estimated project cost (assuming $100/person-hour)

Flask Example:

  • Effort: 10.52 person-months
  • Timeline: 5.70 months (with parallel team members)
  • Team Size: 1.85 people
  • Estimated Cost: $105,177.63

Rich Example (larger project):

  • Effort: 41.04 person-months
  • Timeline: 10.26 months
  • Team Size: 4.00 people
  • Estimated Cost: $410,352.67
⁠Lines of Code (LOC)
  • Total LOC: All lines including comments and blank lines
  • SLOC: Source lines of code (executable code only)
  • Comment lines: Documentation in code
  • Blank lines: Empty lines

Real Data: Flask has 3,065 SLOC across 23 files, averaging 133 SLOC per file.

⁠Maintainability Index (MI)

Calculates how maintainable code is on a scale of 0-100:

  • 85-100: Highly maintainable
  • 65-84: Maintainable
  • 50-64: Moderately maintainable
  • Below 50: Difficult to maintain

⁠Supported Languages

  • Python

⁠Volume Mounts

Host PathContainer PathPurpose
$(pwd)/output/app/outputOutput directory for results

⁠Docker Hub

For more information, visit the Docker Hub repository: https://hub.docker.com/r/kartdev03/complexity-analyzer⁠

⁠Interpreting Results

⁠JSON Output Structure

The JSON report includes:

{
  "project_name": "requests",
  "files_analyzed": 18,
  "total_sloc": 2005,
  "total_loc": 5641,
  "total_blank": 1099,
  "total_comments": 2537,
  "cyclomatic_complexity": [
    {
      "file": "auth.py",
      "function": "build_digest_header",
      "complexity": 23
    }
  ],
  "halstead": {
    "volume": 78594.93,
    "difficulty": 57.64,
    "effort": 4529910.15,
    "estimated_bugs": 26.20
  },
  "cocomo": {
    "model_type": "organic",
    "effort": 4.98,
    "time": 4.60,
    "team_size": 1.08,
    "cost": 49823.17
  },
  "file_details": [...],
  "class_metrics": [...],
  "module_metrics": [...],
  "avg_cyclomatic": 3.36,
  "max_cyclomatic": 23,
  "min_cyclomatic": 1,
  "maintainability_index": {...}
}
⁠Comparison Report

The comparison.json file provides side-by-side metrics:

[
  {
    "project": "Flask",
    "files": 23,
    "sloc": 3065,
    "avg_cyclomatic": 2.527,
    "max_cyclomatic": 21,
    "halstead_volume": 107102.22,
    "halstead_difficulty": 70.04,
    "estimated_bugs": 35.70,
    "cocomo_effort": 10.52,
    "cocomo_time": 5.70,
    "cocomo_cost": 105177.63
  }
]
⁠Analysis Report Summary

The analysis_report.txt provides executive insights:

EXECUTIVE SUMMARY
Total Projects Analyzed: 3
Combined SLOC: 20,006
Combined Files: 119

FLASK
Files Analyzed: 23
Total SLOC: 3,065
Average Cyclomatic Complexity: 2.53 (Low - well-structured)
Halstead Estimated Bugs: 35.70
COCOMO Effort: 10.52 person-months

⁠Metrics Usefulness

MetricUsefulnessBest Use Case
SLOCHighQuick size comparison between projects
Cyclomatic ComplexityVery HighIdentifying functions needing refactoring
Halstead MetricsModerate-HighEstimating bugs and effort
Data-flow ComplexityModerateFinding high-coupling functions
COCOMO ModelHigh for PlanningBudgeting and timeline estimation

⁠Key Findings from Sample Analysis

  1. Low Complexity Projects: All three projects (Flask, Requests, Rich) maintain average cyclomatic complexity below 3.5, indicating well-structured code.

  2. Outlier Functions: Rich has a maximum complexity of 59, suggesting some complex algorithms. Flask and Requests have max complexities of 21 and 23 respectively.

  3. Scalability: Rich's 78 files show that even larger projects can maintain low average complexity through good code organization.

  4. Effort Estimation: Despite Rich being 5x larger than Flask, the effort estimate is only 4x higher, showing economies of scale in larger projects.

  5. Bug Density: Using Halstead metrics, Rich is estimated to have 219 bugs, which scales appropriately with its size.

⁠Support

For issues, questions, or feature requests, visit:

⁠Version

Latest: kartdev03/complexity-analyzer:latest

Pull the specific version or check Docker Hub for available tags.


Last Updated: November 2025

Tag summary

Content type

Image

Digest

sha256:ede7a89a6…

Size

76.9 MB

Last updated

about 1 year ago

docker pull kartdev03/complexity-analyzer