选择信息丰富的类名称
我很难想出信息丰富且有用的类名称。我遇到这个问题的一类向网站/服务器发出请求。我认为将此类命名为 ServerRequest
才合适,它位于 ServerServices
包中。该包还包含类 ServerData
和 ServerImage
。 ServerData
解析来自 ServerRequest
的响应并创建对象。 ServerImage
将使用 ServerRequest
将远程图像复制到本地计算机。
我不认为为每个类名添加前缀是一个好习惯。同时,我不想简单地将类命名为 Request
、Data
和 Image
。如果没有前缀,这些名字就失去了意义,更不用说它们变得多么普遍了。
除了驼峰命名法和使用名词之外,还有哪些好的约定可以使用?
用于上述三个类的类名示例有哪些?
I'm having a hard time coming up with informative and useful class names. One class I am have this problem with makes requests to websites/servers. I thought it would only be fitting to name this class ServerRequest
which is within the package of ServerServices
. This package also contains the classes ServerData
and ServerImage
. ServerData
parses the response from ServerRequest
and creates objects. ServerImage
will copy remote images to local machine using ServerRequest
.
I don't feel like it's good practice to prefix every class name. At the same time I don't want to simply name the classes Request
, Data
, and Image
. Without the prefix the names lose their meaning, not to mention how common they become.
What are good conventions to use, beyond CamelCase and using nouns?
What are some examples of class names to use for the three classes above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,如果您要拥有
ServerData
、MailData
、UserData
那么前缀肯定是知道哪个Data< /code> 我们正在谈论。但是,如果类名混淆不是问题(也取决于项目的规模...),您可以将所有类重新组合到
something.here.server
包下,并保留简单的类名例如请求
、数据
和图像
。这一切都是为了实现最大的可读性和代码清晰度。
Well if you're going to have
ServerData
,MailData
,UserData
then the prefix is certainly a good idea to know whichData
we're talking about. But if class-name confusion isn't a problem (also depends on the scale of the project...) you could regroup all your classes under thesomething.here.server
package and keep simple class names likeRequest
,Data
andImage
.It's all about aiming for maximum readability and code clarity.
听起来您有一个返回
ServerResponse
的ServerClient
,它又被ServerResponseParser
解析为ServerData
>。我对“图像”部分不太清楚。它会是一个使用ServerClient
获取ServerImage
的ImageRequester
吗?It sounds like you have a
ServerClient
that returns aServerResponse
, which is in turn parsed by aServerResponseParser
into aServerData
. I'm not quite clear on the "image" part. Would it be anImageRequester
which usesServerClient
to getServerImage
s?在我看来,类名需要表达保存/解析/请求的数据,包名对应用程序的功能子集进行分组。
例如,我们在 user.data.roster 包中有一个 UserImage,因此与显示用户名册有关的所有内容都在那里。
In my opinion the Classname needs to express what Data is saved/parsed/requested, the Packagename groups a subset of functionality of your application.
So we have a UserImage in package user.data.roster for example, so everything that has todo with showing a roster of the users goes there.