Java know how much (17) emphasize the programming style

Source: Internet
Author: User

Having finished the basic syntax of Java, you can write simple program code, it is necessary to emphasize the programming style.

Although the code style does not affect the program's operation, it is very important to the readability of the program. The program you write to let others understand, first of all in the layout to be very careful.

In fact, everyone's programming style, each software development company's programming style is different. A person to write the program code, should be able to let others understand, even after a long time, oneself also want to see understand, otherwise this program becomes a dead program.

Programming style refers to the format of programming, so that the program looks very layered. Here are some examples of the importance of programming styles:

1  Public classmath{2      Public Static voidMain (string[] args) {3         intX=12;4         DoubleY=12.3d;5         voidprint () {6             CharA= ' a ';7 System.out.println (a);8         }9System.out.println (x+y);Ten     } One}

Does the entire layout of the above program section look comfortable and highly layered? Does it look like you know the entire program architecture at a glance? The key here is indentation, and indentation can also be called a jump.

The above code uses indentation: "public class math" is shelf, and then the Mian () method indents 4 spaces, the code inside the Mian () method is indented 8 spaces, and the print () method's body code is indented 4 more spaces. This way, the entire program's affiliation is obvious. The Mian () method belongs to the math class, the rest belongs to the main () method, and the code snippet within the print () method belongs to this method. The law is that the space is much more code from the space is small.

I recommend that you use the TAB key, and most editors, such as Eclipse, support a custom tab space, typically 4 spaces.

In addition to indentation, empty lines are also necessary, first look at the following program code:

1  Public classmath{2      Public Static voidMain (string[] args) {3         intX=12;4         intY=23;5         voidprint () {6             // .................7         }8        9          voidview () {Ten             // .................... One         } A     } -}

The above program segment has a blank line between the print () method and the view () method to distinguish between different modules. The print () method is not the same as the function done by the view () method, so they are separated by using empty lines, which increases the readability of the program.

In addition, it is important to note the naming of the method or property. These names should have meaning, preferably regular, and do not use only "a", "B" the general variable, appropriate can be based on the function of the variable or function to name it. "Print" above, other programmers know this method at a glance, is about the printing or output function. Another example: variable name "Name", a look to know is about the name of the variable. So, be sure to name the meaningful, otherwise the program readability is not strong.

Another point is about annotations. Next to the method name of each method, some comments should be added, and after a program is completed, a simple description of the program's function and how to operate it.

As long as you do the above, the program is easy for others to read. Even after a long time, you can read the program at a glance.

Series Articles:

Java know how much (1) Language overview

Java know how much (2) virtual machine (JVM) and cross-platform principle

Java know how much (3) employment direction

Java know how much (4) the difference between J2SE, Java EE, J2ME

Java know how much (5) Build Java development environment

Java know how much (6) The first example of a program

Java knows how many (7) classes and objects

Java know how much (8) class library and its organizational structure

Java know how much (9) Import and Java class search path

Java know how much (10) data types and variables

Java know how much (11) data type conversions

Java know how many (12) operators

Java know how much (13) Process Control

Java know how many (14) arrays

Java know how much (15) string

Java know how much (StringBuffer) and Stringbuider

Java know how much (17) emphasize the programming style

Java know how much (17) emphasize the programming style

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.