DEV Community

Muzammil
Muzammil

Posted on

Understanding Code Smell

What is Code Smell?

Code smell refers to any symptom in the source code that may indicate a deeper problem.
While it may not immediately cause bugs, it points to weaknesses in the code's structure or design that could make the system harder to maintain, read, and extend in the future.

Why is Understanding Code Smell Importan?

Uderstanding code smells is essential because it helps us:

  • Maintain code quality.
  • Facilitate code maintenance and future development.
  • Reduce the chance of introducing bugs.
  • Avoid problems caused by poor design choices.
  • Build a professional portofolio as backend developer.

Here are some of the most common types of code smell, along with brief explanations and simple examples.

🔹 Duplicated Code

This happens when the same code appears in multiple places. it makes maintenance harder because if something change, you have to update it everywhere.
Example:

public int add(int a, int b) { 
    return a + b;
}

public int addThree(int a, int b, int c) { 
    return a + b + c; 
}
Enter fullscreen mode Exit fullscreen mode

Solution: Combine the duplicated logic into one flexible method.

🔹 Long Method

A method that's too long and does too many things is hard to understand and test.
Example:

public void processOrder() {
    validateOrder();
    calculateDiscount();
    applyTax();
    generateInvoice();
    sendNotification();
}
Enter fullscreen mode Exit fullscreen mode

Solution: Break it into smaller methods, each with a single, clear responsibility.

🔹 Feature Envy

This occurs when a method relies too much on another class's data or behavior.
Example:

public String getCustomerInfo(Customer customer) {
    return customer.getName() + ", " + customer.getAddress();
}
Enter fullscreen mode Exit fullscreen mode

Solution: Move this logic to the relevant class (in this case, Customer).

🔹 Large Class

A class that has too many responsibilities can become hard to manage.
Example:

public class OrderManager {
    void createOrder() {}
    void cancelOrder() {}
    void calculateTotal() {}
    void sendNotification() {}
}
Enter fullscreen mode Exit fullscreen mode

Solution: Split it into smaller classes, each handling one responsibility.

🔹 Primitive Obsession

Using primitive types (like String or int) instead of creating classes to represent complex concepts.
Example:

public class User {
    private String phoneNumber;
    private String email;
}
Enter fullscreen mode Exit fullscreen mode

Solution: Create a ContactInfo class to encapsulate contact details.

🔹 Long Parameter List

A method that takes too many parameters can be confusing and hard to work with. It's easy to forget the correct order of the parameters, and it clutters the method signature.
Example:

public void createUser(String name, String email, String phone, String address, String city, String postalCode) {
    // logic
}
Enter fullscreen mode Exit fullscreen mode

Solution: Group related parameters into a class or object, such as a UserProfile or ContactInfo.

Consolusion

Code smells are not bugs, but they signal that the code could be improved. By recognizing and addressing these semlls, you can write cleaner, more maintainable, and professional code.

Top comments (0)