Should I avoid commonly used class names?

Some class names are so “generic” that they are often found in several different packages, including in libraries and application code. Some examples:

  • Comment
  • Component
  • Factory
  • Location
  • Region

In my IDE, attempting to auto-complete the import for a class like one of these summons several competing suggestions.

When naming classes, is it a good idea to avoid class names already used elsewhere?

For some of these examples, I would imagine that using such class name is discouraged because it is simply not meaningful enough (e.g. Factory), but I am wondering whether it is discouraged to use a class name because it is used (frequently) elsewhere.


You should use class names where they make the most sense for you. None of the names above that you’ve proposed are off limits, and there’s no reason why you can’t use them (assuming a language that supports namespaces and can avoid naming conflicts in this way).

However, you may consider drilling down to class names that are more specific and precise, which will better describe the meaning of the objects in your code. For example:

  • Instead of Comment: LineComment or BreakComment could easily be class names in a compiler project where you would like to create semantic blocks for comments.
  • Instead of Component: ListComponent, CalendarComponent, or ViewComponent make particular sense when implementing a UI library where you have class-based components.
  • Instead of Factory: PizzaFactory makes more sense if you’re trying to make pizzas!
  • Instead of Location: GeographicLocation or SemanticLocation makes more sense when implementing a directions based navigation app, and you’re trying to distinguish between ’45 deg N, 77 deg W’ and ‘next to the pizza place’.
  • Region: CodeRegion could be used in a compiler, and GeographicRegion could be used in a Maps app.

If you’re afraid to be specific, namespaces and packages help. However, there is nothing discouraging you from using the same name for a class as another package where it makes sense. The class names specifically aren’t copyrighted, and most IDEs now are smart enough to make distinctions between what packages you’re referring to when using autocompletion.

For the most part, specificity is helpful in assisting other developers to read your code, which every developer can appreciate!

Leave a Reply

Your email address will not be published. Required fields are marked *