Java Specification


Naming

For variables, arrays, methods / functions, classes, and objects, it is important to standardize the naming, especially when the scale of the program is expanded and the number of variables is increasing.

The more famous naming convention deduces the Hungarian nomenclature, which is proposed by Microsoft programmers Charles Simonyi, which is commonly used in C language. The name of the identifier is preceded by one or more lowercase letters; the prefix is ​​followed by a word or multiple word combination in the first letter, which specifies the purpose of the variable. For example: lpszStr, which indicates a long pointer (lp) variable that points to a string (sz) ending with '\ 0'.

"Camel" nomenclature, or "hump" nomenclature in recent years more and more popular, in many new libraries and Java such a platform to use more appropriate. It is characterized by the first word all lowercase, after the first letter of the word capital, it looks like a camel's back, there are ups and downs.

Pascal (Pascal) nomenclature and camel nomenclature similar. But the camel nomenclature is the first word of the first letter of the lowercase, and the Pascal naming rule is the first word capitalization.

Another popular naming convention is called underline nomenclature, that is, each word is underlined. Underlined method is popular with the emergence of C language, in UNIX / LIUNX such environment, as well as the use of GNU code is very common.

For many reasons, Java design at the beginning of the use of hump naming, and the vast majority of Java programmers in their code to follow this specification.

Camel case naming has the following specifications:

  • Use meaningful English words or abbreviations, do not recommend using meaningless words, single letters, numbers
  • A variable consists of multiple English words, with no underscores, dashes, dotted numbers, or other symbols
  • The first word is all lowercase, starting with the second word, the first letter is capitalized to disconnect each word
  • The variable word should be able to express the meaning and effect of this variable clearly and accurately

For example, now we need to use a variable that Japan's population, you can name: populationOfJapan. Sometimes it can be a word: ifTheInternetIsAccessible to represent a network-dependent state of the boolean variable.


Code Writing

Good code writing habits can enhance code readability. In the team development process, the consistency of the code writing specification is the premise of effective communication and cooperation.


Indent and line break

Indentation and line breaks can enhance the readability of the code. The principle of indentation :

  • If the previous line is a left brace ("curly brackets" {), the general line is to indent a unit relative to the parentheses.
  • If the previous line is if, for, while, else, do, and the next line is not a brace, the next line must be indented.
  • Indent use the Tab key (on the left side of Q on the keyboard) instead of the space. This is not only convenient, but also to achieve the unity of the distance, the number of spaces is difficult to control, and can not be identified as indented by the editor.

The principle of the line :

  • Wrap the line after the semicolon
  • Wraps after the brackets representing the structure block: {line breaks} wraps
  • If, for, while, else, do after the line

Spacing

The use of spaces can make a statement look less crowded, and each variable can be clearly distinguished.

The use of spaces are different, the following methods are for reference only:

In =, +, -, *, /, before and after a space, before and after the number should have a space, such as:

int a = 10 + b;

Use a space inside the brackets, such as:

while( i = 1 )

Line Break

The empty line plays the role of dividing the area, which makes it easier to read the code, and can see at a glance which parts of the code.


Brackets

Parentheses are critical to the correctness of code writing. To ensure that the brackets are not missing, you can use the following method:

  • Method 1: each time writing parentheses at the same time write a pair of brackets, so there is no forgotten to write the end of the brackets.
  • Method 2: the use of IDE brackets matching and auto-complete function, in Eclipse, as long as the write a brackets, the program will automatically fill the other half.

Comments

Comments can help the program be better understood, so clear, concise, clear comments are a good code style of expression.

The comments may explains :

  • Start by introducing the author, date, and version of the code
  • Introduce the respective roles of each part
  • Introduce the process of solving the problem

There are two ways to write comments in Java, all inherited from C++.

The first is the line comment, with two slashes to indicate the contents of the back to the end of the line are the contents of the note.

// Comments can be placed at the beginning of the document
// Each line has a double slash at the beginning

public class Hello {
  // can also be placed in the middle of the code
  public static void main(String args[]) {
    System.out.println("Hello world!"); // place at end of line
  }
}

The second is the regional comment, /* ... */ the middle of the content will be used as a comment.

/* 
  beginning of the document
*/
public class Hello {
  /* can also be placed in the middle of the code */
  public static void main(String args[]) {
    System.out.println("Hello world!"); /* place at end of line */
  }
}



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook