Guide to High quality C++/C Programming-6th Chapter-Functional Design (3)

Source: Internet
Author: User

For the Add function, you should return a string object in the form of "value delivery." If you use reference passing instead, the function return value is a reference to temp for the local object. Because temp is automatically destroyed at the end of a function, it will cause the returned reference to be invalid. For example:

c = a + B;

At this point A + B does not return expectations, C nothing to get, shed a hidden danger.

6.3 Rules for internal implementations of functions
Functions of different functions are internally implemented differently, and it seems impossible to agree on an "internal implementation". However, based on experience, we can strictly check the "entrance" and "exit" of function body, so as to improve the quality of function.

L "Rule 6-3-1" in the "entrance" of the function body, check the validity of the parameter.

Many program errors are caused by illegal arguments, and we should fully understand and use Assert to prevent such errors. For details, see section 6.5, "use assertions."

L "Rule 6-3-2" at the "exit" of the function body, check the correctness and efficiency of the return statement.

If the function has a return value, then the "exit" of the function is the returns statement. Let us not despise the return statement. If the return statement is not well written, the function either goes wrong or is inefficient.

The following considerations are:

(1) The return statement cannot be returned to "pointer" or "reference" to "stack memory" because the memory is automatically destroyed at the end of the function body. For example

char * Func (void)

{

Char str[] = "Hello World"; STR's memory is on the stack

...

return str; Will cause an error

}

(2) To find out whether the return is "value", "pointer" or "reference".

(3) If the function return value is an object, consider the efficiency of the returns statement. For example

Return String (S1 + s2);

This is the syntax for the temporary object, which means "create a temporary object and return it." Do not assume that it is equivalent to "create a local object temp and return its result" first, as

String Temp (s1 + s2);

return temp;

In essence otherwise, the above code will happen three things. First, the Temp object is created, the initialization is completed, and then the copy constructor copies the temp to the external storage cell where the return value is saved; Finally, the temp is destroyed at the end of the function (calling the destructor). However, the process of "creating a temporary object and returning it" is different, and the compiler creates and initializes the temporary object directly in the external storage unit, eliminating the cost of copying and deconstructing and increasing the efficiency.

Similarly, we do not want to

return int (x + y); Create a temporary variable and return it

Written

int temp = x + y;

return temp;

Because the internal data type, such as int,float,double, does not have constructors and destructors, although the syntax for the "temporary variable" does not increase the efficiency, the program is much simpler and easier to read.

6.4 Other recommendations
2 "recommendation 6-4-1" function to a single function, do not design multi-purpose functions.

2 "recommendation 6-4-2" function body size is small, try to control within 50 lines of code.

2 "recommendation 6-4-3" try to avoid functions with "memory" function. The same input should produce the same output.

A function with a "memory" function whose behavior may be unpredictable, because its behavior may depend on some kind of "memory state." Such a function is neither easy to understand nor conducive to testing and maintenance. The static local variable of a function is the "memory" memory of a function in the C + + language. It is recommended that you use a static local variable as little as possible unless required.

2 "recommendation 6-4-4" Not only checks the validity of input parameters, but also examines the validity of variables that enter the body of the function through other means, such as global variables, file handles, and so on.

2 "recommendation 6-4-5" for error handling of the return value must be clear, so that users can not easily ignore or misunderstand the error situation.

6.5 Using assertions
The program is generally divided into debug and release versions, and the debug version is used for internal debugging and release versions are available to users.

Assert assert is a macro that works only in the debug version and is used to check for situations where "should not" occur. Example 6-5 is a memory copy function. During the run, if the Assert argument is false, the program aborts (and generally prompts a dialog indicating where the assert was raised).

void *memcpy (void *pvto, const void *pvfrom, size_t size)

{

Assert ((pvto!= null) && (pvfrom!= null)); Using assertions

byte *pbto = (BYTE *) pvto; Prevent change of Pvto address

byte *pbfrom = (BYTE *) Pvfrom; Prevent change of Pvfrom address

while (Size-> 0)

*pbto + + = *pbfrom + +;

return pvto;

}

Example 6-5 copying a block of memory that does not overlap

The assert is not a hastily pieced together macro. In order not to cause differences in the debug and release versions of the program, the assert should not have any side effects. So assert is not a function, but a macro. Programmers can think of assert as a harmless test that can be safely used in any system state. If the program terminates at the assert, not that the function containing the assert has an error, but that the caller has made a mistake, the assert can help us find out why the error occurred.

There is very little more frustrating than the assertion that tracks the program, but does not know the effect of the assertion. You spend a lot of time not trying to rule out mistakes, but just trying to figure out what the mistake is. Sometimes, programmers occasionally design false assertions. So if it's not clear what the assertion checks, it's hard to tell whether the error is in the program or in the assertion. Fortunately, the problem is well solved, just add a clear annotation. This is obvious.

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.