Skip to content

Detect AI-generated anti-patterns: magic numbers, comment-over-code, over-engineering #8

@rudra496

Description

@rudra496

Summary

AI-generated code has distinctive anti-patterns that differ from human-written code. Add a new AIPatternsAnalyzer that detects these.

AI Code Anti-Patterns to Detect

1. Magic Numbers

AI loves magic numbers instead of named constants:

# AI-generated
if confidence > 0.85:
    retry(max_attempts=3, delay=2.5)

# Human-written
HIGH_CONFIDENCE_THRESHOLD = 0.85
MAX_RETRIES = 3
RETRY_DELAY_SECONDS = 2.5

2. Comment-Over-Code Ratio

AI tends to over-document obvious code:

# Initialize an empty list to store the results
results = []

# Loop through each item in the items list
for item in items:
    # Append the item to the results list
    results.append(item)

3. Over-Engineering

AI wraps simple logic in unnecessary abstractions:

class DataProcessorFactory:
    @staticmethod
    def create_processor(type: str) -> DataProcessor:
        # For a simple list filter...

4. Uniform Variable Names

AI tends to use result, data, item, value without descriptive names.

5. TODO/Placeholder Comments

AI leaves # TODO: implement this, # Add more error handling here, pass

Implementation

  1. Create ai_trust_validator/analyzers/ai_patterns.py
  2. Implement AIPatternsAnalyzer(BaseAnalyzer)
  3. Add heuristics with configurable thresholds
  4. Add to Validator._init_analyzers()
  5. Add comprehensive tests

Acceptance Criteria

  • Detects at least 5 AI-specific anti-patterns
  • Comment-over-code ratio check works
  • Magic number detection with configurable threshold
  • Results contribute to trust score
  • Tests with real AI-generated code samples

Difficulty

Intermediate — pattern matching is straightforward but designing good heuristics requires thought.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions