1. What is a subroutine?A subroutine is a method or process written for a specific purpose.
Ii. Why do I write subprograms?1. complexity can be reduced. If you are faced with a complex logic, you can use a subroutine to effectively break down the logic. For example, you can encapsulate some column operations and pass some logic through the function name. 2. Improve portability. If you want a program to be highly portable, you can isolate it from hardware or non-verbal standards through subroutines to achieve higher portability. 3. Avoid repeated code. Repetitive code is troublesome to maintain and occupies unnecessary space. If similar code appears in two programs, it indicates that there is a problem in program decomposition, and the changes will increase the trouble. 4. Hide the operation. Where pointer, global variable, or complex Boolean judgment are used, the program can be encapsulated to focus more on the problems to be solved.
3. How to design a subroutine?Generally, a sub-program only achieves a specific purpose, so the main principle of the sub-program design is to enhance the cohesion of the sub-program. 1. If a sub-program only implements a single function, such as sin (), it can be considered as highly cohesive. 2. If there are two features in a sub-program, and the two features only use the same data and there is no other connection, it is called communication cohesion. For example, print the values in a report and reset the values in the report. In this example, the print and reset operations only use the same data, but there is no further link. Therefore, the cohesion of such subprograms is not as strong as that of functions. If printing and resetting are placed in different subprograms, and two subprograms are called by higher-level subprograms, the cohesion is improved. 3. If a sub-program has an operation that only needs to be executed at the same time, it is called temporary cohesion. For example, the system needs to read different configuration files, initialize temporary files, and set the manager for startup. This can be used for temporary cohesion subprograms to call the subprograms composed of each operation rather than directly executing all the operations.
4. How do I name subprograms?1. Describe all the things performed by the subroutine. A good subroutine name usually plays a role in the subinterpretation subroutine function. 2. Avoid meaningless, fuzzy, or ambiguous verbs. Some words can be expressed flexibly and can be extended to cover almost any meaning. For example, if handleoutput () is changed to formatandprintoutput (), the function of this subprogram is very clear. 3. Do not only apply to numbers to form different subprogram names. For example, the numbers following the subroutine names outputuser. outputuser1 and outputuser3 cannot show the differences between the abstractions represented by the subroutine. 4. Name the process in the form of a verb home object with a strong tone. For example, printdocument (), calcmonthlyrevenues (), and checkorderinfo () are all good names. In object-oriented language, you do not need to include the Object Name (object) in the process name. Because the object itself is already included in the call statement, you can use document. print (), orderinfo. check. Statements such as document. printdocument () are not only too bloated, but may also be misunderstood when they are called in Their Derived classes. For example, if the check class is inherited from the document class, check. print () clearly indicates printing a check, and check. printdocument () Looks like printing the checkbook or credit card statement instead of printing the check itself. 5. accurately use the opposite word. It is helpful to maintain consistency by following the naming rules of the right word. The following are some common Captain phrases:
add/remove increment/decrement open/closebegin/end insert/delete show/hidecreate/destroy lock/unlock source/targetfirst/last min/max start/stopget/put next/previous up/downget/set old/new
6. Establish naming rules for common operations. In different systems, it is very important to differentiate different types of operations. For example, to obtain the ID of an object: if every object in a project is assigned a unique identifier, a naming rule is set up for the subroutine that returns this identifier, so that the following name is used:
employee.id.Get()dependent.GetId()supervisor()candidate.id()
Specifically, the employee class provides its ID object, which in turn provides the get () method; the dependent class provides the GETID () method; the supervisor class uses ID as its default return value. The candidate class considers that the default return value of the ID object is ID, so the ID object is exposed. In this way, the acquisition of an ID object requires so many details, and these problems can be completely avoided by establishing a naming rule for the acquisition of ID.
5. How long should a subroutine be?Generally, the maximum length of 200-is a good interval. When a sub-program exceeds rows, special attention should be paid to it. Generally, such a program will encounter problems in readability sooner or later.
6. How do I use the subprogram parameters?1. parameters should be transmitted in the input-Modify-output order. In this way, the transfer is exactly the same as the execution sequence of subprogram input, processing, and output. However, in the C language, the modified parameters are arranged in the first order. It is best to use it in a certain way, at least according to certain rules throughout the project. 2. Consider defining your In and Out keywords. Here, in and out only play a role in illustration. If you want to change the parameter value, you still need to pass a pointer or reference. Note two aspects before applying this technology. 1) the in and out extensions of the standard language may make people using this language unfamiliar. 2) even if the in clause is used, the parameter value can still be changed in the subroutine. In the opinion of others, the function of the subroutine is correct, leading to misunderstanding. Note: In C ++, the const keyword should be used for input limitation. 3. If several subprograms use similar parameters, the order of their parameters should be consistent. For example, printf and fprintf only have one more F, so the parameters should be in the same order. Do not cause memory difficulties due to such a small order problem. 4. The status and error variables should be at the end of the parameter table. For state and error variables, they are only ancillary functions of the program, and they are only used for output parameters, so this is a very reasonable principle. 5. Do not use subroutine parameters as working variables. You should apply for additional local variables in the subroutine. If a parameter is used as a working variable and the value of the parameter has been changed in the program, an error occurs if you want to use the value of the original input parameter somewhere in the subroutine. 6. Describe the parameter assumptions in the interface. If you assume that the parameter passed to the subroutine has certain characteristics, you should describe this assumption. Which parameter assumptions need to be described? 1) The parameter is only used for input, modification, or output. 2) The unit of the number of parameters (inches, feet, meters, etc ). 3) If no enumeration exists, state code and error meanings should be described. 4) Types of values that can be received. 5) the specified value that should not appear. 7. Set the number of parameters of a subroutine to less than 7. If you find that you are always passing many parameters, it means that the coupling between subprograms is too tight. You should re-design this or this subprogram to reduce the coupling between them. If you pass the same parameters to many different subprograms, make these subprograms into a class and use the frequently used data as the internal data of the class. 8. consider naming rules for parameters that indicate input, modification, and output. If you think it is important to separate input, modification, and output parameters, you should establish a naming rule to differentiate them. 9. Pass an object or variable for the subroutine to maintain its interface abstraction. For example, if you have an object that exposes data through 10 accessors, The called subroutine can be operated only by three of them. If the abstract to be expressed is a subprogram that expects three specific values, but these three specific values happen to be provided by the same object, three data items should be transmitted separately. However, if the abstract to be expressed by a subroutine interface has always owned a specific object and the subroutine needs to perform such and such operations on the object, if three specific data items are transmitted separately, that is, it destroys the abstraction of several ports. If you use the method of passing the entire object, and find that you create the object first, fill in the three data items required by the called subroutine with this object, after a subroutine is called, three data values are retrieved from the object, which is evidence that you should pass only the three data items instead of the entire object. (In general, if the assembly code appears before the subroutine is called, or the disassembly code appears after the subroutine is called, it is a poor design of the subroutine .) If you find that you often need to modify the parameter table of the subroutine, and each modification of the parameter is from the same object, it means that you should pass the entire object instead of individual parameter items. 10. Make sure that the actual Parameters match the formal parameters. Always check the type of parameters in the parameter table and pay attention to the type warnings given by the compiler.
7. What are the return values of subprograms?1. Check all possible return paths. Make sure that all paths return values. 2. Do not return pointers or references to local variables.
8. Macro subroutine usage?1. Include the macro expression in parentheses. Macro expressions are included in parentheses to prevent errors and priority issues after expansion. 2. Enclose Macros containing multiple statements in braces. In this way, only one statement is executed in the loop and other operations, while other statements are not executed.
Chapter 7 high-quality subprograms