Code Book 2 Reading Notes 07-Chapter 12 fundamental data types

Source: Internet
Author: User
Tags integer division

Chapter 12 fundamental data types
Basic Data Type

The basic data type is to build blocks of all other data types ).
12.1 Number in general values
1. Avoid using "magic number)
The mysterious value is inProgramThe number of uninterpreted numeric characters (litertal numbers), such as 523, 47.
2. If needed, you can use hard-coded 0 and 1
Values 0 and 1 are used for increment, decrement, and loop from the first element of the array.
3. Prevent devide-by-zero errors
4. Make the type conversion obvious
5. Avoid mixed type comparison
6. Pay attention to the compiler warning.
Outstanding programmers will modify theirCodeTo eliminate all compiler warnings. It is much easier to identify problems through compiler warnings than you can find yourself.

12.2 integer
1. Check Integer Division
2. Check Integer Overflow
The simplest way to avoid integer overflow is to consider every item in an arithmetic expression and imagine the maximum value that each item may reach.
3. Check for intermediate result Overflow

12.3 floating-point numbers floating point number
Many decimal places cannot be exactly expressed by 1 and 0 in a numeric computer. Infinite circular decimal places like 1/3 or 1/7 usually only use 7 or 15 digits.

The bit precision is expressed by a valid number. For example, in ms vb, the 32-bit floating point number of 1/3 is 0.33 333 330. The precision is 7 digits after the decimal point.
The following guidelines should be followed when using floating point numbers:
1. Avoid addition and subtraction between numbers with huge order of magnitude differences.
32-bit floating point variables, 1000000.00 + 0.1 may get 1000000.00, because 32-bit cannot give you enough valid digits to include 1000000 and 0.1

Value range. Similarly, 5000000.02-5000000.01 is likely to get 0.0.
Solution: if you have to add a series of numbers with such great differences, sort these numbers first and add them up from the minimum value.

-- In essence, a reverse summation operation is required.
2. Avoid equal amount of judgment
Many floating point values that should be equal are not necessarily equal. Two different methods are used to calculate the same value. The result may not always get the same value.
For comparison, we should find a method that replaces the equivalent judgment on floating point numbers. An Effective Method: first determine the acceptable precision range, and then use Boolean

Number to determine whether the value is close enough.
3. Handling Rounding Errors
4. Check the support of the language and function library for specific data types.

12.4 characters and strings
1. Avoid using mysterious characters and strings (magic character and magic string)
IfProgramming LanguageIs to support the named variable, the appliance name variable is replaced. Otherwise, global variables are used.
2. Avoid off-by-one errors
Because the substring index method is almost the same as the array index method, you must avoid the off-by-one (Deviation

1) error.
3. Learn how your language and development environment support Unicode.
All strings in Java are Unicode. In other languages such as C and C ++, Unicode processing requires a set of functions related to it. Therefore

Determines whether to use the Unicode Character Set. If you decide to use a unicode string, you need to decide where and when to use it.
4. Determine internationalization/localization strategies as early as possible in the life cycle of the program.
5. If you know that only one language is supported, consider using the iso8859 character set.
You can use

Iso8859 extends the ASCII type standard to replace Unicode.
6. To support multiple languages, use Unicode.
7. Adopt a consistent string type conversion policy.

· String strings in C
1. Note the differences between string pointers and character arrays.
Be alert to any expression containing the character string
In C, stringptr = "some text"; in this case, "some text" is a pointer to a literal string, so the value assignment result

Just let the stringptr pointer point to the literal string. This assignment does not copy the string content to stringptr.
Use naming rules to identify whether a variable is a character array or a string pointer.
Declare the length of the C-style string as constant + 1
Use null to initialize strings to avoid strings without terminals.
Replace pointer in C with character array
Replace strcpy () with strncpy () to avoid a string with no final end

12.5 Boolean variables
1. Use a Boolean variable to document the program.
Unlike judging a Boolean expression, you can assign the result of this expression to a variable to make the meaning of this judgment obvious. For example

:
If (elementindex <0) | (max_elements <elementindex) | (elementindex = lastelementindex ))
{....}
In the following code, the use of Boolean variables makes the if judgment object much clearer.
Finished = (elementindex <0) | (max_elements <elementindex ));
Repeatedentry = (elementindex = lastelementindex );
If (finished | repeatedentry)
{...}

2. boolean variable to simplify complex judgment
Example.
3. If necessary, create your own boolean type.

12.6 Enumeration type enumerated types
1. use enumeration types to improve readability
2. use Enumeration type to improve reliability
3. simplify modification with Enumeration type
4. replace the enumerated type with a Boolean variable
5. check invalid value
6. define the first and last items of enumeration for circular boundaries.
for example, in VB,
Public Enum country
country_first = 0
country_china = 0
country_england = 1
country_france = 2
country_usa = 6
country_last = 6
end Enum
then you can use country_first and country_last as the Circular Boundary:
for I Country = country_first to country_last
...
next
7. Leave the first element of the enumeration type as an invalid value.
8. Clearly define the use rules of the first and last elements in the coding standard of the project, and maintain consistency during use.
9. be cautious about making mistakes when assigning values to enumeration elements.

· If your language does not have an enumeration type, if your language doesn't have enumerated types
If there is no Enumeration type, you can use global variables or classes to simulate it. For example, in Java, (maybe Java now supports enumeration types)
Class country {
Private country (){}
Public static final country China = new country ();
Public static final country England = new country ();
Public static final country France = new country ();
......
Public static Final Country USA = new country ();
}

12.7 named constant named Constants
1. Use a named constant in the Data Declaration
For example, const local_number_leghth = 7
2. Avoid using text volumes, even if it is "safe"
3. Simulate named constants with variables or classes with appropriate scopes (if not supported by the Language)
4. unified use of named Constants

12.8 array Arrays
1. Make sure that all the labels under the array do not exceed the boundary of the array.
2. Consider replacing arrays with containers or using arrays as ordered structures for processing.
Some smart people in the computer science community suggest never randomly access arrays, but only sequentially (mills and linger 1986 ). Their

The argument is that random access to an array is like using a GOTO statement in a program. They recommend that you use the data structure of sequential access elements such as set, stack, and queue to retrieve

Algebra group.
Before you habitually choose arrays, consider whether you can use other container classes that can access data in sequence as a replacement solution-such as collections, stacks, queues, and so on.
3. Check the boundary points of the array
4. If the array is multi-dimensional, make sure that the order in which the following objects are used is correct.
5. Watch out for subscripts.
If you are using nested loops, it is easy to write array [I] into array [J]. Changing the cycle subscript is called "subscripts in a string of cross-talk ". Ratio

I and J have more meaningful subscripts to reduce the occurrence of subscripts.
6. Use the array in combination with the array_length () macro in C.

12.9 create your own type (type alias) creating your own types (type aliasing)
Reason for creating your own type
1. Easy to modify
2. Avoid releasing too much information
3. Increased reliability
4. Make up for language deficiencies
· Guidelines for creating your own types
1. The created type is named function-oriented.
2. Avoid using predefined types
3. Do not redefine a predefined type.
4. Define alternative types for Migration
5. Consider creating a class instead of using typedef.

Key Points
1. Using a specific data type means remembering many independent principles that apply to each type.
2. If your language supports it, creating a custom type will make your program easier to modify and more descriptive.
3. When you use typedef or its equivalent method to create a simple type, consider whether to create a new class.

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.