Code sophisticated road - how to write the code

How can I write code? We can see from the code specifications, from the good name, written comments, and a reasonable division of code blocks and other negative optimize their code.

Code Specification

Coding specification refers to a set of agreed rules for a particular programming language, often including file organization, indentation, comments, statements, statement, spaces, naming conventions, programming practices, programming principles and best practices.

Standardized code, the code can reduce the chance of error. Because there is no proper indentation and brackets due to bug such as previously mentioned. If you are a lover of compliance with the code or standard programmer it is difficult to make such a mistake.

Code complexity is the enemy of quality. The more complex the code, the more prone to problems, and because of the complexity, it is difficult to find these hidden problems. In the coding, we should try to make the code style intuitive and simple logic, a direct expression.

Standardized code, reduce software maintenance costs. We write the code follows the specification, code review personnel only comfortable, post-maintenance personnel can understand. Even their own to protect their own code, I believe in looking at the code to write their own after three months will certainly praise for the then himself points clear of variable and accurate indentation, simple logic, the right comments ...

Thing, no matter how strong its usefulness, after all, not as people feel good is more practical.

Good code is not only for yourself, but also for others to see, and above all for others to see.

From the good name

Why do we need a good name? First we look at a Liezi:

public byte[] isEmpty();

This name is not very boring? Simply do not understand this name is used to doing, so use a good naming convention is very important.

Some good code should be self-explanatory, through simple variables, methods, classes named allow code becomes easy to understand and improve the clarity, readability and aesthetic appearance of the code.

Written comments

If a piece of code there is no way to do self-explanatory how to do? This time in respect of the Notes. Notes is a kind of inevitable compromises.

First, because the comments do not need to run, so there is no conventional way to test it. Notes, right? As there is no code changes? These questions are written should be noted that where noted. Notes difficult to maintain, which is using annotations bring the greatest trouble. However, for most programmers, the problem is too few comments, but not too much.

We want to provide key codes and methods explicit comments, which must be accurate, necessary and clear.

  1. Accurate and error comment is worse than no comment.
  2. Necessary, extra time to waste the reader's comment.
  3. Clear, the code cluttered comments will do more chaotic.

code segment

Is there a way to read over 2000 lines? Is not see this code was very headache? Or write a class mess? The method of construction in the middle, get / set method or the like above the member variable.

First, the properties and methods of a class, generally is a major component of a code file. Structure type, recommended encoding order:

  1. Attribute class;
  2. Construction method;
  3. Factory methods;
  4. Other methods.

According to this specification, a class method at a glance, this code does not read like a headless flies guess this is what is, what is that method.

Secondly, in order to use qualifiers declaration of a class, method or methods properties, in order to more accurately describe the application scenarios and statement of understanding, we usually use the modifying keywords.
private final static long serialVersionUIDNo private static final long serialVersionUIDto the specification, staticthe keyword should be in finalbefore the keyword.

Should code block by a space, indentation, blank lines divided. Use blank lines to separate the different code blocks in the same level. This makes the code more clear and apparent.

Guess you like

Origin www.cnblogs.com/manastudent/p/12264460.html