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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Optimizing Java Applications for Arm64 in the Cloud
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Top 7 Mistakes When Testing JavaFX Applications

Trending

  • Key Takeaways From Integrating a RAG Application With LangSmith
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • LLM Integration in Enterprise Applications: A Practical Guide
  1. DZone
  2. Coding
  3. Java
  4. Naming Conventions for Parameterized Types

Naming Conventions for Parameterized Types

By 
Tim Boudreau user avatar
Tim Boudreau
·
Sep. 20, 10 · Interview
Likes (0)
Comment
Save
Tweet
Share
17.8K Views

Join the DZone community and get the full member experience.

Join For Free

Parameterized types - the <> expressions that can be used in Java as of JDK 5 are not just for collections. I find myself frequently using them in APIs I design. They really do let you write things which are more generic in the non-Java sense of the word - and the result is more reusable code, which means less code overall, which means fewer bugs and things to test. The verbosity, and some of the weirdness of type-erasure are less than ideal, but used right, the benefits are worth the complexity.

The standard (and somewhere recommended) naming convention for parameterized types is to use a single-letter name. That works fine in signatures that have only one such type. But in practice, single-letter names make code less self-describing, and if you're defining a class with more than one parameterized type, it can be confusing and hard to read. People other than me will have to call, understand and maintain my code - the more self-describing I can make it, the better.

So I am looking for a naming convention that makes it obvious that something is a parameterized type, but allows for descriptive names. I am wondering if anybody else has run into this problem, and if there is any emerging consensus on naming generics. Do you work on a project that uses generics a lot? If so, what do you do?

Here's an example. At the moment, I'm writing a generic (in both senses) class which simply limits the number of threads which can access some resource. It's basically a wrapper around a Semaphore which uses a Runnable-like object to ensure that the Semaphore is accessed correctly, and does some non-blocking statistic gathering about thread contention. So to access the scarce resource, you pass in a ResourceAccessor:

    public interface ResourceAccessor <ProtectedResource, Argument, Result> {
        public Result run (ProtectedResource resource, Argument argument);
    }
The problem is that, when somebody looks at this interface, they will instantly get the idea that there are really classes they need to go find, which are called ProtectedResource, Argument and Result - and of course, no such classes exist - these are just names for generic types.
The standard-naming-convention is worse:
    public interface ResourceAccessor <T, R, S> {
        public S run (T resource, R argument);
    }

Here, nobody could possibly figure out what on earth this class is for without extensive documentation - this is a really horrible idea. So I've concluded that the standard recommendations for generic type names are simply wrong for any non-trivial usage (I.e. Collection<T> is fine, since there is one type and Collections are well-understood). You simply can't do this on a non-collection code structure you have invented, or people will just be confused and not use it.


The best suggestion I've heard thus far is using $ as a prefix:

    public interface ResourceAccessor <$ProtectedResource, $Argument, $Result> {
        public $Result run ($ProtectedResource resource, $Argument argument);
    }

I don't find this pretty, but I don't have any better ideas, and at least it makes it crystal-clear that there is something different about these names.

Any thoughts? What do you do in this situation?

Statistics Java (programming language) Semaphore (programming) Moment Java Development Kit Testing Pass (software)

Opinions expressed by DZone contributors are their own.

Related

  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Optimizing Java Applications for Arm64 in the Cloud
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Top 7 Mistakes When Testing JavaFX Applications

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook