Avoiding Code Smells In Java
In the Java programming language, the concept of code smells refers to the process of easily understanding user-defined, custom Java program source code, maintaining a source code base, testing and manually debugging large amounts of source code, or enhancing the internal structure of code, which can be complex tasks. While a program may not necessarily have issues or bugs, in most cases, the concept indicates that refactoring the program source code will result in better code.

Some of the Basic Java Code Smells Concept.
| # | Code Smell Method | Short Code Smell Method Example | How To Avoid Situation |
| 1. | Long Method | That type method contains 1000+ program source codes lines | We can split large source code into a smaller block of code methods |
| 2. | Large Class | One of the classes handles database, business logic, and UI interface according to multiple class method | In that it follows single responsibility principal concept method |
| 3. | Duplicate Code | Remember Same code repeated in several user define class methods | In that it extracts common logic into a method/class that use one or time according to use |
| 4. | Magic Numbers | if (employee_age > 21) | Here we use class named constants such as MINIMUM_EMPLOYEE_AGE |
| 5. | Long Parameter List | We can create it with create Client(p,q,r,t,u,v) | In that mode, you can use a multiple class parameter object |
| 6. | Deeply Nested Code | We can use Multiple nested if statements, if necessary, in existing class | For that, we can use a guard clauses and extract class methods |
| 7. | Unused Code | May be class Unused variables/ methods/ imports method depend on user element | Immediately remove all of them from you existing class or program object |
| 8. | God Class | Never hold one class, that does almost hold everything in active class | In that mode we can, divide large program in multiple small responsibilities |
| 9. | Primitive Obsession | In that mode we can Passing many related String/int values users define custom value element | In that mode we can create meaningful domain classes according to program source code need |
| 10. | Switch/if Explosion | Use Large switch handling concept to many object class types | We can use or apply polymorphism or a suitable design pattern for that |
An example of avoiding a long method in a Java program.
Example of Java Smelly Code Concept.
public void processOrder(Order order) {
// validate client order
// total price
// used discount
// saveclient order
// send email to client
// Java users can also add multiple lines at once…
}
Java code designed using the refactoring method.
public void processOrder(Order order) {
testOrder(order);
totalPrice(order);
useDiscount(order);
saveclientOrder(order);
sendEmailMessage(order);
}
In this example, each user-defined class method now has clear program element responsibilities defined.
Avoid magic number examples.
Example of basic Smelly Code Concept.
if (employee.getAge() >= 21) {
permittedEntry();
}
Example of better Smelly Code Concept.
private static final int MINIMUM_EMPLOYEE_AGE = 21;
if (employee.getAge() >= MINIMUM_EMPLOYEE_AGE) {
permittedEntry();
}
In this example, the constant variable element indicates the use or purpose of the 21 employee age.
Example to avoid duplicate code.
Smelly duplicate code.
double countEmployeeSalary(Employee employee) {
return employee.getBasicSalary() + employee.getBonus();
}
double countManagerSalary(Manager manager) {
return manager.getBasicSalary() + manager.getBonus();
}
If the count or calculation in this example is actually declared or defined the same, then you should extract a common class behavior or use a proper shared abstraction method.
double countSalary(Employee employee) {
return employee.getBasicSalary() + employee.getBonus();
}
Example to avoid deep nesting.
Smelly avoid deep nesting code.
if (client != null) {
if (client.isRunning()) {
if (client.hasAllowed()) {
processTask();
}
}
}
Example of better deep nesting code.
if (client == null || !client.isRunning() || !client.hasAllowed()) {
return;
}
processTask();
In the example above, the code is now easier to read, and it reduces the number of nested function functions in the program.
Conclusion of Avoiding Code Smells with Java.
- Java programmers should always follow these 5 SOLID Principles concepts.
- Always keep the declared classes and user-defined methods in the program small and focused.
- Prioritize only useful naming values instead of comments explaining large or confusing program source code.
- Avoid duplicating code functions in unnecessary code smells.
- Use the final reserved keyword to indicate immutability program nature values where appropriate or necessary in the current program.
- Prioritize the concept of composition instead of using excessive inheritance or method use in your larger program.
- Use class interfaces in your program where they provide abstraction that is relevant or meaningful.
- Always manage business logic in your program source code separately from infrastructure problems like database access
- Write unit tests for your large, complex programs to make refactoring more secure and manageable.
- Use available tools like SonarQube, SpotBugs, and Checkstyle to find issues related to your existing program source code maintenance.
In short, in the Java programming language, a good user-defined Java program source code should be readable, easy to maintain, easy to test, and easy to modify. Code smells in Java are alarms or warnings that one or more of these could degrade or damage program source code quality.

