DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Trending

  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Grafana Loki Fundamentals and Architecture
  1. DZone
  2. Coding
  3. Java
  4. Smells in Java Code: Do You Recognize Them?

Smells in Java Code: Do You Recognize Them?

A look at ten common code smells, or code that violates design principles, in Java. Recognize any in your applications?

By 
Ganesh Samarthyam user avatar
Ganesh Samarthyam
·
Aug. 18, 16 · Opinion
Likes (37)
Comment
Save
Tweet
Share
53.8K Views

Join the DZone community and get the full member experience.

Join For Free

Smells are structures in code that violate design principles and negatively impact quality [1]. Here are some of the bad smells in Java code. 

1. Constant Interface. Constant interfaces have only static final data members declared in them without any methods. Suggested refactoring for this smell depends on kind of constants present in the constant interface: the constants can get added as members in the class or can be rewritten as enums. The interface java.io.ObjectStreamConstant is an example for this smell. 

2. Global Variable Class. The class has one public static (non-final) field. Since its freely available for modification by anyone, it becomes an equivalent to a C-like global variable, with only difference that the variable name is prefixed by the class name, thus avoiding name-clashes. Here is an example: 

class Balls {
    public static long balls = 0;
}

3. Global Function Class. It is a public class that has only one static public method and no other fields or methods; it can have an optional private constructor to disallow instantiation. It is equivalent to a C-like global function, except that the function needs to be prefixed by the class name to access the function.  

4. Publicly Exposed Fields. It is a public class with public non-final, non-static data members and no methods (with an optional constructor). It is difficult to maintain public, C-like classes, as Effective Java [2] notes: "Several classes in the Java platform libraries violate the advice that public classes should not expose fields directly. Prominent examples include the Point and Dimension classes in the java.awt package. Rather than examples to be emulated, these classes should be regarded as cautionary tales."

5. Orphan Abstract. An abstract class does not have any concrete derived classes. An abstract class can be used meaningfully only when implemented by concrete derived classes. If the abstract class is not useful, it can be removed. If the class can be instantiated consider, making it concrete. If it represents an useful abstraction, provide one or more concrete classes that implement that abstract class.

6. Forgotten Interface. A class implements all the methods with the same signatures as the methods listed in an interface. It is a likely mistake that the class intended to implement an interface, but forgot to list the interface as its base type. A consequence of this smell is that the objects of the class cannot be treated as subtype of the interface and hence the benefit of subtyping and runtime polymorphism is not exploited.

7. Clone Class. A Clone class is an exact replica of another class (unrelated by inheritance). Essentially, only the name of the class is different, but all its members, their signature, accessibility, etc. are the same. A constraint is that at least one member should be present in the class. An exception is that order of its members might be different.

If two or more classes have common data and behavior, they should inherit from a common class that captures those data and behaviors. So, one fix is to check if it is possible to provide a common base class and make the clone classes as derived classes. Clone classes often occur because of copy-pasted code; in that case, it is better to remove such duplicate classes and instead use a unique class.

8. Lonely Class. The class does not depend on or use any other class; also, no other classes depend on or use the class. This might not really be a mistake or error in design; however, it is rare to see a very independent class that neither uses other classes nor used by any other classes. So, it is possible that it is a design mistake. 

9. Abstract Leaf. Abstract classes are most meaningful and useful when we have them near the roots of a hierarchy. In another extreme, it is meaningless to have them as leaves in a hierarchy: they are useless and can be eliminated; abstract leaf classes usually indicate a design mistake.

10. Tagged Class. This smell often occurs in programs written by programmers from structured programming background. They often define classes like structs and instead of providing an inheritance hierarchy of related types and use runtime polymorphism, they often have an enumeration to list types and have switch-case statements (or chained if-else statements) to distinguish between the types to do type specific operations.

The term 'tag class' is from "Effective Java" [2]: "it is a class whose instances come in two or more flavors and contain a tag field indicating the flavor of the instance''. 

A detect a tag class in code, a check is made for the following features:

  • An enumeration (or public static final ints) to indicate the flavour of the instance 
  • A field to store the value of the enum/int; typically, the constructor sets this field 
  • A switch statement in one or more methods which execute code based on the tag value 

Obvious refactoring of the tagged class is a class hierarchy.

Do you recognize them from your experience? What are the smells that you commonly see in Java code that are missing in this list? 

There are numerous static analyzer tools (FindBugs, PMD, etc) available for detecting "bug patterns" in Java. However, it is surprising that there aren't many tools available that are dedicated to detecting such code smells. Hope some tools will emerge in near future.  

References: 

[1] “Refactoring for Software Design Smells: Managing Technical Debt”, Girish Suryanarayana, Ganesh Samarthyam, Tushar Sharma, ISBN - 978-0128013977, Morgan Kaufmann/Elsevier, 2014. 

[2] "Effective Java" (2nd Edition), Joshua Bloch, Addison-Wesley, 2008. 

 

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!