Software Contract-based Design (DbC --- Design by Contract)
I. Basic Concept of DbC the idea of DbC is derived from the behaviors (obligations and interests) of businessmen and users in commercial activities. Both parties must abide by a contract to complete the transaction. The contractual relationship between sellers and users is as follows: 1. A seller must provide a certain product (obligation) and have the right to get the user's payment (interest); 2. the client must pay the fee (obligation) and have the right to obtain the product (Benefit); 3. both sellers and users must abide by certain obligations, such as laws and regulations. II. design by Contract is a software Design method, which was first proposed by Bertrand Meyer in 1986, dbC has been registered as a trademark by Eiffel Software. Developers call this method contract-based programming and contract-based priority programming. The idea of using DbC in software system development is to regard the software system as the mutual collaboration between different components. The mutual collaboration between components must follow the precisely defined specifications (contracts and contracts ). Many languages have the assert feature. Using C or C ++ with assertions can ensure the compliance between different components and ensure the correctness of software development, therefore, DbC claims to write assertions first, which should be part of the software design process. Advantages of using DbC: (1) Easy to debug and discover errors in the program; (2) Easy to reuse code. A software module with a contract describes the behavior of the module, this document is easy to generate code reuse. (3) It has little impact on Running code [2]. Traditional debugging and DbC debugging: (1) if-else method: When a traditional program module starts, many If (condition) {conditions met;} else {not met, the format of return} is used to detect boundary conditions (also called contracts ). When the condition is met, the execution module function code is executed. If the condition is not met, the caller is returned, and errors are handled by the caller. (2) try-catch Method: run the code in try {module function code} first. If there is an error (exception), run catch (expected exception) {caught exception} is used to capture exceptions. This is run first, and then check whether there are any error methods. (3) assert () usually used in Java language development for white-box testing and functional boundary testing. But it is a process of compiling a function and testing a function. (4) DbC method: first look at the contract and then run the method. It also uses assertions. It is a bit similar to the first if-else method. However, when an error occurs, it automatically calls the callback function my_onAssert () instead of returning it to the caller like the first method. In the callback function my_onAssert (), you can display the file and row number of the Error. During embedded system development, you can also implement serial communication in the callback function to transmit the error to the PC; you can also use the number of LED flashes to indicate the wrong position. II. Implementation of DbC (C language)
Iii. Used C99 predefined macros
(1) _ FILE _: name string of the source FILE; (2) _ LINE __: number of locations of the row in the source FILE; (3) # name: that is, the string corresponding to name. 4. DbC and ROM.