10 Common Code Smells: How to Spot, Avoid, and Fix Them Effectively
Have you ever looked at a piece of code and sensed that something was just… off? This subtle, unsettling feeling is often a result of code smells. While they don’t break functionality, they hint at deeper issues that can lead to poor performance,...

I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
1. Long Method
public class OrderProcessor {
public void processOrder(Order order) {
validateOrder(order);
calculateDiscounts(order);
applyShipping(order);
logOrderDetails(order);
sendConfirmationEmail(order);
// More functionality continues...
}
}
public class OrderProcessor {
public void processOrder(Order order) {
validateOrder(order);
applyDiscounts(order);
calculateShipping(order);
logOrder(order);
notifyCustomer(order);
}
private void validateOrder(Order order) { /.../ }
private void applyDiscounts(Order order) { /.../ }
private void calculateShipping(Order order) { /.../ }
private void logOrder(Order order) { /.../ }
private void notifyCustomer(Order order) { /.../ }
}
2. Large Class
public class Vehicle {
private Engine engine;
private String gpsLocation;
private String radioStation;
// Methods related to all features of the vehicle, including navigation, engine control, and entertainment...
}
public class Vehicle {
private Engine engine;
private NavigationSystem navigationSystem;
private EntertainmentSystem entertainmentSystem;
// Core vehicle methods only
}
public class NavigationSystem { /.../ }
public class EntertainmentSystem { /.../ }
3. Duplicated Code
public void printReport() {
System.out.println("Report generated at: " + new Date());
}
public void printSummary() {
System.out.println("Report generated at: " + new Date());
}
private void printGeneratedTime() {
System.out.println("Report generated at: " + new Date());
}
public void printReport() {
printGeneratedTime();
}
public void printSummary() {
printGeneratedTime();
}
4. Feature Envy
public class OrderProcessor {
public double calculateDiscount(Order order) {
return order.getPrice() * order.getDiscountRate();
}
}
public class Order {
public double calculateDiscount() {
return this.price * this.discountRate;
}
}
5. Data Clumps
public void createOrder(String product, int quantity, double price) {
// order creation logic
}
public void createOrder(Product product) {
// order creation logic
}
public class Product {
private String name;
private int quantity;
private double price;
}
6. Switch Statements
public double calculateShippingCost(String country) {
switch (country) {
case "USA":
return 10.0;
case "Canada":
return 12.0;
// Additional cases...
}
}
public interface ShippingCalculator {
double calculateCost();
}
public class USAShipping implements ShippingCalculator {
public double calculateCost() { return 10.0; }
}
public class CanadaShipping implements ShippingCalculator {
public double calculateCost() { return 12.0; }
}
7. Lazy Class
8. Primitive Obsession
9. Inappropriate Intimacy
10. Message Chains
11. Conclusion
Read more at : 10 Common Code Smells: How to Spot, Avoid, and Fix Them Effectively





