Chapter 6: Function Design

First, the parameters of the rule

1, to complete writing parameters (parameter statements can not write the name)

2, the parameter name to the right, the sequence to be reasonable (preferably in front of the object parameters, source parameters on the back)

3, if the argument is a pointer, and is used only for input, should be added in front const limit, the contents of the protected space will not be modified

4, if the parameter value is passed, preferably into const & embodiment (not create a new variable value but also its protection is not modified)

5, the number of parameters try not to over 5

6, try not to use the type and number of uncertain parameters

(Number of parameters print function is uncertain)

 

Second, the rule returns the value of

1, do not omit the return type of the value, if no return value type is declared as void

(C language does not return a value, it defaults to int, C ++ type safety checks will be carried out, an error)

2, the function name and the type of the return value may not conflict semantically

(Getchar function is an exception, the return value is of type int)

3, the normal value obtained with the output parameters, error flag is returned by the return statement

 

Third, the internal function implementation rules

1. validity check at the entrance of the parameters of the function (assertion assert)

2, check the correctness and efficiency of the return statement at the exit function.

  a, return statements must return a pointer to the stack memory (the functions that create variables) is a pointer or reference

  b, to find out what the return value is the value of the pointer or reference

  c, if function return value is an object, to consider the efficiency of return statement

(If it is a class object, it is best to create a temporary object and returns it, if it is returned directly to the internal type)

 

Fourth, other recommendations

1, the function of the function to a single

2, the size of function body to be smaller

3, the function to avoid having a "memory"

4, not only to check the validity of input parameters, but also to check the validity of the variables into the function of the body by other means

5, the return value for error handling must be clear

 

V. assertion assert

  Assertion is only in the Debug version of the work of the macro, which is used to check the "situation should not have happened" if there is an error condition will stop the program immediately

1, the use of assertions to capture illegal situation should not have happened

2, at the entrance of the function, using the validity (legality) assertion checking parameters

 

Sixth, compared with the reference pointer

(This part I can see another blog: https://www.cnblogs.com/lyf98/p/11741953.html )

 

Guess you like

Origin www.cnblogs.com/lyf98/p/11747007.html