选择信息丰富的类名称

发布于 2024-11-29 11:30:45 字数 515 浏览 2 评论 0原文

我很难想出信息丰富且有用的类名称。我遇到这个问题的一类向网站/服务器发出请求。我认为将此类命名为 ServerRequest 才合适,它位于 ServerServices 包中。该包还包含类 ServerDataServerImageServerData 解析来自 ServerRequest 的响应并创建对象。 ServerImage 将使用 ServerRequest 将远程图像复制到本地计算机。

我不认为为每个类名添加前缀是一个好习惯。同时,我不想简单地将类命名为 RequestDataImage。如果没有前缀,这些名字就失去了意义,更不用说它们变得多么普遍了。

除了驼峰命名法和使用名词之外,还有哪些好的约定可以使用?

用于上述三个类的类名示例有哪些?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟月 2024-12-06 11:30:45

好吧,如果您要拥有 ServerDataMailDataUserData 那么前缀肯定是知道哪个 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 which Data 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 the something.here.server package and keep simple class names like Request, Data and Image.

It's all about aiming for maximum readability and code clarity.

国产ˉ祖宗 2024-12-06 11:30:45

听起来您有一个返回 ServerResponseServerClient,它又被 ServerResponseParser 解析为 ServerData >。我对“图像”部分不太清楚。它会是一个使用 ServerClient 获取 ServerImageImageRequester 吗?

It sounds like you have a ServerClient that returns a ServerResponse, which is in turn parsed by a ServerResponseParser into a ServerData. I'm not quite clear on the "image" part. Would it be an ImageRequester which uses ServerClient to get ServerImages?

深居我梦 2024-12-06 11:30:45

在我看来,类名需要表达保存/解析/请求的数据,包名对应用程序的功能子集进行分组。

例如,我们在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文