Java package naming rules

Source: Internet
Author: User
Turn to a package naming Rule During J2EE development to develop good development habits

CodeStandardized purpose: to standardize the coding process for futureProgramDevelop good behavior habits during development.
Standardized coding scope: J2EE project development.
Package naming rules:
Purpose: The package naming rules should reflect the good division of project resources.

Servlet class package naming convention: company name. Development Group name. Project name. Web. servlet
Example: net. linkcn. Web. servlet

Custom label class package naming rules: company name, Development Group name, project name, Web. tags
Example: net. linkcn. Web. tags

Naming rules for packages containing filter classes: company name. Development Group name. Project name. Web. Filter
Example: net. linkcn. Web. Filter

Action class package naming convention: company name. Development Group name. Project name. Web. Struts. Action
Example: net. linkcn. Web. Struts. Action

Naming rules for the packages containing the actionform class: company name, Development Group name, project name, Web. Struts. Form
Example: net. linkcn. Web. Struts. Form

Naming rules for the package where the JavaBean resides: company name, Development Group name, project name, Web. Struts. Service. impl
Example: net. linkcn. Web. Service. impl

JavaBean implementation interface naming convention: company name. Development Group name. Project name. Web. Service
Example: net. linkcn. Web. Service

Naming Convention for the DAO class package: company name. Development Group name. Project name. Dao. impl
Example: net. linkcn. Dao. impl

The name of the interface implemented by the DAO class in the package: company name, Development Group name, project name, Dao
Example: net. linkcn. Dao

Naming rules for pojo classes and HBM files: company name, Development Group name, project name, Dao. HBM
Example: net. linkcn. Dao. HBM

Naming rules for packages of global public classes and interface classes: company name, Development Group name, project name, global
Example: net. linkcn. Global

Name of the global tool package: company name, Development Group name, project name, util
Example: net. linkcn. util

Class naming rules
Basic naming rules:

Class and Interface Name

Naming rules: it must start with an uppercase letter. If multiple words exist, each word must start with an uppercase letter.
Example: studentinfo
Interface Name

Naming rules: start with an uppercase letter "I". If multiple words exist, each word header is capitalized.
Example: istudentinfo

Interface implementation Class Name:
Naming rules: remove the first letter "I" of the implemented Interface Name and end with "impl". If there are multiple words, each word header is capitalized.
Example: studentinfoimpl

J2EE + SSH framework naming rules
Servlet Class Name:
Naming rules: end with a servlet word
Example: loginservlet

Pojo Name:
Use the class automatically generated by hibernate.

DAO class name:
Use the class automatically generated by hibernate.

Action Class Name:
Naming rules: the action name is determined by the pojo name, And the pojo name is action.
For example:
A pojo is named diary and its corresponding action is diaryaction.

Actionform Class Name:
Naming rules: the actionform name is determined by the pojo name, And the pojo name is form.
For example:
A pojo is named diary and its corresponding actioform is diaryform.

Business logic interface Name:
Naming rules: the name of the business logic interface is determined by the pojo name, And the ipojo name is service.
For example:
A pojo is named diary and its business logic interface is idiaryservice.

Business Logic implementation Class Name:
Naming rules: the name of the business logic interface implementation class is determined by the pojo name.
For example:
A pojo name is diary, and the corresponding business logic interface implementation class is diaryserviceimpl

Class variable name:
Naming Convention: the first letter of a variable name must be in lower case. If the variable name contains multiple words, the first letter of a single word is in upper case. Do not use "_" to connect words with words, the variable name access control must be private. You can add the setter and getter methods to it.
For example:
Private int studentage;
Public int getstudentage (){
Return studentage;
}
Public void setstudentage (INT studentage ){
This. studentage = studentage;
}

Constant Name:
Naming rules: All letters are in uppercase. If there are multiple words, the words are separated. The variable must be of the public, static, and final types.
Example: public static final string user_name = "username";

Method Name
Naming Convention: the first letter must be in lower case. If the variable name contains multiple words, the first letter of the subsequent words is in upper case. Do not use "_" for connection between words. Do not use nouns for words.
Example: Public int checklogin (string name, string PWD ){}

Annotation specification: The annotation specification is the most important component of the entire development specification and must be strictly enforced.

Class annotation:
Purpose: annotate the entire class and briefly describe the role of this class.
Writing rules: Class comments must be written before the class declaration syntax. In the comments, describe the base function, author, date, version, company name, and copyright of the class.
Format:

Class declaration syntax

For example:

Public class admindao

Comments on variables and constants:
Purpose: Briefly describe the meaning of the variable.
Writing rules: Variable comments must be written before the definition of variables to briefly describe their meaning.
Format:

For example:

Public int age;
Method description:
Purpose: Briefly describe the function of this method, and describe its parameters and meanings of returned values.
Writing rules: the method comment must be written before the method definition. The comment includes a simple description of the function of the method, a simple description of the parameters of the method, the type of the returned value, and the meaning of the returned value.
Format:

For example:

Public booleaneditadminpassword (INT adminid, string oldpassword,
String password) throws userexception, serviceexception;

JSP page Name:
Naming rules: the JSP page name must start with a lowercase letter. If there are multiple words, the following words start with an uppercase letter. The name must reflect the meaning of the page. It is best to associate it with the module name.
For example:
Login. jsp -- logon page
Register. jsp -- registration page
Message. jsp -- customer message page

J2EE project folder organization specifications:
Objective: To standardize the student's Web Application resource organization form and form a good document organization habit. The document should be organized to reflect the division of modules.

According to the features of the eclipse tool, the directory structure of the project is as follows:
SRC
---- Store java files
Webroot
| -- Images -- stores public images required by web programs
| -- CSS -- stores the public style sheets required by the web program
| -- JS -- stores the public JS files required by the web program
| -- Commons -- stores public files required by web programs
| -- Function module folder (stores resources related to a function module)
| -- Images -- stores images related to this function module
| -- CSS -- stores style sheet files related to this module
| -- JS -- stores JS files related to this module
| -- JSP and HTML pages
| -- WEB-INF
| -- Classes
| -- Lib
| -- TLD File

J2EE project submission specifications
When the project is completed, the project should be delivered as a product delivery user. Good project organization specifications allow users to easily find the resources required by the project and reflect the professionalism of the company. When submitting a project, you must submit the project in the following format.

Project main Folder:
Purpose: store other project resource files.
Naming Convention: Time _ class No. _ Group X.
Example: 070706_gs2t18 _ group 4.
The main project folder contains the following folders and files:
| -- SRC: Save the. Java file.
| -- Database: stores database script files or database backup files.
| -- Source: saves all files in the webroot directory of the eclipse project.
| -- Depend: save other jar files that must be relied on to compile the program.
| -- Javadoc: saves the javadoc API documentation generated by all classes.
| -- War: Save the archive file of the program
| -- Xx. War: The packaged project file can be run directly.
| -- Project: saves the original project code and files of the development project.
| --Product description .doc: displays the product usage in graphic mode.
| -- Build. xml: ant script, used to generate a running war file.
| --Project description .ppt: Project Description ppt (PPT is only used for simulation projects in schools and not for other commercial purposes)
Note: In a complete project, the database must have a certain amount of valid test data to support the running of the program.

Package name
The Java package name is composed of lowercase words. However, due to the characteristics of Java object-oriented programming, every Java programmer can write their own Java packages. To ensure the uniqueness of each Java package name, in the latest Java programming specification, the programmer is required to add a unique prefix before the name of the defined package. Because domain names on the Internet are not repeated, programmers generally use their own domain names on the Internet as the unique prefix of their packages.
Example: net. frontfree. javagroup

Class Name
The class name must start with an upper-case letter and the other letters in the word must be lower-case. If the class name is composed of multiple words, the first letter of each word should be in the upper-case format, such as testpage; if the class name contains the abbreviation, each letter in the written word should be capitalized, for example, xmlexample. Another naming technique is that the class is designed to represent objects, therefore, you should select a noun whenever possible when naming a category.
Example: Circle

Method Name
The first word of the method name should start with a lower-case letter, and the subsequent words should start with an upper-case letter.
Example: sendmessge

Constant name
The constant names should all use uppercase letters and indicate the complete meaning of the constant. If a constant name is composed of multiple words, the words should be separated by underscores.
Example: max_value

Parameter Name
The parameter naming rules are the same as the method naming rules. To avoid confusion during reading the program, make sure that the parameter name is a word as clear as possible.

Javadoc comments
In addition to using our common annotation methods, the Java language specification also defines a special annotation, that is, the javadoc annotation we call, it is used to record the APIs in our code generation. Javadoc annotation is a multiline annotation that ends with a single end. It can contain HTML tags and specialized keywords. The advantage of using javadoc annotations is that the compiled notes can be automatically converted into online documents, saving the trouble of writing program documents separately.
For example:

At the beginning of each program, javadoc is generally used to comment on the overall description of the program and copyright information. Then, javadoc comments can be added to each class, interface, method, and field in the main program, the first part of each comment first summarizes the functions completed by the class, interface, method, and field in one sentence. This sentence should take a separate line to highlight its generalization, after this sentence, you can follow more detailed descriptions. After descriptive paragraphs, you can also follow special paragraphs starting with the javadoc annotation label, such as @ auther and @ version in the preceding example. These paragraphs are displayed in a specific way in the generated documents.

Variable and constant naming
The variable naming method adopts the Hungarian naming method. The basic structure is scope_typevariablename. It uses a three-character prefix to indicate the data type. The three-character prefix must be in lowercase, the prefix is followed by a name consisting of one or more words with strong ideographic meaning. The first letter of each word is uppercase, And the other letters are lowercase, this ensures that the variable name can be correctly broken. For example, define an integer variable to record the number of documents: intdoccount, where int indicates the data type, followed by the ideographic English name, each word is capitalized. In this way, a variable name can reflect the meaning of the variable type and the value stored in the variable, which makes the code statement readable and easier to understand. Byte, Int, Char, long, float, double, Boolean, and short.
The following table lists the relationships between variable types and initials:
Data Type/object type/variable prefix/Remarks
Byte bye
Char Chr
Float FLT
When Boolean bln is used as a Boolean variable, bln is used.
Integer/INT int
String Str
Single Sng
Short sht
Long/long LNG
Double/Double DBL
Currency cur
When variant bln astr OBJ vnt is used as a Boolean variable, bln is used as a string array, astr is used as an object, obj is used, and vnt is used for uncertainty.
For arrays, add another a before the data type prefix. For example, the string array is astr. Add "g _" before global variables used in multiple functions _". For example, a global string variable: g_struserinfo.

Pay attention to the following points when naming variables:
· Select a meaningful name. Note that the first letter of each word must 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 used only as small cycle index variables.

· Do not use flag to name state variables.

· Use is to name logical variables, such as blnfileisfound. By naming Boolean variables in the affirmative form, other developers can be more clear
To understand the meaning of Boolean variables.

· If necessary, append the computing qualifier at the end of the variable, for example, cursalessum.

· The name does not contain cursales and cursalessum.

· The names of static final variables should be capitalized and the complete meaning should be indicated.

· If You Need To abbreviation a variable name, pay attention to the consistency of the abbreviation rules in the entire code. For example, if intcnt is used in some areas of the code and intcount is used in other areas, unnecessary complexity is added to the Code. We recommend that you do not use abbreviations in variable names.

· By placing a quantizer at the end, you can create more unified variables, which are easier to understand and search. For example, use strcustomerfirst and strcustomerlast instead of strfirstcustomer and strlastcustomer. Commonly used quantifiers include: First (the first in a group of variables), last (the last in a group of variables), and next (the next variable in a group of variables), Prev (the previous one in a group of variables), and cur (the current variable in a group of variables ).

· Select the best data type for each variable, which can reduce the memory demand, speed up code execution, and reduce the possibility of errors. The data type used for variable may affect the calculation result of the variable. In this case, the compiler does not generate runtime errors, but only forces the value to comply with the data type requirements. This type of problem is extremely difficult to find.

· Minimize the scope of variables. If the scope of a variable is greater than its expected range, the variable can continue to exist and still occupy resources for a long time after the variable is no longer needed. Their main problem is that any method in any class can modify them, and it is difficult to track where they are modified. Resource occupation is an important issue in the scope. For variables, minimizing the scope will have a huge impact on application reliability.

In Java code, whenever a constant is used, it is recommended to replace numbers and fixed strings with constants. That is to say, the program should not contain any other number word except 0, 1. Constants can be concentrated in some definitions at the beginning of the program or in a wider scope. The names should all use uppercase letters and indicate the complete meaning of the constants. If a constant name is composed of multiple words, use the underscore "_" to separate these words, such as num_days_in_week and max_value.

ArticleSource: Click to open the link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.