0

Should I avoid naming my classes the same as classes in other external packages/libraries at the expense of sacrificing name conciseness and adding a little bit of complexity?

For instance, in my application I have a package named UIComponents that holds reusable components that certain parts of the GUI can use for common tasks. For example Form, Grid, Window. Since I am writing a simulator, should I use instead something like SimForm, SimGrid and SimWindow to avoid naming them the same as the components in some libraries I use?

carlossierra
  • 4,479
  • 4
  • 19
  • 30
  • 2
    as soon as your classes in different package - its fine, in case of same package- you have class path conflict, which could lead to epic failure – Iłya Bursov May 18 '16 at 20:08
  • 2
    Unique enough to not conflict with classes that are likely to be used with it. For example, don't name it the same as any basic JVM class (like [StringBuilder](http://stackoverflow.com/q/37174726/836214)) – Krease May 18 '16 at 20:15
  • 1
    I tend to not worry about the same name until I find myself having to specify the full package.class name due to importing/using the same named class from elsewhere. Then, I rename to avoid the ugliness! – Daniel Widdis May 18 '16 at 21:29

4 Answers4

4

Opinion-based, perhaps, but here's what I think: Is it going to be confusing?

When I make card games, I tend to have a class named Card. When I try to use it for the first time in a file, my IDE asks me, "Do you mean my.package.Card or javafx.smartcardio.Card?" Well, obviously I have no interest in whatever smartcardio is, so it's not a problem for me to use Card as a name.

Now suppose I was making a house designer using JavaFX. JavaFX has a class called Window, and I might use it for my application. Because of this, I'd say I want to avoid naming my own custom window class Window, and use something else like, HouseWindow...or something like that.

Zircon
  • 4,677
  • 15
  • 32
1

If your classes are replacements or extensions of classes in another package, and perform a similar function, then there is value in creating your own name prefix, exactly as you have suggested. Seeing SimForm, SimGrid directly in my code is a very helpful reminder to me that I am using your classes and not the JDK classes.

Paul Galbraith
  • 128
  • 1
  • 10
1

Although there are some conventions that you should check, about this subject, in my opinion, it is up to you because you are the one with the responsibility to know business details and to make your code clean, readable and capable of being maintained in the future.

Your class names should identify easily and with no doubts what business entity you are dealing with and every class that is not related (directly) with your business should have an even better name.

Even though there is no problem naming your class with a name that exists in one library, you should only do it if you think that it is the correct name and that no other would explain the concept better.

Xavier Silva
  • 267
  • 1
  • 19
0

You can have any number of class of same name until they all are in different package.

In daily life, we try to make class unique in each module/component. In rare scenario we will have to use the exact same name.

sauumum
  • 1,638
  • 1
  • 19
  • 36