Surface content 1. The Code should be written to minimize the time required for others to understand it. A comment can help you better understand the code. Although reducing the number of lines of code is a good goal, it is a better goal to minimize the time required for the code in Bali Street. 2. Select a professional word. For example, if the function name uses getxxx (), this get does not express much information. Is it obtained from the cache? Get from the database? Or from the network? If it is a network, you can use a more professional generic name such as fetchxxx () or downloadxxx () 3.tmpand retval. you can name it as needed, such as tmpfile, to let people know that the variable is a temporary file. (TMP is used only for variables with short-term and temporary factors) 4. For nested loops, I, j, and K are used to this process. However, if the nested index is incorrectly used (K should be used and the result is written as I by hand), it is difficult to query it. You can use the classa_ I, classb_ I, classc_ I format, in this way, the defect is more obvious, because once the index used by the data represents a different type than the data type, you can see it at a glance. 5. A variable name is like a small comment. Although the space is not very large, no matter whether you squeeze in any additional information in the name, every time someone sees this variable name, they will see this information at the same time. For example: String ID; // eg: "af84ef845cd8" if you want to remember the format of this ID, you may wish to name it hexid6. drop useless words, such as converttostring (), tostring (). 7. generally, adding words like is, has, can, and shocould can make the meaning of the Boolean value clearer. method definition. getmean () gives the function caller the feeling that it is a lightweight and quick method for getting the value. computemean () it is a method for calculating the value of the output value with a greater amount of computing. Be careful about users' expectations for specific words. Logic content 1. Make changes to conditions, loops, and other control flows more "natural", so that readers can read the code in a coherent manner without stopping or rerunning it. 2. A better measurement method than the pursuit of minimizing the number of lines of code is to minimize the time needed for people to understand it. For example, we recommend that you use the IF/else operator by default, and only use the three items in the simplest case. 3. Avoid using do... while loop. Many readers read the code twice at last. 4. nested code is hard to understand. Each nested layer adds another condition to the reader's "thinking stack". Therefore, when the reader sees a roar of the right braces, it may be difficult to recall the conditions behind the stack. Reduce nesting by returning early; rewrite the code into a more "linear" code to avoid deep nesting. 5. When you make changes to the code, you can look at it from a new perspective and treat it as a whole. Try to avoid simply finding the easiest place to insert the code that I need to add. The code may be clear after the modification, but it may not be enough for others to read the code as a whole. 6. ultra-long expressions are split into small pieces that are easier to understand. 7. narrow down the scope of all variables, not just for global variables, so that variables can be visible to as few lines of code as possible: this can effectively reduce the number of variables that readers need to consider at the same time. 8. In JavaScript, the keyword VaR is always used to define variables. Re-organize code A. Extract sub-problems unrelated to the main purpose of the program B. re-organize the Code so that it can only do one thing at a time. C. Describe the code in a natural language first, and then use this description to help you find a more clean solution. 1. if you want to: "I want our library to have an XYZ () function", write one (if it does not exist). After a while, you will build a good set of tool code that can be applied to multiple projects. 2. When using an independent small function, you will feel easier to add features, improve readability, and handle boundary conditions. 3. The more independent libraries split from the project, the better, because the rest of the code will be smaller and easier to think about. 4. If the interfaces you use are not neat, you can create your own neat "Wrap" function. The above can be summarized:
Separate General (General) code from proprietary project code. The result is that most of the Code is General (General) code. By creating a large set of libraries and helper functions to solve general problems, the rest is just the core part that differentiates your program. The reason for doing so is that programmers can pay attention to it.
Small but well definedThese problems are already the same as the other parts of the project.
BreakOpen. The result is that solutions to these subproblems tend to be more
Complete and correct. 5. re-organize the code: After dividing some tasks into separate areas in the Code for improvement, these areas are independent of each other. When you read a region, you don't have to remember the content of his region. The difficulty is to accurately describe all these things that your program has done. 6. Understand what the Library provides when writing a part of the refined code. 7. requirements often affect each other in a subtle way. This means that it may only take 1/4 of the effort to solve half of the problem. 8. Make the code library smaller and lighter, the better. Create "tool" code to reduce repeated code, reduce useless code and useless functions, and maintain separate sub-project states for projects.
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.