Between Classes
Data Class
A Data Class code smell refers to classes that primarily consist of fields, no methods, may associated with getter and setter methods. It have lacking of meaningful behavior or logic. Instead of having methods that operate on their data, they simply store it.
These classes typically violate key object-oriented principles like encapsulation and cohesion. Instead of encapsulating behavior and acting as meaningful abstractions, they act as mere containers for data. They can’t independently operate on the data that they own.
Solution: introduce some behavior and keep some logic inside the class
Data Clumps
Data Clumps code smell occurs when the same group of data items (variables, parameters, fields) tend to appear together in multiple places in the code. Instead of repeating the same set of data multiple times, this group should be encapsulated in a class or structure, which reduces duplication, increases readability, and improves maintainability.
Problem:
public class Order {
private String customerName;
private String customerAddress;
private String customerPhone;
public Order(String customerName, String customerAddress, String customerPhone) {
this.customerName = customerName;
this.customerAddress = customerAddress;
this.customerPhone = customerPhone;
}
public void processOrder(String customerName, String customerAddress, String customerPhone) {
System.out.println("Processing order for " + customerName);
System.out.println("Shipping to: " + customerAddress);
System.out.println("Contact phone: " + customerPhone);
}
public void shipOrder(String customerName, String customerAddress, String customerPhone) {
System.out.println("Shipping order for " + customerName);
System.out.println("Address: " + customerAddress);
System.out.println("Phone: " + customerPhone);
}
}
Solution:
public class Customer {
private String name;
private String address;
private String phone;
public Customer(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhone() {
return phone;
}
}
public class Order {
private Customer customer;
public Order(Customer customer) {
this.customer = customer;
}
public void processOrder() {
System.out.println("Processing order for " + customer.getName());
System.out.println("Shipping to: " + customer.getAddress());
System.out.println("Contact phone: " + customer.getPhone());
}
public void shipOrder() {
System.out.println("Shipping order for " + customer.getName());
System.out.println("Address: " + customer.getAddress());
System.out.println("Phone: " + customer.getPhone());
}
}
Alternative Classes with Different Interfaces
Alternative Classes with Different Interfaces code smell occurs when two or more classes are doing the same or similar jobs but have different method names or interfaces.
Solution: If two classes have similar functionality, merge them into once class with a single interface. If classes perform similar but slightly different tasks, introduce a common interface or abstract class that both classes can implement or inherit from. If the classes can't be merged, you can extract the common functionality into a helper class and delegate the work to it.
Refused Bequest
Refused Bequest is a code smell that occurs when a subclass inherits methods or properties from a parent class but doesn’t use or “want” them. This can happen when a subclass extends a base class but only needs part of its functionality, resulting in an awkward inheritance relationship. The subclass essentially "refuses" some of the inheritance it receives, leading to unused or irrelevant code in the subclass.
This code smell usually indicates poor inheritance design and violates the Liskov Substitution Principle (LSP), which states that objects of a subclass should be able to substitute objects of the base class without altering the correctness of the program.
Solution: refactor the base class into two or more classes and allow the subclass to inherit from the appropriate one or you can use interface.
Lazy Class
Lazy Class is a code smell that occurs when a class doesn’t do enough work to justify its existence. This happens when a class is created for a specific purpose but over time ends up with very few methods or limited functionality.
Soultion: Remove unnecessary part, or merge the use full part with a relevent class
Shotgun Surgery
Shotgun Surgery is a code smell that occurs when a change in one part of the code requires making several small changes across multiple classes or methods.
Solution: Consolidate related behavior into a single class or fewer classes. Group related data into classes, and hide unnecessary dependencies.