DIV+CSS: How to write code to be more efficient

Source: Internet
Author: User
Tags switches

How can I write CSS code to be more efficient? This is a matter of concern to many web creators and developers alike. There is probably no magic, you can guarantee to reduce your style sheet to a percentage of how much, but reasonable CSS coding and organizational skills, it does help you to more efficiently write more efficient code, naturally, the size of the style sheet reduces the time to download.

First, typesetting:

1. Add the appropriate spaces between the keywords and the operators.

2. Blank line between the relative independent program block and the Block

3, longer statements, expressions, etc. to be divided into multiple lines of writing.

4, the new line to adapt to the indentation, so that the layout is neat, the statement can be read.

5. Long expressions to divide a new line at a low-priority operator, the operator is placed at the top of the new line.

6. If there is a long expression or statement in the sentence of the circle, judgment, etc., it is necessary to divide the adaptation.

7. If the parameters in the function or process are longer, the appropriate division should be made.

8. It is not allowed to write multiple phrases in one line, that is, a single sentence is written on a line.

9, the beginning of the function or process, the definition of the structure and the loop, judgment and other statements in the code to adopt the indentation style.

10. C + + language is a block of blocks defined with curly braces ' {' and '} ', when the program block is written ' {' and '} ' should be exclusive and in the same column, and aligned to the left of the statement referencing them. The beginning of the function body, the definition of the class, the definition of the structure, the definition of the enumeration, and the procedures in the If, for, do, while, switch, case statements are indented as above.

Second, the note

1, the note should be simple and clear.

2, side write code side comments, modify the code at the same time modify the corresponding comments to ensure that the comments and code consistency.

3, in the necessary local notes, the amount of comments should be moderate. The contents of the note should be clear, concise and accurate, and prevent the ambiguity of the annotation. Keep the comment adjacent to the code it describes, which is the nearest principle of the annotation.

4, the comments on the code should be placed above the adjacent position, not to be placed below.

5, the data structure of the comments should be placed above the adjacent position, not to be placed below; comments on each field in the structure should be placed to the right of the field, and comments from different fields in the same structure should be aligned.

6, variables, constants of the comments should be placed above the adjacent position or to the right.

7, the global variable to have a more detailed comment, including its function, the scope of the value, which functions or procedures to access it and access to the notes, and so on.

8, the head of each source file must have the necessary annotation information, including: file name, version number, author; Generate date, module function description (such as function, main algorithm, the relationship between the internal parts, the file and other file relations, etc.), the main function or process list and the history of the document modification records.

9, in front of each function or process to have the necessary annotation information, including: function or process name, function description, input, output and return value description, call relationship and call relationship description, etc.

Third, the name

1. Shorter words can be shortened by removing "vowels";

2. A longer word takes precedence over the first few characters of a word, and uses parentheses to clarify the order of operations of the expression, avoiding the use of the default precedence.

3. Use of Hungarian notation

Four, readability

1, avoid the use of difficult to understand the number, with meaningful logo to replace.

2, do not use difficult to understand the high-skilled statement.

3, the source program in the more closely related code should be as close as possible.

V. variables

1, remove unnecessary public variables.

2. Constructs only one module or function can modify, create, and the rest of the module or function access only public variables, to prevent many different modules or functions can be modified, create the same public variable phenomenon.

3, carefully define and clarify the meaning of the public variables, the role of the value range and the relationship between the public variables.

4. Identify the relationship between public variables and functions or processes that manipulate this public variable, such as access, modification, and creation.

5, when passing data to the public variables, be very careful to prevent the assignment and unreasonable values or cross-over and other phenomena occur.

6. Prevent local variables from having the same name as public variables.

7, carefully design the structure of the layout and order of elements, so that the structure is easy to understand, save space, and reduce the phenomenon caused by misuse.

8, the design of the structure to try to consider forward-compatible and later version upgrade, and for some future possible applications to reserve (such as reserving some space, etc.).

9, pay attention to the specific language and compiler processing different data types of principles and related details.

10, the use of uninitialized variables is strictly forbidden. Initializes variables while declaring them.

11, when programming, pay attention to the data type of casting.

Vi. functions, processes

1, the function of the scale as far as possible to limit within 200 lines.

2, a function is best to complete only one function.

3. Write functions for simple functions.

4 function should be predictable, that is, the same output should be produced as long as the input data is the same.

5. Try not to write functions that depend on the internal implementation of other functions.

6, avoid the design of multi-parameter functions, not the use of parameters removed from the interface.

7. The function of each parameter, the range of values and the relationship between parameters are explained in detail with the annotations.

8, check the validity of all parameters input function.

9, check the validity of all non-parametric input functions, such as data files, public variables and so on.

10, the function name should accurately describe function functions.

11. Avoid using meaningless or ambiguous verbs to name the function

12, the return value of the function should be clear and clear, so that users can not easily ignore the error situation.

13, clear function function, accurate (rather than approximate) to achieve the function design.

14, reduce the function itself or the recursive call between functions.

15, when writing a reentrant function, if the use of global variables, it should be closed by the interrupt, the semaphore (that is, p, v operation) and other means to protect it.

Seven, the testability

1, before writing code, should be pre-design procedures and testing methods and means, and design a variety of debugging switches and corresponding test code such as printing functions.

2, before the integration test/system, to build a good test environment, test projects and test cases, while carefully analyzing and optimizing test cases to improve testing efficiency.

Viii. procedure Efficiency

1. Always pay attention to the efficiency of the code when programming.

2. Improve the efficiency of code under the premise of guaranteeing the correctness, stability, readability and testability of the software system.

3, can not blindly pursue the code efficiency, and the correctness of the software, stability, readability and measurable impact.

4, when programming, you should always pay attention to the efficiency of the code, optimize the code, to consider the comprehensive.

5, to carefully construct or directly with the assembly to write calls frequent or high performance requirements of the function.

6, through the System data Structure Division and the organization improvement, as well as the program algorithm optimization to improve the space efficiency.

7, in the multi-cycle, the busiest cycle should be placed in the most inner layer.

8, minimize the loop nesting level.

9, to avoid the circulation of the body containing the judgment statement, the loop should be placed in the judgment statement of the code block.

10, try to use multiplication or other methods instead of division, especially in floating-point operation division.

Nine, quality assurance

1, in the software design process to build software quality. Code Quality Assurance First Principles

(1) Correctness, refers to the program to achieve the design requirements of the function.

(2) Stability, security, refers to the procedure is stable, reliable, safe.

(3) testability, refers to the procedure to have a good testability.

(4) specification/readability, refers to the procedural writing style, naming rules, etc. to conform to the norms.

(5) Global efficiency refers to the overall efficiency of a software system.

(6) Local efficiency refers to the efficiency of a module/sub-module/function itself.

(7) Personal expression/personal convenience, refers to personal programming habits.

2, only refer to their own storage space.

3. Prevent references to memory space that has been freed.

4. The memory allocated in the procedure/function is released before the procedure/function exits.

5. The file handle (used for opening the file) requested in the procedure/function to close before the procedure/function exits.

6, prevent the memory operation out of bounds.

7, always pay attention to whether the expression will overflow, underflow.

8, serious processing procedures can encounter a variety of error situations.

9, at the beginning of the system operation, to initialize the relevant variables and operating environment, to prevent uninitialized variables are referenced.

10. At the beginning of the system operation, we should check the consistency of the data loaded into the system.

11. It is strictly forbidden to change the settings and configuration of other modules or systems arbitrarily.

12, can not arbitrarily change the interface with other modules.

13, fully understand the interface of the system, and then use the functions provided by the system.

14. Always be aware of the easy-to-mix operator. When the program is finished, you should check the operators from beginning to end.

15. Do not use statements that have a large relationship with the hardware or the operating system, and use the recommended standard statements.

16. Recommendation: When using a software development kit or control provided by a third party, the following points should be noted:

(1) Fully understand the application interface, the use of the environment and the use of precautions.

(2) It is not too much to believe its correctness.

(3) Do not use unfamiliar third-party toolkits and controls unless necessary.

Ten, Code compilation

1, write code should be careful to save at any time, and regularly back up, to prevent loss due to power outages, hard disk damage and other causes of code lost.

2. Within the same project group, it is best to use the same editor and use the same setup options.

3, the rational design of the software system catalog, easy to use by developers.

4. Turn on all the alarm switches of the compiler to compile the program.

5. In the same project group or product group, you want to compile the switch options uniformly.

6. Use the tool software (such as visual SourceSafe) to maintain the code version.

Xi. Code testing, maintenance

1. Unit testing requires at least a statement overlay.

2, unit test start to track each statement, and observe the data flow and variable changes.

3. The code after cleaning, sorting or optimization should be reviewed and tested.

4, the code version upgrade to undergo rigorous testing.

DIV+CSS: How to write code to be more efficient

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.