An educational Maven project demonstrating common code smells and their refactoring solutions in Java with practical, real-world examples.
- Project Overview
- Prerequisites
- Project Structure
- Quick Start
- Available Code Smells
- Running Instructions
- Testing
- Contributing
This project serves as an educational resource for learning clean code principles through hands-on examples. Each code smell is demonstrated with:
- β Before and after code comparisons
- β Interactive console demonstrations
- β Unit tests showing improvements
- Long Method: Extract Method refactoring to break down complex methods
- Long Parameter List: Introduce Parameter Object refactoring to group related parameters
- Magic Numbers: Replace Magic Number with Named Constant refactoring to improve readability
- Switch Statements: Replace Conditional with Polymorphism to eliminate switch statements
- Feature Envy: Move Method refactoring to place methods with the data they operate on
- Circular Dependencies: Dependency Inversion refactoring to break circular dependencies with interfaces
- Java 21 (OpenJDK 21.0.7+)
- Maven 3.9+
- IDE (IntelliJ IDEA, Eclipse, or VS Code recommended)
# Using SDKMAN (recommended)
sdk install java 21.0.7-tem
# For this project only (recommended approach)
# The project includes .sdkmanrc file for automatic environment switching
sdk env
# Or manually switch for this session only
sdk use java 21.0.7-tem
# Verify installation
java --version
clean-code/
βββ src/main/java/com/cleancode/
βββ domain/
βββ sizecomplexity/
β βββ longmethod/
β βββ longparameterlist/
β βββ magicnumbers/
βββ oopantipatterns/
β βββ switchstatements/
βββ dependencyproblems/
β βββ featureenvy/
β βββ circulardependencies/
βββ Main.java
Code Smell Organization:
- Each code smell has its own dedicated package
- Clear separation between problematic and refactored code
- Shared domain models in
com.cleancode.domain
package - Comprehensive test coverage for each smell
git clone https://github.com/shift-elevate/clean-code.git
cd clean-code
The project includes a .sdkmanrc
file that automatically configures Java 21 and Maven when you enter the directory:
# Activate the project environment (Java 21 + Maven)
sdk env
# This will automatically switch to:
# - Java 21.0.7-tem
# - Maven 3.9.10
mvn clean compile
mvn exec:java -Dexec.mainClass="com.cleancode.Main"
This will launch the main demonstration showing all implemented code smells and their refactored solutions.
The best way to see all code smells and refactoring solutions in action:
# Run the main application with all demonstrations
mvn exec:java -Dexec.mainClass="com.cleancode.Main"
# Or compile and run directly
mvn clean compile
java -cp target/classes com.cleancode.Main
For standalone execution:
# Build executable JAR
mvn clean package
# Run the JAR
java -jar target/clean-code-java-1.0.0.jar
mvn test
# Long Method Code Smell
mvn exec:java@long-method
# Switch Statements Code Smell
mvn exec:java@switch-statements
# Long Parameter List Code Smell
mvn exec:java@long-parameter-list
# Magic Numbers Code Smell
mvn exec:java@magic-numbers
# Feature Envy Code Smell
mvn exec:java@feature-envy
# Circular Dependencies Code Smell
mvn exec:java@circular-dependencies
Note: Each execution ID runs only the specific code smell demonstration, not all of them.
# Run the main application with all demonstrations
mvn exec:java -Dexec.mainClass="com.cleancode.Main"
# Long Method Code Smell
mvn test -Dtest=LongMethodTest
# Long Parameter List Code Smell
mvn test -Dtest=LongParameterListTest
# Magic Numbers Code Smell
mvn test -Dtest=MagicNumbersTest
# Switch Statements Code Smell
mvn test -Dtest=SwitchStatementsTest
# Feature Envy Code Smell
mvn test -Dtest=FeatureEnvyTest
# Circular Dependencies Code Smell
mvn test -Dtest=CircularDependenciesTest
Java Version Issues:
# Check Java version
java --version
# Ensure Java 21 is active
sdk current java
Maven Compilation Issues:
# Clean and rebuild
mvn clean compile
# Skip tests if needed
mvn compile -DskipTests
Class Path Issues:
# Ensure proper classpath when running
java -cp target/classes com.cleancode.Main