简体   繁体   English

Java代码样式

[英]Java Code Style

Does Java Code Style say anything about numerical suffix in variable naming? Java代码样式是否对变量命名中的数字后缀有任何说明? Which of this two options is acceptable (or maybe both)? 这两个选项中的哪个选项是可以接受的(或可能两者都可以)?

final Computer computer_1 = new Computer();
final Computer computer1 = new Computer(); 

Java doesn't say anything in particular about numbers in their naming conventions. Java在其命名约定中没有特别提及数字。 Although they do mention camelCases to be the general convention for objects like in your case 尽管他们确实提到了camelCases是像您这样的对象的常规约定

final Computer computer1 = new Computer() would be the preferred option here Although its not a good naming practice. 最终计算机computer1 = new Computer()将是此处的首选选项,尽管它不是一个好的命名习惯。 You would generally name your objects to describe their use 通常,您将命名对象以描述其用途

as you can read here , computer1 is the standard. 正如您在这里可以看到的,computer1是标准的。 Underscore should e used only in constants. 下划线应仅在常量中使用。

There is wrong or right. 是对是错。 But if you want to use a standard, I would recommend the Google Java Style Guide 但是,如果您想使用标准,我建议您使用Google Java样式指南

Due to this: Variables in Java should be written in lowerCamelCase. 因此:Java中的变量应使用lowerCamelCase编写。 So it is: 就是这样:

Computer computer1 = new Computer();

If we consider that Oracle's coding style is the official one (except Constants), then underscoring is not official. 如果我们认为Oracle的编码风格是官方的(常量除外),则强调不是官方的。 So the second choice is more acceptable. 因此,第二个选择更可接受。

The name of a variable, function or class should answer all the big questions. 变量,函数或类的名称应回答所有大问题。 It should tell you why it exists, what it does, and how it is used. 它应该告诉您它为什么存在,它做什么以及如何使用。

As computer1 is standard and computer_1 is not, use the always standard version to facilitate the work to future developers that maybe are going to read your code. 由于computer1是标准的,而computer_1不是标准的,因此请使用始终标准的版本,以方便将来可能要阅读您的代码的开发人员进行工作。

You can find lots of java coding style guides if you browse through the internet. 如果您通过Internet浏览,则可以找到许多Java编码样式指南。 some of answers mention here. 这里提到一些答案。 From a different perspective lets see a scenario where name consist of 2 words ( computer one ) instead of computer1 . 从不同的角度来看,让我们来看一个方案,其中名称由2个单词( computer one )而不是computer1

There are 3 ways commonly declare this. 通常有3种方法来声明这一点。

01.camelCase 01.camelCase

Computer computerOne = new Computer();

02.underscore_separated 02.underscore_separated

Computer computer_one = new Computer();

03.nospaces 03.nospaces

Computer computerone = new Computer();

i would say most understandable way of declaration is camelCase. 我会说,最容易理解的声明方式是camelCase。 Secondly no spaces. 其次,没有空格。 Underscore separated method used mostly in pl-sql or any sql related programming languages. 下划线分隔的方法主要用于pl-sql或任何与sql相关的编程语言中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM