How to optimize php code structure

Source: Internet
Author: User
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.

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.