Within Classes
Comment
Comment can be used for explaining the code. But what if our written code is self-explanatory, then we don't need to use comment. Overuse or missuse can give worst readiability experience.
Definition: Comment Code Smell refers to the overuse or misuse of comments in the code, indicating that the code might be poorly written or not self-explanatory enough.
While comments can be helpful in explaining complex logic, excessive or unnecessary comments often suggest that the code itself could be improved to become more readable or self-explanatory.
Why Comment Code Smell is a Problem?
-
Redundant Comments: When comments explain something that is obvious from the code itself, they become redundant.
Example:
// This is a constructor for the Customer class
public Customer() {
// Initialize the customer's name to an empty string
this.name = "";
// Set the customer's age to zero
this.age = 0;
}The comments here are stating what is already clear from the code, a constructor. Better Approach:
public Customer() {
this.name = "";
this.age = 0;
} -
Outdated Comments: Comments can become outdated if the code changes but the comments don’t get updated.
Example:
// Fetch the customer's name from the database
String customerName = getCustomerNameFromCache();The comment suggests that the code is fetching the customer's name from the database, but the method is actually fetching it from a cache.
Better Aproach:
String customerName = getCustomerName(); -
Commented-Out Code: Leaving large blocks of commented-out code can cause confusion about whether that code is still needed or not.
Example:
public void processPayment(Payment payment) {
// PaymentGateway gateway = new PaymentGateway();
// gateway.connect();
// gateway.process(payment);
payment.processLocally();
}It’s not clear whether this code should be kept for future use, removed completely, or if it’s obsolete. Better Aproach:
public void processPayment(Payment payment) {
payment.processLocally();
}
Long Method
A long method contains too many lines of code. Any code with more than 25 lines of code should make you question.
Why Long Methods Are a Problem
- Reduced Readability
- Harder to Debug
- Low Reusability
Solution: Split the method into smaller methods, each with a single responsibility.
Long Parameter List
Any function with more parameters is obviously more complex. One of the thumb rules is to use a maximum of 3 to 4 parameters in a function.
Why Long Parameter Lists Are a Problem?
- Reduces Readability
- Increases the Likelihood of Errors
- Difficult to Maintain
Solution: When a set of parameters naturally belongs together, group them into a single object and pass it through the method.
Duplicate Code
Duplicate Code code smell occurs when the same or very similar code appears in more than one place in a program. This is considered problematic because it leads to higher maintenance costs, increases the chances of bugs, and makes code more difficult to modify or extend.
Solution:
- Extract Method: If you have similar blocks of code in different parts of a class, you can refactor that logic into a separate method. If the duplicated code is found across multiple classes, extract the common code into a shared method(in a different class).
- Pull-Up Constructor Body: If multiple classes contain similar behavior, you can use inheritance or interfaces to abstract the shared logic into a base class or interface.
Dead Code
Dead Code code smell refers to parts of the codebase that are never executed or used in the program.
Solution: Just removed it.
Large Class
Large Class code smell occurs when a class in an application contains too many responsibilities or too much code, making it difficult to understand, maintain, and modify. This happens when a class tries to handle more than it should, violating the Single Responsibility Principle (SRP), which states that a class should have only one reason to change.
Solution: Just separate into multiple classes.