Kingofark's INEffective CC ++: Self-White 1: Return Value Application

zhaozj2021-02-08  547

INEFFECTIVE C / C : The Confession of A Novel "ineffective C / C : a low white"

BY K] [N g of @ r k

[Declaration: Kingofark is not a master, and the examples raised in the discussion may not be available, and it is not necessarily a good practice. All examples are merely illustrative and are not representative. Kingofark self-love is shallow, welcome to propose criticism and pointed out the mistake. ]

Item 1: Return Values ​​Saturc: Return Value Application

[Question]: Try the following code fragment.

/// ... (in the external definition) // There is a set of functions belong to the same system, all of which are OK or NG as return value #define ok 0 # define ng -1int Afunction (); // Return OK or NG

// ... (in a function) INT RTN; // Store the return value of the transfer subunies RTN = OK; // Initialization // ... RTN = AFUNCTION (); // Call a function IF (RTN == Ng) {// ... (here do some error handling) Return RTN; // As long as you are wrong, you will return to the outlet} // ... Return RTN; // OOPS! Current Function Return /

[View of Kingofark]:

First, it is to be described, and the return value is set to OK, NG, and let a set of functions have the same return mode. This is not a good idea (in fact, the author thinks this design is very poor - but you know, when you When it is a software worker, it is not what you design it yourself), but this is not the focus to describe this terms, so we do not discuss these external background conditions.

This code debris has at least three issues:

1) Decision method of function return value

/ IF (RTN == Ng) /

"if (RTN == Ng)" means: as long as the return value is not NG, it is considered to be success. And "if (RTN! = OK) means: as long as the return value is not OK, it is considered to fail.

The former means that there is a logical error or an unpredictable error that occurs in the child function, and the failure of the sub-function is not caught in the code writer. The execution route causes the route to return NG, and this sub-function call is still considered successful - because this The time the child function returns very likely OK, even a spam value (if the internal variable is used as a return value inside the child function), but it does not equal NG!

The latter is attached to the meaning of the function, that is, only the correct way to execute the route in the child function, causing the correctness of the call to return to OK, all other cases (including the function failure to return NG and any other Unpredictable errors are considered to fail - the benefits of doing this are:

a) Easy to debug: Just return OK as long as there is no expected execution route inside the function, then the function call is considered to be failed. This is often possible to find low-level logic errors within some functions;

b) Promoting the function writer considers as much as possible: If you really want the function to have a high fault, please arrange the return OK.

2) Initialization

/ RTN = OK; // Initialization /

The variable returned to the storage function is initialized to the return value of success, meaning:

a) If this variable is used in the later code, it is not careful to return (so that the compiler will not warn your variables), then this function will think is Successful calls regardless of any incredible things, so that the debugger is difficult to find this function (the debugger must carefully examine the value associated with this function and the effect of the function). b) Writing the code may be an optimist. Optimistic is a good thing, but it is not to say that it will not make mistakes - we want "serious and lively" :-)

It seems to be written "RTN = Ng;" better, because this means: In the function, as long as the RTN is assigned to OK, then when using "return RTN;" back, it will return NG to cause your attention, See if it is wrong (forget using this variable).

3) Returns

/ RETURN RTN; // OOPS! Current function returns /

If the function does not have an error system (ie, the return value covers a series of designed errors), it seems to be explicitly returned in OK or NG, thereby avoiding the return value for some reason, such as

RTN = fun (); // misuse: Unlucky! Fun () happens to be a function that may return multiple error codes; // Note: It is not good to return itself!

or

// Note: This method is too surprised, absolutely not recommended! Only use as an example. Return (RTN || Flag); // In error: miserable! I wanted to write | (where Flag = -1)

In addition, there is a problem that needs attention: be sure to determine the function called according to the return value of the function! !

It seems to be a nonsense, but someone does this:

/// ... (in the same system as previously) INT myfun () {if (...) {return 1; // a return case} // ... return 0; // another Return // ...}

// ... (in another place) RTN = myfun (); if (RTN! = OK) // or even if (RTN == ng) {//... }/

Yes, in general, we will define something such as OK as 0, but if not? If the system is revised, another master determines whether the OK should be equal to 100?

In the above code, the writer of MyFun () is just only forgotten in the comment and documentation, "0 corresponds to the OK in the system; -1 corresponds to the NG in the system" and "return -1; "Misreaded as" Return 1; ",,,,,,,,,,,,,,,,,,,,,,,,,:::::::::::::::::::::::::::::::::::::::

As long as you insist on using the return value to use the return value to return the value, then whether the writer of the function has forgotten the description, what other trivia, your practice is right. If you look at it, you think, oh, it should be writer forgot to explain, "I should" use OK / NG, so you do this, then when OK is unfortunately redefined as 100 and the function When you do any changes, you will be you.

[Kingofark's harvest]: 1) Ning can kill a thousand, can not let go.

2) Write only, don't play.

3) The gap between ideals and reality is: the ideal mistake is what you want, and the real mistake is something you can't think of.

4) When you are in the process of adjusting, please take yourself to the place where you know the most impossible, then take a closer check.

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

New Post(0)