我应该遵循 Java 包的命名约定吗?
Do I ought to follow naming conventions for Java packages?
我正在学习 Java 并找到了教程以及 Oracle's website 、com.etc.project.program
中使用的命名约定
我真的必须遵守这个约定吗?
关注和不关注的优缺点是什么?
约定不是强制性的,但有帮助
- 你创造更好的代码
- 其他更容易理解的人:
根据wikipedia:
Reasons for using a naming convention (as opposed to allowing programmers to choose any character sequence) include the following:
- to reduce the effort needed to read and understand source code
- to enable code reviews to focus on more important issues than arguing over syntax and naming standards.
- to enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
- to enhance source code appearance (for example, by disallowing overlong names or unclear abbreviations).
不,您不需要遵循任何约定,您可以编写编译器允许的所有内容。
但是遵循一些约定会让其他人(以及将来您自己)更容易理解您的代码。
正如您在问题本身中所说的,它只是一个 "convention"
Convention is a way in which something is usually done.
这只是一个惯例。是否要遵循它取决于您。 但我们始终建议您遵循这些约定。
Java
中的命名约定
NAME CONVENTION
class name should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
variable name should start with lowercase letter e.g. firstName, orderNumber etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.
constants name should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
包命名约定只是约定。你可以忽略它们,但你不应该。
这些约定有助于防止命名冲突,并使您的代码更易于他人阅读。
一旦您开始与其他开发人员协作,遵循这些约定将使您和他们的工作更加轻松。
而且,它们真的不难理解。
还有其他 Whosebug 页面讨论了这个 here and here。
但如果你还没有看到它们,让我给你一些理由和参考:
如果您只是在做没有其他人会使用的个人项目
代码,然后你可以随意起一个你喜欢的包名。不要化妆
以 com 开头的东西。或净。或其他顶级域名
但是,因为这意味着您拥有域名(即。
使用 com.john 作为你的包名只是因为你的名字恰好
做约翰可不是个好主意)。
如果您要将代码提供给其他人,您应该使用
全局唯一包名称,根据 Java 约定
意味着您应该注册并使用域名。
我正在学习 Java 并找到了教程以及 Oracle's website 、com.etc.project.program
我真的必须遵守这个约定吗?
关注和不关注的优缺点是什么?
约定不是强制性的,但有帮助
- 你创造更好的代码
- 其他更容易理解的人:
根据wikipedia:
Reasons for using a naming convention (as opposed to allowing programmers to choose any character sequence) include the following:
- to reduce the effort needed to read and understand source code
- to enable code reviews to focus on more important issues than arguing over syntax and naming standards.
- to enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
- to enhance source code appearance (for example, by disallowing overlong names or unclear abbreviations).
不,您不需要遵循任何约定,您可以编写编译器允许的所有内容。
但是遵循一些约定会让其他人(以及将来您自己)更容易理解您的代码。
正如您在问题本身中所说的,它只是一个 "convention"
Convention is a way in which something is usually done.
这只是一个惯例。是否要遵循它取决于您。 但我们始终建议您遵循这些约定。
Java
中的命名约定NAME CONVENTION
class name should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc.
interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc.
method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc.
variable name should start with lowercase letter e.g. firstName, orderNumber etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.
constants name should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
包命名约定只是约定。你可以忽略它们,但你不应该。
这些约定有助于防止命名冲突,并使您的代码更易于他人阅读。
一旦您开始与其他开发人员协作,遵循这些约定将使您和他们的工作更加轻松。
而且,它们真的不难理解。
还有其他 Whosebug 页面讨论了这个 here and here。
但如果你还没有看到它们,让我给你一些理由和参考:
如果您只是在做没有其他人会使用的个人项目 代码,然后你可以随意起一个你喜欢的包名。不要化妆 以 com 开头的东西。或净。或其他顶级域名 但是,因为这意味着您拥有域名(即。 使用 com.john 作为你的包名只是因为你的名字恰好 做约翰可不是个好主意)。
如果您要将代码提供给其他人,您应该使用 全局唯一包名称,根据 Java 约定 意味着您应该注册并使用域名。