Skip to content

Latest commit

 

History

History

README.md

🚀 Lab 31: AI-Driven Refactor with Metrics

🧭 Lab Overview

Measured legacy Python code complexity, refactored the pricing logic into a strategy-based design, and validated the change with metrics, benchmarks, and correctness tests.

🎯 Objectives

  • Use AI tools to safely refactor legacy code
  • Measure code complexity before and after refactoring
  • Track performance improvements through metrics
  • Validate refactoring changes with automated tests
  • Apply DevOps best practices for code quality monitoring

🧩 Prerequisites

  • Basic Python programming knowledge
  • Understanding of code complexity concepts
  • Familiarity with Linux command line
  • Basic Git operations
  • Understanding of software metrics (cyclomatic complexity, maintainability)

🖥️ Lab Environment

  • Platform: Ubuntu 24.04 LTS cloud lab environment
  • Host: ip-172-31-10-244
  • Shell: Bash
  • Primary toolchain: Python 3.12, Radon, Pylint, Pytest, pytest-benchmark, Black, isort, Git, Aider configuration

🛠️ Task Overview

  • Analyze Legacy Code Complexity
  • AI-Assisted Refactoring
  • Measure and Compare Metrics

📁 Repository Structure

lab31-ai-driven-refactor-with-metrics/
├── artifacts/
│   ├── baseline_metrics_full.json
│   ├── refactored_metrics_full.json
│   └── refactoring_impact_report.txt
├── .aider.conf.yaml
├── commands.sh
├── compare_metrics.py
├── legacy_calculator.py
├── measure_metrics.py
├── output.txt
├── refactor_instructions.md
├── refactored_calculator.py
├── test_correctness.py
├── test_performance.py
└── test_refactored_performance.py

✅ Verification and Validation

  • Radon metrics, benchmark comparison, and six correctness test cases.
  • Average cyclomatic complexity reduced from 9.50 to 2.17.
  • Shared-function comparison reported 75.44% overall complexity improvement.
  • Maintainability Index improved from 48.07 to 59.53.
  • Correctness tests passed and behavior remained functionally equivalent.
  • Performance remained acceptable, with some overhead introduced by strategy dispatch.

📘 What I Learned

  • How to baseline code quality before changing legacy logic.
  • How strategy-based refactoring can reduce nested conditionals.
  • How to compare complexity, maintainability, and benchmark data together.
  • Why automated correctness tests are mandatory during refactors.

🌍 Why This Matters

Metrics-backed refactoring is useful when reducing technical debt without breaking production logic.

🏭 Real-World Applications

  • Maintaining legacy business logic in Python services.
  • Preparing refactor work for CI/CD quality gates.
  • Justifying technical-debt cleanup with measurable evidence.

🧪 Result

This lab was completed successfully and documented with separate source files, execution commands, runtime output, interview prep, and troubleshooting guidance.

🏁 Conclusion

This lab strengthened practical experience with ai-driven refactor with metrics in a hands-on Linux environment. The documented workflow, source files, and verification steps make the implementation reproducible and suitable for portfolio use.