开发者

Java "User" class naming best practice?

I'm about to create a User clas开发者_运维百科s for my project. Any opinions as to whether it is bad practice to create such a commonly-named class? I could complement it with a prefix specific to my project.


This is what packages are for.


I would suggest you to definitely prefix an application specific term to your User class. "User" is way too common and vague. You might easily end up using API which has "User" class or Interface. Though latest IDEs make it relatively easy to refactor your code, it would be cleaner and easier if you have a app specific "User" class.


Using common names is not a bad practice, until and unless you describe the name as that to the functionality of the class. keep in mind to create proper packages that can define your class more specifically.


I always try to use common names like User for clarity. Just be sure the names are drawn from the problem domain and existing project vocabulary. Be alert for ambiguities, and if one arise, change the class name. Modern IDEs with automated refactoring support make that easy.


You should use Java packages to avoid name clashes. There is nothing wrong with using a common name, just be sure to put it in its own package. For example, you could have the following structure:

com/
  yourwebsite/
      yourproject/
            userdata/
                 User.java

You then begin the file "User.java" with:

package com.yourwebsite.yourproject.userdata;

When you import it, you would use:

import com.yourwebsite.yourproject.userdata.User;


This name is unlikely to be used by any public API, so it's ok for an app to use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜