Good Java style (Part 1)

zhaozj2021-02-16  46

Nice Java style (Part 1) by Thornton Rose

Introduction

As a software developer and consultant who has already worked, I have seen a lot of shape-colored programming language code. Among them, there is also ugly, but unfortunately, most of them are ugly. I hope to convince you, my development partners, we should pay more attention to our code style, especially those that provide user interfaces and other visual parts in the application. In the first part of these two series, I will explain why we should care about our code appearance and then explain some of the general elements of a good Java style. Why code is important, even though Java is used to write a program instead of a prose, it is still used to express ideas and views. Moreover, in the survey, those ideas and views have completed a lot of things in practice. In order to write a good Java style, it seems to be a waste of time, but in fact it is very suitable for our writing programs, because the ideas and views that it want to express are extraordinarily clear. Here is some reasons, why should I use a good Java code specification?

The 80% life period of a software product is maintenance. Almost no software is maintained by its authors in its entire life. Use a good style to increase the maintenanceability of the software. If the source code and software product is bundled with customers, as the rest of the product, it should have a good sub-package, enough enough and professional code. Writing code with good style has the following benefits:

Improve the readability, sustainability, harmony of the code. These can make the code easier to understand and maintain. Make the code more easily tracked and debug because it is clear and continuous. Make you or another programmer is more likely to continue when writing, especially after a long time. Increase the benefits of Walkthrough, because you can make more investers more focus on what is doing in the code. Summary The policy is not difficult to use the Java style, but it does need to pay attention to details. Here are some common profile guidelines:

Make the code clearer and easier to read make the code more sustained useful identifier logic to organize your files and classes for each file (here, including some internal classes) using 80-90 characters maximum lines. Wise use spaces to And / OR other separator rows, use spaces (space) instead of Tab Tabs vs. Spaces When writing code, "Tabs vs. Space" is a rigorous point of view. Here I am not implying that there is only one correct way. I support the use of spaces, because it guarantees that my code is the same in your editor and I have seen in my editor. If you feel that the space is used instead of Tab "Not the right", then use Tab. Parentheses and indentation When writing code, indent the style (CF., Raymond, "Indent style), or place parentheses (" {"and"} ") and some associated indentation code is another rigorous View. Like Java, there are many C style languages. I don't imply which one is more preferred here. In most examples of this article, I use K & R style, if you don't like K & R style, then use other styles. Note You can use two comments in the Java code: Javadoc comments (also known as document comments) and permits. Javadoc comments can be extracted by Javadoc tools to create an API document. Executive comments are those who explain the code destination and methods. Use the following policy when comment your Java code:

Use Javadoc comments as much as possible (on the classes and methods, make it minimize). Use the block annotation, less use // Note, unless some special cases, such as variable declaration should be remembered: Good comments are helpful Bad comments are troublesome. Example 1. Bad Comment Style // applyRotAscii () - Apply ASCII ROT private void applyRotAscii () {try {int rotLength = Integer.parseInt (rotationLengthField.getText () trim ().); // get rot len ​​RotAscii cipher = new RotAscii (rotLength); // new cipher textArea.setText (cipher.transform (textArea.getText ())); // transform} catch (Exception ex) {/ * Show exception * / ExceptionDialog.show (this, "Invalid rotation length: ", ex);}} Example 2. Good Comment Style / ** * Apply the ASCII rotation cipher to the user's text The length is retrieved * from the rotation length field, and the user's text is retrieved from the *. . text area * * @author Thornton Rose * / private void applyRotAscii () {int rotLength = 0; // rotation length RotAscii cipher = null; // ASCII rotation cipher try {// Get rotation length field and convert to integer rotLength. = INTEGER.PARSEINT (RotationLeng . ThField.getText () trim ()); // Create ASCII rotation cipher and transform the user's text with it cipher = new RotAscii. (RotLength); textArea.setText (cipher.transform (textArea.getText ()));} Catch (Exception EX) {// Report The Exception To The User. ExceptionDialog.show (this, "Invalid Rotation Length:",}} Block and statement use the following policy to write blocks and statements:

One line only writes a statement control statement to use parentheses such as {} (EG, 'IF'). Consider using a note (EG,} // end if), especially long or nesting blocks at the end of the block The statement of the beginning of the block, the statement statement, the statement, the initialization variable, if you are a perfection, the left aligned variable name is retracted in the Switch block, if you have a blank in IF, for, or the While statement, in parentheses "(" ("Pre-leaves have blank uses blank and insert words to enhance the variables in the Forford in the expression. The cyclic variable may be defined in the statement initialization section. EG, FOR

转载请注明原文地址:https://www.9cbs.com/read-24933.html

New Post(0)