您在 Java 中的个人/爱好项目中使用什么包命名约定?
我已经熟悉使用域名创建唯一包名称的标准 Java 包命名约定(即包 com.stackoverflow.widgets)。 但是,我从未见过任何关于如何为个人项目选择包名称的建议。 我认为这是因为这确实是个人品味的问题。
那么,如何为永远不会投入生产的个人项目选择包名称(您可能在业余时间尝试新的框架)。 假设您没有可以使用其域来创建包结构的个人网站,您会做什么(或将做什么)? 您是否有一个逻辑系统来为业余爱好项目生成新的包名称,或者您是否只使用简单的一次性包名称,例如 mypackage
?
因为我只是想知道不同的人对此有什么想法,所以我将其设为社区维基。
就我个人而言,我从来没有考虑过太多,但今晚我想玩一下 Wicket ,它我突然意识到我不清楚如何组织我的爱好项目。 业余爱好项目的单独、独特的包命名约定(至少在我看来)将是保持个人和工作相关代码彼此清晰分开的好方法。
我正在考虑一个简单的分层命名约定,将我的个人项目的源代码保存在单个根文件夹中:
- 使用
myprojects
作为根文件夹 - 附加项目名称
- 添加任何其他子包名称
所以,我的 Wicket项目将位于包 myprojects.learningwicket
中,单元测试将位于包 myprojects.learningwicket.tests
中(例如)。
I'm already familiar with the standard Java package naming convention of using a domain name to create a unique package name (i.e. package com.stackoverflow.widgets
). However, I've never seen any recommendations for how to choose package names for personal projects. I assume because this is because this is really a matter of personal taste.
So, how do you choose package names for personal projects that will never make it into production (you might be experimenting with a new framework in your spare time). Assuming you don't have a personal website whose domain you can use to create your package structure, what do (or would) you do? Do you have a logical system in place for generating new package names for hobby projects, or do you just use simple throw-away package names like mypackage
?
Since I'm just curious to see what different people's thoughts are on this, I've made this a community wiki.
For me personally, I've never given it much thought, but I wanted to play around with Wicket tonight and it occurred to me that I don't have a clear idea of how I want to organize my hobby projects. A separate, distinct package naming convention for hobby projects (in my mind, at least) would serve as a good way to keep personal and work-related code clearly separate from each other.
I was thinking of a simple hierarchal naming convention, to keep the source for my personal projects in a single root folder:
- Use
myprojects
as the root folder - Append the project name
- Add any additional subpackage names
So, my Wicket project would be in the package myprojects.learningwicket
and unit tests would be in the package myprojects.learningwicket.tests
(for example).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我的命名:
prj.project_name
。My naming:
prj.project_name
.我使用我的 OpenID URL,然后附加我的项目名称。 例如,
com.myopenid.cd1.twitter
是我一直在开发的 Twitter 客户端的根包。I use my OpenID URL and then I append my project's name. for example,
com.myopenid.cd1.twitter
is the root package of a Twitter client I've been developing.我只是使用我的名字:surname.initials.xxx,作为简洁和避免冲突之间的一个很好的平衡。 我认为如果我选择公开发布代码,这将提供一个合理的无冲突命名空间。 我还编写了一个小程序,它可以重新打包整个目录树,所以我想我是否需要重新打包以发布它,这是非常轻松的......因此我没有为此失去太多睡眠。
在 surname.initials.xxx 之后,我使用 app 表示应用程序包,使用 lib 表示库包,使用 tst 表示我纯粹正在尝试的东西。
I just use my name: surname.initials.xxx, as a nice comprise between brevity and collision avoidance. I figured that would give a reasonable collision free namespace should I ever choose to publicly publish the code. I also have a little program I've written which can repackage entire directory trees so I figured should I ever need to repackage for publishing it's quite painless... therefore I didn't loose too much sleep over it.
After the surname.initials.xxx, I use either app for application packages, lib for library packages, and tst for stuff I am purely experimenting with.
您对姓氏.名字.项目有何看法???
喜欢 luz.marlon.project 吗?
what you think about lastname.firstname.project ???
like luz.marlon.project ?
我想问同样的问题。 到目前为止,我已经使用了前缀 com.tehvan,尽管我实际上并没有公司。
I thought about asking this same question. So far i've used the prefix com.tehvan, even though i don't actually have a company.
如果您只是做个人项目,其他人不会使用代码,那么您可以编写您喜欢的包名称。 不过,请勿编写以
com.
或net.
或其他顶级域开头的内容,因为这意味着您拥有该域名(即使用com.john
作为你的包名称,仅仅因为你的名字恰好是 John,这不是一个好主意)。如果您要将代码提供给其他人,您应该使用全局唯一的包名称,根据 Java 约定,这意味着您应该注册并使用域名。
If you're just doing personal projects where nobody else will use the code, then you can make up a package name that you like. Don't make up something that starts with
com.
ornet.
or other top-level domain though, because that would imply that you own the domain name (ie. usingcom.john
as your package name just because your name happens to be John is not a good idea).If you're going to give the code to anybody else, you should use a globally unique package name, which according to Java conventions means you should register and use a domain name.
我只使用我的姓名首字母:fg.nameofproject.etc
它减少了打字。
它可以随时以 sf.net 或 com 为前缀。 或组织。 或 com.google..
由于该项目是个性化的,因此请特别对待它,就像您新熨烫的个性化礼品衬衫一样 - 感觉会很好。
I just use my initials: fg.nameofproject.etc
It reduces typing.
It can be prefixed at any time with sf.net or com. or org. or com.google..
As the project is personal treat it special just like your freshly pressed personalized gift shirt - it will feel good.
<讽刺>
呸。 任何有自尊心的程序员都会拥有自己的域名。 这显然是一个刁钻的问题。 每个人都有自己的个人域名!:-)
好吧,严肃地说,购买自定义域名可能是最简单的选择。 每年花费大约 10 美元,您就可以找到信誉良好的提供商来托管域名和转发电子邮件。
<sarcasm>
Bah. Any self-respecting programmer would have his or her own domain name. This is clearly a trick question. Everyone has their own personal domain name!</sarcasm>
:-)Ok, in all seriousness, buying a custom domain name is probably the easiest option. For about $10 a year, you can find reputable providers to host the domain and forward email.
我的大部分爱好项目都存储在 Google Code 中,因此我只使用项目站点作为包名称:
com.googlecode.donkirkby.someproject
。I store most of my hobby projects in Google Code, so I just use the project site as the package name:
com.googlecode.donkirkby.someproject
.我的名字通常是 anjan
,我使用 com.anjan
我有一个自己的幻想公司——有时我使用
sourceforge 的传统(如 hibernate 和其他软件包所示)是 net.sf.*
所以,这取决于你的心情,你可以接受。
my name is anjan
usually, I use com.anjan
I have a fantasy company of my own -- sometimes I use that
the tradition with sourceforge(as hibernate and other packages showed) is net.sf.*
so, depending on your mood, you can go with that.
我想你已经解决了。 这里要避免的诱惑是根本不关心包名称。 节省几次击键很容易,因为“我只是编写一些测试代码”。 但随后代码变得良好、有用且庞大,然后您意识到您为可能长期存在的库或应用程序奠定了坚实的基础。 它可能不是一个库或一个应用程序,永远不会离开您的家庭网络,但关键是,您没有提前考虑。 这就是计算机科学的丹麦幽灵——永远提前思考,哪怕只有一点点。
我的爱好代码使用的命名约定与您的非常相似。 我有一个名为“futura”的顶级目录(产生该名称的又长又无聊的原因),我的所有代码都挂在其中。 我确实尝试将我的代码组织到包库中,即使它可能是我从未在其他项目中使用的类或包。 我将所有应用程序(即类中具有 void main(String[] args) 的任何应用程序)放在 futura.app.* 文件夹中。 我还尝试为我自己的代码模仿标准 Java 库包名称,尽管在一些情况下,我因为自己的品味而打破了惯例(即,futura.inet 用于互联网,而不仅仅是套接字、代码和 futura.collections 用于非-util 的东西。)套用 David Mamet 的话:永远要通用化。 始终保持通用性!
从您发布问题时的谨慎来看,我怀疑您也同意我的最后一点:您不必将业余黑客攻击视为企业级项目,但如果如果你把一些纪律带入主场比赛,这种爱好就会更有价值。
I think you've got it worked out. The temptation to avoid here is to not bother with a package name at all. It's easy to save a few keystrokes because "I'm just writing some test code." But then the code gets good and useful and large, and then you realize you have a solid start for what may be a long-lived library or application. It may not be a library or an application that ever leaves your home network, but the point is, you've not thought ahead. That's the Denmark ghost of computer science -- always think ahead, if only a little bit.
The naming convention I use for my hobby code is very much like yours. I have a top-level directory named "futura" (long, boring reasons why that name came about) that all my code hangs off of. I do try and organize my code into package libraries, even if it might be a class or package I never use for another project. I place all applications (that is, anything that has a void main(String[] args) in the class) in the futura.app.* folder. I also try to mimic the standard Java library package names for my own code, although in a couple of cases I broke convention because of my own tastes (i.e. futura.inet for Internet, not merely socket, code, and futura.collections for non-util stuff.) To paraphrase David Mamet: Always be genericizing. Always be genericizing!
From the care you used to post the question, I suspect you agree with my last point as well: You don't have to treat hobbyist hacking as an enterprise-level project, but if you bring some of that discipline to the home game, the hobby is all the more rewarding.