A Python-based tool for detecting common code smells in Java source files. This tool can identify and report on six major code smell patterns that indicate potential issues with code maintainability and design.
β¨ Detects 6 Major Code Smells:
- Long Method - Methods that are excessively long
- God Class (Blob) - Classes with too many responsibilities
- Duplicated Code - Repeated code blocks that should be refactored
- Large Parameter List - Methods with too many parameters
- Magic Numbers - Hardcoded numeric values that should be constants
- Feature Envy - Methods overly interested in other classes' data
π§ Flexible Configuration:
- YAML-based configuration with per-smell settings
- CLI overrides for runtime control
- Adjustable thresholds and detection parameters
π Multiple Output Formats:
- Detailed reports with line numbers and suggestions
- Summary reports with statistics
- JSON output for integration with other tools
- Python 3.7+
- PyYAML package
# Install required dependencies
pip install PyYAML
# Clone or download the project
cd code-smell-detector/src# Analyze a single file
python detector_cli.py MyClass.java
# Analyze all Java files in a directory
python detector_cli.py src/
# List available detectors
python detector_cli.py --list-detectors# Only detect specific smells
python detector_cli.py src/ --only LongMethod,GodClass
# Exclude specific smells
python detector_cli.py src/ --exclude MagicNumbers,DuplicatedCode# Detailed report (default)
python detector_cli.py src/ --format detailed
# Summary statistics
python detector_cli.py src/ --format summary
# JSON output
python detector_cli.py src/ --format json# Use custom configuration file
python detector_cli.py src/ --config custom_config.yaml
# Save report to file
python detector_cli.py src/ --output report.txt
# Verbose output
python detector_cli.py src/ --verboseThe tool uses a YAML configuration file (config/config.yaml) to customize detection parameters:
code_smells:
LongMethod:
enabled: true
threshold_lines: 30
GodClass:
enabled: true
threshold_methods: 15
threshold_lines: 200
MagicNumbers:
enabled: true
exclude_common: true # Skip 0, 1, -1, 2, etc.
exclude_constants: true # Skip final/static variables--onlyoverrides everything else--excludeoverrides config file- Config file settings used as default
What it detects: Methods longer than the configured threshold (default: 30 lines) Why it matters: Long methods are harder to understand, test, and maintain Suggestion: Break into smaller, focused methods
What it detects: Classes with too many methods or lines of code Why it matters: Violates Single Responsibility Principle Suggestion: Split into multiple smaller, focused classes
What it detects: Similar code blocks that appear multiple times Why it matters: Changes must be made in multiple places Suggestion: Extract into reusable methods
What it detects: Methods with many parameters (default: >5) Why it matters: Hard to use and understand Suggestion: Use parameter objects or builder pattern
What it detects: Hardcoded numeric values in code Why it matters: Unclear meaning and hard to maintain Suggestion: Extract into named constants
What it detects: Methods that access other classes' data frequently Why it matters: Suggests misplaced responsibility Suggestion: Move method to appropriate class
============================================================
CODE SMELL DETECTION REPORT
============================================================
Total smells detected: 51
Active detectors: LongMethod, GodClass, DuplicatedCode, LargeParameterList, MagicNumbers, FeatureEnvy
π LongMethod (1 occurrences)
----------------------------------------
π File: LibrarySystem.java
π Lines: 32-122
β οΈ Method 'borrowBook' is too long (90 lines, threshold: 30)
π§ Severity: High
π‘ Suggestion: Consider breaking this method into smaller, more focused methods
CODE SMELL SUMMARY
==============================
Total: 94 smells
By Type:
FeatureEnvy: 7
GodClass: 2
LongMethod: 3
MagicNumbers: 82
By Severity:
High: 3
Low: 82
Medium: 9
code-smell-detector/
βββ src/
β βββ detector_cli.py # Command-line interface
β βββ detector_engine.py # Main detection engine
β βββ detectors/
β βββ base_detector.py # Abstract base class
β βββ structure_detectors.py # Long Method, God Class
β βββ parameter_detectors.py # Large Parameter List, Magic Numbers
β βββ duplication_detectors.py # Duplicated Code, Feature Envy
βββ config/
β βββ config.yaml # Configuration file
βββ test-files/ # Sample Java files for testing
βββ README.md # This documentation
The detector has been tested on a deliberately smelly Java library management system that contains all six code smell types. Test results show accurate detection and reporting.
# Test all detectors on sample code
python detector_cli.py ../test-files/ --verbose
# Test specific detectors
python detector_cli.py ../test-files/LibrarySystem.java --only LongMethod,GodClass
# Generate JSON report
python detector_cli.py ../test-files/ --format json --output results.jsonThe tool is designed for extensibility:
- Add New Detectors: Inherit from
BaseDetectorand implement the detection logic - Custom Output Formats: Extend the reporting system in
detector_engine.py - Language Support: Adapt the parsing logic for other programming languages
- IDE Integration: Use the JSON output format for integration with development tools
To add a new code smell detector:
- Create a new detector class inheriting from
BaseDetector - Implement the
detect()method andsmell_typeproperty - Add configuration options to
config.yaml - Register the detector in
detector_engine.py - Update documentation
This project is created for educational purposes as part of a Software Engineering assignment demonstrating code smell detection techniques.