Java good programming habits __ technology

Source: Internet
Author: User
Tags readable

Java Good programming habits

1. Comments should be added at the beginning of each procedure to explain the purpose of the program, the author and the date and time when the program was last modified.

2. Use blank lines and spaces to enhance the readability of the program.

3. By convention, the first letter in the class name identifier is capitalized, and the first letter of each word is then capitalized. Java programmers think such identifiers usually represent classes in Java, so follow this convention to make them more readable.

4. Curly braces for defining a class body in a procedure or a method's curly braces note type {} and then indent one level to increase readability while preventing missing curly braces on the other.

5. Choose the indent you like, and use it in a fixed way, you can use the TAB key to create indents, and the spacing between the tabs of the different editors is different. It is recommended that you use 3 spaces for each level of indentation.

6. Add a space after each comma in the argument list to improve the readability of the program.

7. Each variable is declared with a separate line, making it easier to add descriptive comments after each declaration

8. Select a meaningful variable name to make the program self explanatory. That is, as long as you read the program, you can understand the function of the program without having to read the manual or a large number of other annotations.

9. By convention, variable name identifiers begin with lowercase letters, and each word after the first word is preceded by a capital letter. For example, the variable name identifier firstnumber The first letter of the second word number in uppercase.

10. Adding spaces on both sides of the two-element operator makes it more eye-catching and improves the readability of the program

11. The addition of parentheses () to complex arithmetic expressions can make them more readable even if they are not necessary.

12. If the statement is longer, you can write it as multiple lines. If you must write a statement across rows, you should select a meaningful breakpoint, such as after a comma separates a comma from the list, or after an operator of a long expression. If the statement spans two or more lines, you should indent all line breaks.

13. When you write an expression that contains more than one operator, you should refer to the operator precedence table. Make sure that the expression is calculated in the order you expect it to be. If you are unsure of the order of calculations in a complex expression, use parentheses to enforce the order, as you would in an algebraic expression. Note that some operators, such as assignments, should add an access modifier before each field and method declaration. As a conventional rule, instance variables should be declared private, and methods should be declared public (some methods are declared private if they are only accessed by other methods of the class)

15. Each field in the class should be listed first so that it is easy to read

16. Adding a blank line before the method declaration allows each method to be delimited and enhances the readability of the program.

The two bodies of the 17.if...else statement should be indented.

18. If there are a few levels of indentation, each set should indent the same number of spaces.

19. Conditional expressions are less readable than if...else, and should be used only to replace simple if...else statements that choose between two values.

20. To improve readability, leave a blank line between the declaration statement and the other statements in the method.

21. Unlike the two-dollar operator, a unary operator (such as ++or--) should be placed next to the operand, with no spaces in the middle.

22. Add a blank line to the upper and lower sides of the loop and select control statements, and indent the statement body to enhance readability.

23. Using the final value in the condition of a while or for statement, and with the <= relational operator, helps to avoid a 1 error. For a loop that indicates a value 0~10, the loop condition should be counter<=10 rather than counter<=10 (the latter would result in a difference of 1 errors). or use counter<11. Many programmers prefer a zero based calculation, and if you count it 10 times, the counter is initialized to 0, and the loop continues to test the condition as counter<10.

24. In most cases, the prefix increment and suffix increments are available for variable plus 1 in the statement. In both cases the effect is exactly the same, but the prefix increment method has a slightly better performance advantage. In general, we can optimize the code to achieve the best performance, so long should choose their own feeling the most accustomed to use the way.

25. Limit the size of the header of the control statement to a single line whenever possible.

26. In the initiallization and increment portions of the For statement, only those expressions related to the control variable are placed. Operations on other variables should appear before the loop (if the variables are executed only once, similar to the initialization statement), or in the loop body (if the program performs these operations every loop traversal, similar to increment or decrement).

27. Do not use double or float-type variables to perform accurate currency calculations. The inaccuracy of floating-point numbers can result in incorrect currency values resulting from errors.

28. Even if it is not necessary, the curly braces should always be included in the Do...while statement. This helps to eliminate the ambiguity between the while statement and the Do...while statement that contains only one statement

29. In a switch statement, although both case and default conditions can occur in any order, it should be the case that the default is last. This does not require a break statement when default is placed at the end. Some programmers will still add this break to make the code clearer and to be symmetrical with other situations.


30. Be sure to familiarize yourself with the rich classes and methods provided by JAVAAPI.

31. Use only uppercase letters in the constant name. This makes the constant name appear prominent in the program, and it reminds the programmer that these things enumerate constants rather than variables.

32. Using enumerated constants makes the program easier to read and maintain.

33. In order to improve readability, only one variable is declared in each declaration statement.

34. The constant quantity is also called a named variable or a read-only variable. They are more readable than the direct use of a value representation.

35. According to the Nineth chapter of the Java Language Specification (Javalanguage specification), declaring an interface method without the keyword public and abstract is the correct style because they are superfluous. Similarly, declaring constants does not require a keyword public and static as well as final, and they are also superfluous.

Java Free learning materials Live Public course Group: 175161984

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.