After each method call, you must verify the return value to determine whether to return or continue execution. How can I adjust the following code? {Code...} after each method call, you must verify the return value to determine whether to return or continue execution. How can we adjust the following code?
/*** Execute business logic ** @ param $ action execution Method * @ param $ allParams * @ return array | bool */public static function parseMore ($ action, $ allParams) {// user login request data parsing $ inputs = self: userLoginParse ($ allParams); // verify if (self ::$ _ errorNo! = StatusCode: STATUS_TRUE) return array (); if ($ inputs = false) return false; // call the business method $ result = call_user_func (array ("Frontier ", strtolower ($ action), $ inputs); // verify if (self ::$ _ errorNo! = StatusCode: STATUS_TRUE) return array (); if ($ result = false) return false; return array ($ action => $ result );}
Reply content:
After each method call, you must verify the return value to determine whether to return or continue execution. How can I adjust the following code?
/*** Execute business logic ** @ param $ action execution Method * @ param $ allParams * @ return array | bool */public static function parseMore ($ action, $ allParams) {// user login request data parsing $ inputs = self: userLoginParse ($ allParams); // verify if (self ::$ _ errorNo! = StatusCode: STATUS_TRUE) return array (); if ($ inputs = false) return false; // call the business method $ result = call_user_func (array ("Frontier ", strtolower ($ action), $ inputs); // verify if (self ::$ _ errorNo! = StatusCode: STATUS_TRUE) return array (); if ($ result = false) return false; return array ($ action => $ result );}
In this scenario, exceptions should be used instead of return values.
IncludinguserLoginParseAnd thatcall_user_funcDynamic method, it seems to be in useself::$_errorNoTransfer error status. This is a bad mode that is not recommended for even C/C ++ (the error code is transmitted for global variables)
This code may be long like this.
public static function parseMore($action, $allParams){ try { $inputs = self::userLoginParse($allParams); } catch(EmptyInputException $e) { return array(); } $result = call_user_func(array("Frontier", strtolower($action)), $inputs); return array( $action => $result );}
It should be long like this
public static function parseMore($action, $allParams){ $inputs = self::userLoginParse($allParams); if($inputs === null) { return array(); } $result = call_user_func(array("Frontier", strtolower($action)), $inputs); return array( $action => $result );}
Depends onreturn array();AnduserLoginParseBusiness
I usually judge whether bool is returned by a function.
A) bool is mixed with other types: It must be unreasonable. For example, an exception should be used in the scenario of subject. For example, if the query XXX returns an object/false, null should be used.
B) Check the function name. If it cannot be changed to isXXX or hasXXXQuestion answering?, Problems
I will consider the following two methods:
1. Strip the check area and write a separate method to check$_errorNoAnd return values
2. In the called method, an exception is thrown insteadreturn falseAnd catch it outside. Based on the Exception code, determine whether to return a false or empty array.