Each function is used to complete a single function. A single function is easy to understand, test, and predict. Input to the output. During the design, functions are divided into query and command. Query function: only queries the object state without changing the object state. Command function: only modifies the object state without returning the object state.
Each function is used to complete a single function. A single function is easy to understand, test, and predict. Input to the output.
During the design, functions are divided into query and command.
- Query function: only queries the object state without changing the object state. For example, dist_length indicates the length of the two-way linked list returned.
- Command function: only modifies the object state without returning the object state. For example, dist_delete only deletes the linked list node and returns whether the operation is successful without returning the current length or whether the node exists.
Queries are classified into basic queries and composite queries.
- Basic query: only query the single state of an object.
- Compound query: you can query multiple states of an object.
For example, window_get_width () or window width, window_get_rect () gets the upper left corner, width, and height of the window.
During implementation, check the input data and confirm that the user has correctly called the function. Contractual design defines the responsibilities of callers and implementers.
Check your function
- Pay attention to syntax errors, code layout, and variable naming.
- Pay attention to common compilation errors, such as memory leakage and possible out-of-bounds access. The variable is not initialized, the function does not return a value, and the function does not return a value.
- Simulate computer execution. Check whether the execution ends as expected.
- Let me explain the implementation process of the program first. Check whether the process is understandable.
Check for errors
- Memory leakage
- Memory access out of bounds
- Wild pointer
- Access a null pointer
- Reference uninitialized variables
- Unclear pointer operation
- Errors caused by structure initialization
- Structure Size changes
- Allocation and release do not match
- Returns a pointer to a temporary variable.
- Trying to modify constant global variables and strings
- Misinterpret value transfer and reference
- Symbol Name duplication
- Stack Overflow
- Misuse sizeof
- Byte alignment
- Byte Order
- Multi-threaded shared variables are not modified using volitile
- The return value of the function is missing.
This article is available at http://www.nowamagic.net/librarys/veda/detail/486.