Skip to content

shift-elevate/clean-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Clean Code in Java 🧹

An educational Maven project demonstrating common code smells and their refactoring solutions in Java with practical, real-world examples.

πŸ“‹ Table of Contents

🎯 Project Overview

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

πŸ“‹ Available Code Smells

Size & Complexity Problems

  • 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

OOP Anti-Patterns

  • Switch Statements: Replace Conditional with Polymorphism to eliminate switch statements

Dependency Problems

  • 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

πŸ”§ Prerequisites

  • Java 21 (OpenJDK 21.0.7+)
  • Maven 3.9+
  • IDE (IntelliJ IDEA, Eclipse, or VS Code recommended)

Installing Java 21

# 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

πŸ“ Project Structure

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

πŸš€ Quick Start

1. Clone the Repository

git clone https://github.com/shift-elevate/clean-code.git
cd clean-code

2. Setup Environment (Automatic with SDKMAN)

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

3. Build the Project

mvn clean compile

4. Run the Main Application

mvn exec:java -Dexec.mainClass="com.cleancode.Main"

This will launch the main demonstration showing all implemented code smells and their refactored solutions.

πŸ“š Running Instructions

🎯 Recommended: Run Main Application for Complete Demo

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

πŸ“¦ Advanced: Build and Run JAR

For standalone execution:

# Build executable JAR
mvn clean package

# Run the JAR
java -jar target/clean-code-java-1.0.0.jar

πŸ§ͺ Testing

Run All Tests

mvn test

Run Individual Code Smell Demonstrations

# 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 All Code Smells Together

# Run the main application with all demonstrations
mvn exec:java -Dexec.mainClass="com.cleancode.Main"

Run Specific Test Class

# 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

πŸ› Troubleshooting

Common Issues

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