Code Complete PART2

Source: Internet
Author: User

Basic data type:

1. The only numbers that can appear in the program body are 0 and 1, except that all numbers are defined by a macro or a const type, with a clear variable name describing the purpose

2. Prevent the addition of 0 errors, assert (denominator!=0) or other

3. Explicitly type conversions

4. Avoid comparisons between different types

5. Note Compiler warnings

For integers , note: integer division (de-tailed), check for integer overflow, check for intermediate result overflow

For floating-point numbers , it is important to note that you need to add a threshold value to avoid = =, for example

for (i=0;i<10;i++)

A + = 0.1;

if (a==1.0)

Because the final result must not exactly equal 1, 0.1 in memory binary representation is 0.000110011 ...

For strings , avoid off-by-one errors, as well as Unicode, and string and character arrays, malloc and calloc (including allocations and initializations), with strcpy_s and strncpy, without strcpy

For bool, use Boolean variables to simplify if judgment, general write if (a&&b| | C) can write directly bool Bskip = a&&b| | C, if (bskip) ....

For enumeration Types , the first is left to the illegal value, because most of the time when there is no assignment, is 0, so that the problem can be detected early

For arrays , if the operation is more complex, do not use i,j to denote subscript, because it is easier to confuse ...

Create your own type, typedef float coordinate, which can serve as a wrapper effect and know that this variable is used to store coordinates at a glance; remember not to redefine the type of a variable

For pointers , you can use dog tag to monitor that there are no bad writes in this section. Allocate more when allocating some, in the memory space of the previous paragraph as the dog tag

Organize straight-line code

If some functions are to be called before others, make it clear in the function name that init .... or something.

Using conditional statements

If...else the normal path first, and then in the else deal with the less common code

If you write an if, then also write else, consider what else to do if it appears

Simplifying judgment with BOOL variables

For case statements, put the most commonly executed on top, use default to check for errors, print error messages

Using loops

Add {} to the loop body at any time to facilitate code churn

Some + + operations of the loop, at the front or last side of the loop body

The loop counter should be plastic.

Loop body no more than 50 lines

Nesting less than 3 layers

The long loop moves to the child function

Code Complete PART2

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.