Code Daquan Study Notes (iv): 7th chapter-Design of high quality subroutines

Source: Internet
Author: User

1. A subroutine is a callable method or procedure (procedure) that is written for a specific purpose, such as a function in C + +, a method in Java

Subroutine avoids code snippet duplication, improves code readability, and facilitates code churn

2. Good sub-program name

(1) Avoid the use of meaningless, ambiguous verbs, such as processinput ()

(2) Determine the length of the subroutine's name as needed, to be clear and understandable as standard

(3) Subroutine name is generally the form of verb garbin language

(4) Accurate use of antithesis words to provide a complete interface

3. Optimal length of subroutine

Theoretically the best length is 50-150 lines, must not exceed 200 lines

4. Principles for using sub-program parameters

(1) Arrange parameters in order of input-modify-output

Implied sequence of operations within subroutines: input parameters, modify data, output results

(2) Put the state variable at the end

(3) Do not handle the parameters of the program as a working variable, you should use local variables

C + + can declare input parameters as const, preventing subroutines from modifying this parameter

(4) Ensure that the form parameter matches the type of the actual parameter, and note that the compiler gives a parameter mismatch warning

5. Issues to consider when working with functions

(1) A function is a subroutine with a return value, and a procedure is a subroutine that has no return value.

(2) Set the return value of the function

Check all possible return paths, use a default value to initialize the return value

Suppresses the return of pointers or references to local variables

6. Macro functions and inline functions

(1) With as few macro functions as possible, when using macro functions, each parameter of the macro and the entire function body must be protected with parentheses to prevent ambiguity

(2) C + + offers a number of alternative macro solutions:

(3) Inline function avoids function call overhead

The inline function must be defined in the header file, and C + + functions defined within the class default to the inline function

Code Daquan Study Notes (iv): 7th chapter-Design of high quality subroutines

Related Article

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.