GURU Of The Week Terms 20: Complexity of Codes (Part 1)

zhaozj2021-02-08  452

GotW # 20 CODE Complexity - Part I

Author: Herb Sutter

Translation: k] [N g of @ rkTM

[Declaration]: This article takes the Guru of The Week column on www.gotw.ca website, and its copyright belongs to the original person. Translator Kingofark translated this article without the consent of the original person. This translation is only for self-study and reference, please read this article, do not reprint, spread this translation; people download this translation content, please delete its backup immediately after reading. Translator Kingofark is not responsible for people who violate the above two principles. This declaration.

Revision 1.0

GURU Of The Week Terms 20: Complexity of Codes (Part 1)

Difficulty: 9/10

(This clause puts forward a fun challenge: How many ways to perform a road in a function of only three lines of code? The answer is almost certainly surprised.)

[problem]

In the case where there is no other additional information, how many of the following codes do not perform a road?

String EvaluateSarandReturnName (Employee E)

{

IF (e.title () == "CEO" || E.SALARY ()> 100000)

{

Cout << E.First () << "<< E.last ()

<< "is overpaid" << Endl;

}

Return E.First () "" E.last ();

}

[answer]

Assumption:

a) Ignore the different sequences of the function parameter and the exception thrown by the destructor. [Note 1]

The following questions provide fearless brave:

If you allow the patterned function to throw an exception, how many of the way is there to perform the passage?

b) The function called is considered atomic. In fact, such as "E.TITLE ()" This call may throw an exception due to several reasons (for example, itself may throw an exception; it may also "fail to capture another function called by it Throwing exceptions and throw an exception; or it might use the Return By Value to cause the temporary object to construct the constructor to throw an exception). Here we assume that for functions, only pay attention to the result of executing the E.TITLE () operation, that is, whether it is thrown after the operation is completed.

Solution: 23 (only in the 4 line code!)

If you find a grade to yourself

-------------------------------------------------- -------------------

3 average level (average)

4-14 can cognition anomalies (Exception-Aware)

15-23 elite qualification (Guru Material)

This 23 execution path includes:

--3 3 and unrelated (non-Exceptional) path

- 20 hidden paths, all related to abnormalities to understand the three ordinary paths, 诀窍 is to know the C / C "Short-circuit Evaluation Rule":

1. If e.title () == "CEO", then it is not necessary to evaluate the second condition (for example, e.salary () will not be called), but COUT is still executed. [Note 2]

2. If e.title ()! = "CEO" but E.SAlary ()> 100000, then two conditions will be evaluated, and COUT will be executed.

3. If e.title ()! = "CEO" and E.SAlary () <= 100000, then COUT will not be executed.

The following is performed by an exception:

String EvaluateSarandReturnName (Employee E)

^ * ^ ^ 4 ^

4. The quotes use the Pass By Value mode, which will evoke the Employee Copy Constructionor. This COPY operation may throw an exception.

*. When you copy the function temporary return value to the area of ​​the function caller, String's Copy Constructor may throw an exception. However, we have ignored this possibility because it happened outside the function (not to mention from the current situation, the existing execution path is already busy!)

IF (e.title () == "CEO" || E.SALARY ()> 100000)

^ 5 ^ ^ 7 ^ ^ 6 ^ ^ 11 ^ ^ 8 ^ ^ 10 ^ ^ 9 ^

5. Member function title () itself may throw an exception; or it uses Return By Value to return to Class Type objects, causing copy operations that may throw an exception.

6. In order to match the valid Operator ==, the string may need to be converted to a temporary object that is the same as the class Type (or the return type of E.TITLE ()), and the construction of this temporary object may throw an exception.

7. If Operator == is the function provided by the programmer, then it may throw an exception.

8. Similar to # 5, salary () itself may throw an exception, or throw an exception during the construction of a temporary object due to its return of temporary objects.

9. Similar to # 6, it may be necessary to construct temporary objects, and this constructor may throw an exception.

10. Similar to # 7, this may be a function provided by the programmer, then it may throw an exception.

11. Similar to # 7 and # 10, this may be a function provided by programmers, then it may throw an exception.

Cout << E.First () << "<< E.last ()

^ 17 ^ ^ 18 ^

<< "is overpaid" << Endl;

12-16 As described in the C standard, five of the five to Operator << may throw an exception. 17-18 Similar to # 5. FigSt () and / or last () may throw an exception, or due to its return to temporary objects, it may throw an exception during the construction of an object.

Return E.First () "" E.last ();

^ 19 ^ ^ 22 ^^ 21 ^ ^ 23 ^ ^ 20 ^

19-20 is similar to # 5. FigSt () and / or last () may throw an exception, or due to its return to temporary objects, it may throw an exception during the construction of an object.

twenty one. Similar to # 6, it may be necessary to construct temporary objects, and this constructor may throw an exception.

22-23 Similar to # 7, this may be a function provided by the programmer, then it may throw an exception.

The purpose of the GOTW clause of this issue is to demonstrate how many hidden execution paths can exist in a simple code in a language allowing an exception mechanism. This hidden complexity affects the reliability and measurability of the function? Please look for this question in the next gotw.

[Note 1]: Never allow an exception to penetrate from the destructor. If this is allowed, the code will not work properly. Please see more discussion in C Report Nov / DEC 1997: Destructors That throw and why they're eviL.

[Note 2]: If ==, || and> is properly reloaded (OVERLOAD), then in the IF statement, || perhaps a function call. If it is a function call, "Short Remark Rules" is inhibited so that all conditions in the IF statement will always be evaluated.

(Finish)

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

New Post(0)