Skip to content

Add Code Smell Detection and Automatic Refactoring#940

Draft
codegen-sh[bot] wants to merge 2 commits intodevelopfrom
codegen-code-smell-detector-a7f9d2
Draft

Add Code Smell Detection and Automatic Refactoring#940
codegen-sh[bot] wants to merge 2 commits intodevelopfrom
codegen-code-smell-detector-a7f9d2

Conversation

@codegen-sh
Copy link
Contributor

@codegen-sh codegen-sh bot commented Mar 22, 2025

Overview

This PR adds a powerful new feature to Codegen: automatic code smell detection and refactoring. This feature helps developers identify and fix common code quality issues in their codebases.

Key Features

  • Code Smell Detection: Automatically detect common code smells like:

    • Long functions
    • Long parameter lists
    • Duplicate code
    • Dead code (unused functions, classes, variables)
    • Complex conditionals
    • Data clumps (same fields appearing in multiple classes)
  • Automatic Refactoring: Intelligently refactor detected code smells:

    • Extract long functions into smaller, more focused functions
    • Convert long parameter lists into parameter objects/classes
    • Remove dead code
    • And more!
  • CLI Integration: New codegen code-smells detect command with options for:

    • Filtering by severity and category
    • Customizing detection thresholds
    • Generating JSON reports
    • Automatic refactoring
  • SDK Integration: Full programmatic API for custom code smell detection and refactoring workflows

Example Usage

# Detect code smells in the current directory
codegen code-smells detect

# Detect and automatically refactor code smells
codegen code-smells detect --refactor

# Filter by severity
codegen code-smells detect --severity high

# Customize detection thresholds
codegen code-smells detect --long-function-lines 30 --long-parameter-list 4

# Generate a JSON report
codegen code-smells detect --output report.json

Programmatic Usage

from codegen import Codebase, CodeSmellDetector, CodeSmellRefactorer

# Initialize codebase and detector
codebase = Codebase("./")
detector = CodeSmellDetector(codebase)
refactorer = CodeSmellRefactorer(codebase)

# Detect code smells
smells = detector.detect_all()

# Filter by severity or category
high_severity_smells = detector.detect_by_severity(CodeSmellSeverity.HIGH)

# Refactor automatically
for smell in smells:
    if refactorer.can_refactor(smell):
        refactorer.refactor(smell)

This feature helps developers maintain high code quality by identifying and fixing common issues that can lead to maintenance problems. The automatic refactoring capabilities demonstrate Codegen's power to not just analyze but also transform code in meaningful ways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants