The fifth article: three ways to achieve the goal

zhaozj2021-02-17  49

Linuxaid.com.cn 01-07-10 10:51 175P

Axing

Using an assertion is a function or macro to protest loud when it is assumed incorrect. It can be used to verify the assumptions made in the program and exclude accidents. An assertive function is often roughly brought about two contents: assume that the Boolean expression and one of the time to print when it is time. The following is a Pascal assertion that assumes denominator is not zero: assert (denominator <> 0, 'denominator is undexpectedlg equal to 0.'); this assertion assumes that Denominator is not equal to "0", the first part denominator <> 0 is a Boolean expression, its result is TRUE or FALSE. The second part is that the result of the first portion is a FALSE elbow, the information to be printed. Even if you don't want users to see the assertion information in the final software, it is very convenient to use assertions in the development and maintenance phase. In the development phase, assertions can eliminate mutual contradictory assumptions, eliminate bad values ​​that are incorporated into the program, and the like. In maintenance, it can indicate whether the change affects the other parts of the program. In fact, the assertion This technology is already used throughout, in the Visual C environment of the Windows platform, it has already pre-issued a lot of assertions in advance. It is only necessary for a developer to do. Calling it; of course, developers under Linux are not so lucky, but if you want to use his words, it is not a difficult thing, as an example, you can find relevant information in Linux's source code, related information It also includes techniques such as preprocessing will be mentioned below. The assertion process is very easy to write, the following is an example written in PASCAL: Procedure Assert (Ases: String); Beginif (not assertion) beginwriteln (a "; writeln; Writeln; Halt (FATAL ERROR) Endend; Once such a process is written, you can call it with a statement like the first example. Below is some guidelines using assertions: If there is a pre-processing, use the pre-processing macro command. If you use the pre-processing process to process the assertion during the development phase, it is very easy to get the assertion in the final code. In the assertion, you should avoid using the executable code, put the executable code into the assertion, when the assertion is turned off, the compiler may discriminate. Please see the following assertions; assert (fileopen (inputfile "); 'couldnt Oped INPUT file'); the problem that is generated by this line is that if you do not compile the assertion, you can't make the code that opens the file, the executable statement should be Put it in your own position, assign the result to a state variable and then test the status. The following is an example of a safe usage assertion; filestatus: fileopen (inputfile); assert (filestatus <> null, 'couldn't Opeh Input File'); in fact, the biggest use of assertions is to generate two different software versions: debugging Edition and the final version.

I don't know how everyone is debugging, but joining a standard assertion program in their own procedure, and the pre-processing program (for example: #define debug) is a nice idea. If the developer of Visual C used by MS knows (also uses the hateful Bill to make an example, but only for the academic use) software has two versions of Debug and Release, which is the difference in this. In addition, it is very comfortable to develop software under the Window platform, and there is a version of the IDE environment. It is particularly convenient. LINUX is more hard, but there is at least a lot of source code under Linux, and I am The development work engaged in the maintenance environment can only be described by the miserators, and what is going to write itself. At that time, it feels that the person who makes the software is genius! Plan to remove the debug help debug help measures include: assertions, memory check reports, print statements, etc., and some other code written for convenience of debugging. If the software developed is for yourself, then reserved them in the program. However, if it is a commercial software, these measures are left in the program, which will affect performance indicators such as their speed and occupation. In this case, the plan should be made in advance to avoid debugging information in the program, and below is a few ways. Use version control. Version Control Tools can develop different versions of software from the same source file. At the development phase, you can set the version control tool containing all debug auxiliary means, which, to the product phase, you can easily remove these auxiliary means that you don't want to appear in commercial versions. I am too shallow, I don't know what is good for Linux (strong, free) version control tool, which is said that Rational's cleancase has Linux version, but I have never seen it. As for the Windows platform, MS's SourceSafe is very good, it is better to find, if you want to develop procedures under Linux, you can consider using it to help you manage the version. I don't know if Bill will die. Use internal pre-processing programs. If you have a pre-processing program in a programming environment, such as a C language, just use the compiler switch, you can add or remove these auxiliary means. You can use the pre-processing program directly, or you can define the pre-processing program by writing a macro. Below is an example of writing, directly using the pre-processing program: #define debug ... # ifdefined / * Debug code * / ... # ENDIF This idea may have several expressions. It is not only defined Debug, but also gives it a value, then test it, not testing it is defined. Use this method to distinguish between different levels of debug code. It is also possible to want some debugging code for a long-term settlement program. You can use the statements such as #if debug> 0, so that this code can be surrounded, and some other debug code may have some special use, this You can use a statement such as #if debug-a printer error to surround this code, you can also want to set the debug level, available as follows: #if debueg> level_a If you don't like the program inside #if defined () If such a statement, you can use a preprocessor macro to complete the same task.

The following is an example: #define DEBUG # if defined (DEBUG) #define DebugCode (code fragment) {code_fragment} # else # define DebugCode (code fragment) #endifDebugCode (statement 1; statement 2; ... statment n;) using a pre- Like the first example of the handler, this technique also has a variety of forms, which can make it more complex, so that it is not simply including or all excluded debug code. Write your own preprocessor. If there is no pre-processing program in the programming language, you can write a pretreatment program that joins or remove the debug code, which is very easy. It is also necessary to identify an agreement to debug code and write your own pre-compiled programs to follow this approach. For example, in PASCAL, a pre-compiled program that responds to the following keyword can be written: / # begin debug / and / # end debug / person and write a batch file to call this pre-processing, then compile this paragraph The code has been predicated. From the long-term point of view, doing this can save a lot of time because you will not compile the code without pre-processed. Retain use of debuggers. In many cases, a debug subroutine can be called for debugging work. The development phase is defined according to Debug, contains or does not contain this code. This subroutine may perform several operations before the control returns a call. In the final software, you can use a subroutine instead of that complex debug subroutine, which will return to control immediately, or return control after two quick operations. Using this method, the impact on performance is small, compared with the preprocessed proprigibility program, it has much faster. Therefore, it is necessary to retain two versions of this subroutine in the development phase and the final product phase for use in future development and product commissioning.

For example, you can use a subroutine that passes to the pointer as a start: Procedure Dosomething (POINTER: PONITER_TYPE; ...); Begin {Check Parameters Passed In} CHECKPOINTER; this row call check pointer subroutine. In the development phase, the checkpointer () subroutine will have a comprehensive inspection of the pointer. This work may take a time, but it is very effective. It is likely to be the following: Procedure Checkpointer (Pointer: Pointer_Type); This program checks each pointer that is incoming, which can be used during development. Finish check: Begin {Perform Check 1 - Maybe Chech That it's not nil} {Perfom Check 3 --Maybe Check That What is point to isn't corrupted} {Perform Check N --...} END; When entering the final product phase, it may not be desirable to contact the finger check with the pointer check. At this time, you can use the subroutine below to replace the subroutine: Procedure Checkpointer (Pointer: Polnter_Type); this program is only That is, it returns its caller becom {NO code; Just Return to Caller} end; these are not to remove all the programs of the debug aid, but from the perspective of providing some ideas that can work with the case in your environment, These are already enough. It is estimated that the change is almost inevitable for each program. For example, develop a new version of the old software, you need to make many changes to the original code, but even when developing a software's first edition, you have to do it due to the addition of some do not expected. change. When developing software, try to make it easy to change. Moreover, the more possible changes, the more it is easy to do, implicit the change domain you expected in it, which is one of the most powerful weapons that affect the program due to changes. It is expected that this kind of thing is easy to do. It is easy to do. The demand for software is constantly changing. I want to achieve the software function of one for all, which is undoubtedly an idiotic dream, but if you follow the coupling method mentioned in the previous article. If you do it, you will have a surprise harvest when you change. Entering garbage not necessarily outputting a good program that will never output a mess like garbage, no matter what it is entered. A good program is characterized by "entering garbage, nothing," or "entering garbage, output error information", or "not allowing garbage entry". From now on, "entering garbage, output garbage" is often inferior. Check the values ​​for all external programs input. When reading data from a user file, make sure that the reader's data value is within the allowable range. To ensure that the value can be taken, and the string should be short enough for processing. To comment out the allowable range of input data in your code. Check the full subroutine input parameter value. Check the subroutine input parameter value, in fact, the same as checking the external program data, but at this time, the subroutine replaces the file or user.

转载请注明原文地址:https://www.9cbs.com/read-29447.html

New Post(0)