• 周四. 10 月 3rd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java Naming Rules

King Wang

1 月 3, 2022

At first sight Java It’s hard not to be confused when it comes to code , A pile of words , There’s in both case , What rules should there be . Let’s introduce Java The naming rules of each item in .

Tips: Naming rules are just a general habit , There is no mandatory provision , Feel free to . It is suggested to follow the rules , Make the code more beautiful 、 Easier to read .

class (class) The name of

According to the contract ,Java Class names usually start with uppercase letters , If the class name consists of more than one word , Then the initial of each word should be large Write for example TestPage; If the class name contains word abbreviations , Then every letter of the word should be capitalized , Such as :XMLExample, Another naming technique is that classes are designed to Representing the object , So when naming a class, you should try to choose nouns .   for example : Graphics

The name of the package


It is recommended to name the package (package) All in lowercase . Because every programmer can write his own class , And programmers are basically reluctant to name packages with their own names , Therefore, it is recommended to name the package with your own domain name . for example :
com.mydomain.javaproj

Method name

The first letter is lowercase , The rest of the words are capitalized . Such as :printWords

Name of constant

For constants , It should all be in capital letters . If there are more than one word , Underline is recommended between every two words _ Split up . Such as :MAX_VAL

Variable name

There are three main naming conventions :
Camel Tagging : The initial is lowercase , The next words start with capital letters
Pascal Tagging : The initial is in capital , The next words start with capital letters
Hungarian notation : In order to Pascal The tag method variable is preceded by a lowercase sequence indicating the type of the variable

stay Java We usually use Hungarian notation , The basic structure is scope_typeVariableName, it Use 1-3 Character prefixes to represent data types ,3 The prefix of characters must be lowercase , The prefix is followed by a name made up of one or more ideographic words , And the first letter of each word is capitalized , Other words Mother small , This ensures that the variable name can be correctly broken . for example , Define an integer variable , Used to record the number of documents :intDocCount, among int Indicates the type of data , The back is the table The English name of Italy , Capitalize the first letter of each word . such , In a variable name can reflect the type of variable and the meaning of the value stored by the variable , This makes the code statements readable 、 Easier to understand . byte、int、char、long、float、 double、boolean and short.

data type / Prefix ( attach )
byte b
char c
short sh
int i
long l
char c
string s
float f
double d
hashtable h
[] arr
List lst
Vector v
StringBuffer sb
Boolean b
Byte bt
Map map
Object ob

For global variables to be used within multiple functions , Add… To the front “g_”. For example, a global string variable :g_strUserInfo.

Pay attention to the following points when naming variables :

· Choose a meaningful name , Note that the initial of each word should be capitalized .

· In a function, the same variable is not used to represent two values with different meanings .

· i、j、k And so on are just loop index variables for small loops .

· Avoid using Flag To name state variables .

· use Is To name logical variables , Such as :blnFileIsFound. By naming Boolean variables in a positive form , So that other developers can more clearly understand the meaning of Boolean variables .

· If necessary , Add calculation qualifier at the end of variable , Such as :curSalesSum.

· Names do not contain ,curSales and curSalesSum.

· static final Variable ( Constant ) Our names should all be capitalized , And indicate the full meaning .

· If you need to abbreviate the variable name , Be sure to pay attention to the consistency of abbreviation rules in the whole code . for example , If used in some areas of the code intCnt, And in other areas, we use intCount, It adds unnecessary complexity to the code . It is recommended to avoid abbreviations in variable names .

· By placing a quantifier at the end , You can create more uniform variables , They are easier to understand , It’s also easier to search . for example , Please use strCustomerFirst and strCustomerLast, Instead of using strFirstCustomer and strLastCustomer. often The quantifier suffixes used are :First( The first of a set of variables )、Last( The last of a set of variables )、Next( The next variable in a set of variables )、Prev( Top… Of a set of variables One )、Cur( The current variable in a set of variables ).

· Choose the best data type for each variable , This can reduce the demand for memory , Speed up code execution , It will reduce the possibility of mistakes . The data type used for the variable may affect the result of the variable’s calculation . under these circumstances , The compiler will not generate runtime errors , It just forces the value to fit the data type . This kind of problem is very difficult to find .

· Try to narrow down the scope of variables . If the scope of a variable is larger than it should be , Variables can continue to exist , And it still takes up resources for a long time after the variable is no longer needed . Their main problem is , Any class Any of the methods in can modify them , And it’s hard to track exactly where the changes were made . Taking up resources is an important issue in scope . For variables , Minimize the scope of the application Reliability has a huge impact .

Parameter name

The naming standard of parameters is the same as that of methods , And to avoid confusion when reading the program , Please make the parameter name as clear as possible under the condition that the parameter name is one word .

Javadoc notes

Java In addition to the usual way of commenting ,Java The language specification also defines a special annotation , That’s us said Javadoc notes , It’s used to record… In our code API Of .Javadoc A comment is a multiline comment , With /** start , And then */ end , Annotations can contain some HTML Markers and special keywords . Use Javadoc The advantage of annotations is that they can be automatically converted to online documents , Save the trouble of writing program documents alone . 

for example :

/** 

* This is an example of 

* Javadoc 

* @author darchon 

* @version 0.1, 10/11/2002 

*/

At the beginning of every program , Commonly used Javadoc Comment on the overall description of the program and copyright information , Then in the main program For each class 、 Interface 、 Method 、 Field add Javadoc notes , The beginning of each comment begins with a sentence summarizing the class 、 Interface 、 Method 、 What the fields do , This sentence should be in a single line to highlight its general effect , You can follow this sentence With a more detailed description of the paragraph . After the descriptive paragraphs, you can also follow some with Javadoc Note the special paragraph at the beginning of the label , For example, in the example above @auther and @version, this Some paragraphs will be displayed in a specific way in the generated document .

发表回复