Source

Naming Conventions in Java - GeeksforGeeks

A programmer is always said to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way out to read the code.

人们总是说程序员要写出干净代码, 其中变量的命名是非常重要的,合适命名是更容易阅读代码的前提。

Consider the below illustrations to get a better understanding which later on we will be discussing in detail.

我们可以从下面的一些案例中理解如何更好的,命名:

Class

**If you are naming any class then it should be a noun and so should be named as per the goal to be achieved in the program such as Add2Numbers, ReverseString, and so on not likely A1, Programming, etc. It should be specific pointing what exactly is there inside without glancing at the body of the class.

如果你要命名任何类,那幺它应该是一个名词,所以应该根据进程中要实现的目标命名,如Add2Numbers,ReverseString等,而不应该是A1、B1等。它应该具体指出里面到底有什幺,而不应该像是瞥一眼的班级编号。

Interface

If you are naming an interface, it should look like an adjective such as consider the existing ones:
Runnable, Serializable, etc Try to use ‘able’ at the end, yes it is said to try as there are no hard and fast bound rules as if we do consider an inbuilt interface such as ‘Remote’

如果你要对接口命名,你需要考虑它永远不会存在二义性,比如Runnable, Serializable等等。

尝试使用able作为单词的末尾,使得它看起来就像是没有硬性和快速绑定的规则,就好像我们考虑内置接口。比如Remote

Methods

Consider if you are supposed to create an interface to make read operation then it is suggested as per naming conventions in java to name a similar likely ‘Readable’ interface.

考虑一下,如果你应该创建一个接口来进行读取操作,那幺建议根据java中的命名约定来命名一个类似的可能“可读”接口。

Now if we do look closer(仔细观察) a method is supposed to do something that it does contains in its body henceforth(从此以后) it should be a verb.

如果我们仔细观察一个方法,(方法)应该做一些它确实包含在其正文中的东西,从此他应该是一个动词。

Constants

Naming Conventions in Java

In java, it is good practice to name class, variables, and methods name as what they are actually supposed to do instead of naming them randomly.

在JAVA中,类、方法、变量应该声明为他应该做的事情,而不是胡乱命名。

 Below are some naming conventions of the java programming language. They must be followed while developing software in java for good maintenance and readability of code.

Camel’s case in java programming

驼峰命名法。

consists of compound words or phrases such that each word or abbreviation begins with a capital letter or first word with a lowercase letter, rest all with capital. Here in simpler terms, it means if there are two

Note: Do look out for these exceptions cases to camel casing in java as follows:
注意,下面的几点不应该遵循驼峰命名:

  • In package, everything is small even while we are combining two or more words in java
    在包命名中最好全部使用小写,哪怕是需要连接多个单词也全部小写。
    注意:这一点是因为有一些古老的服务器是对大小写不敏感或者大写包名存在扫描问题的情况不允许使用大写。
  • In constants, we do use everything as uppercase and only ‘_’ character is used even if we are combining two or more words in java.
    只使用下划线作为连接符,并且建议为全大写。

Classes and Interfaces

Class names should be nouns, in mixed cases with the first letter of each internal word capitalized.  Interfaces names should also be capitalized just like class names.

首字母大写,并且混合多个单词首字母都要大写。接口和类名一样遵循同样规则。

Use whole words and must avoid acronyms(首字母缩写) and abbreviations(缩写).

参考案例:

Classes: class Student { }
         class Integer {}
         class Scanner {}
         
Interfaces : Runnable
             Remote
             Serializable

Methods

Methods should be verbs, in mixed case with the first letter lowercase and with the first letter of each internal word capitalized(内部单词大写).

方法名称建议为一个动词,除开首字母应该是小写以外,内部的每个单词都应该大写。

public static void main(String [] args)  {}

Variables

Variable names should be short yet meaningful.

变量名称应该简短并且有意义。

  • Should be mnemonic(记忆) i.e, designed to indicate(指示) to the casual observer the intent(意图) of its use.

容易记忆,并且向阅读人员指示意图。

  • One-character variable names should be avoided except for temporary variables(临时变量).

避免一个字母的变量,除非是临时变量,但是个人认为临时变量也不能丢失含义。

  • Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

临时变量的常用名称是整数的 i、j、k、m 和 n;C、D 和 E 表示字符。

int[] marks;
double double answer,

Constant variables

  • Should be all uppercase with words separated by underscores (“\_”)

应该全大写并且下划线连接多个单词。

  • There are various constants used in predefined classes like Float, Long, String etc.

预定义的类中使用了各种常量,如浮点型、长型、字符串等。(这些要好好利用,并且自定义的时候要避开使用)

num = PI;

Packages

  • The prefix of a unique package name is always written in all-lowercase ASCII letters(ASCII 字符) and should be one of the top-level domain names, like com, edu, gov, mil, net, org.
  • Subsequent(随后) components of the package name vary(变化) according to(根据) an organization’s own internal naming conventions.

唯一软件包名称的前缀始终以全小写的 ASCII 字符,并且应该是顶级域名之一,如 com、edu、gov、mil、net、org。

之后的组件命名约定应该根据公司的内部规定进行变化或者转化。

java.util.Scanner ;
java.io.*;

Note:

  • For class and interfaces, the first letter has to be uppercase. 通常类名的首字母应该大写。
  • For method , variable, package_name, and constants, the first letter has to be lowercase.
    对于方法,变量包名或者常量,首字母应该小写。

Xander
201 声望53 粉丝