Java Naming rules for java programming specifications

Source: Internet
Author: User

To become a good programmer, we must first cultivate good programming habits. To improve code readability, we must have good naming rules.
This article is compiled by a small editor based on a lot of information on the Internet. If there are any improper or incorrect content, please correct me.

Before starting the article, let's briefly introduce some knowledge to facilitate reading.
Camper name MethodThe camper naming method is used to identify a variable or function name that is composed of one or more words. The first word starts with a lowercase letter, the first letter of the second word is capitalized or the first letter of each word is capitalized, for example, myFirstProject.

Java Naming rules:

Project name:
1: Some people use short words, and all are capitalized;
2: Some people use upper-case letters + camper name

Package name: [All in lower case, starting with the domain name]
1: All package names are in lower case;
2: The first part of a Package is usually one of top-level domain names, such as com, edu, gov, mil, net, and org. The subsequent part is determined by the internal naming rules of each organization, the internal naming rule specifies the directory name and department name of each component.

Project name. Example: com. frontfree. javagroup;
3: For the naming conventions in j2ee, I have seen a blog on the Internet. I have summarized the naming rules of each recipe in j2 in detail. I will give you a URL:

Http://blog.csdn.net/yilvqingtai/article/details/7989618

Class Name: [upper-case letters + hump naming method]
According to the Conventions, Java class names often start with uppercase letters. If the class name is composed of multiple words, the first letter of each word should be a large one, such as TestDemo. If the class name contains the abbreviation, then the word written

Every letter in the format of, for example, DOMDemo.

Interface: [upper-case letters + hump naming method]
1: The Interface Name naming rules are consistent with the class name;
2: Use the complete English description naming interface.

Method: [hump name + name implies]
1: lowercase letters, and other words, for example, getName ();
2: Use the complete description to accurately describe the method name. If you see the method name, you should know the function of this method. For example, you can get the name from the database by using the id, although it is a little long, it provides great convenience for later understanding and maintenance. Of course, to prevent code from being too long, we should try to shorten the name length. You can try to use the abbreviation where it is short;
3: The method name should be a verb + noun, such as getName ();
4: prefix
I: The method name for obtaining a class (a method with a returned value) is usually prefixed with get, for example, getRootNode ();
II: Boolean judgment methods of classes generally use the is prefix, such as isEmopty ();
III: add the prefix set to the class setting method, for example, setName ();
IV: general method: describes the method using a complete English description, starting with a word, for example, openFile ();

Method parameters:
1: Define method parameters with specific words. If possible, use the same name as the assigned variable;
2. lowercase letters, and uppercase letters

Common variable: [hump name + name implies + prefix]
1: Use the camper name;
2: The variable name should be as short as possible. As the name suggests, do not use a, B, c, etc;
3: Avoid similarity or case-insensitive differences, such as: flag and Flag, and avoid simultaneous use;
4: Use the I, j, k
5: prefix problem:
I: You can add 2 to 3 letters in front of the variable name to indicate the Data Type of the variable, such as strClassName (String type) and intStudentAge (int type;
II: Global variables and local variables can be distinguished by prefix. g _ is used to represent global variables, such as g_strClassName. We recommend that you use as few global variables as possible because global variables are difficult to maintain.
6: suffix problem:
I: to create a unified variable, a keyword suffix is usually added after the variable name, such as strStudentFirst and strStudentLast, instead of strFirstStudent.
Common quantifiers are suffixed with First, Next, Prev, Last, and Cur (the current variable in a group of variables)

Static variables: [all uppercase, often underlined]
1: all uppercase words are connected with "_", for example, static int THREAD_FLAG.
2: for other rules, refer to rules 2, 3, 5, and 6 of common variables.

Constant: [all uppercase, often underlined]
1: all uppercase words are connected;
2: constants must be defined with meaningful words;

Xml:
1: All xml file names are in lowercase;

Jsp/js and other common files:
1: Consistent with the class naming rules;


Related Article

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.