A function inside with a return good or multiple return good?

Source: Internet
Author: User

   0)    {        return true;    }    else    {        return false;    } } function dmeo2($num) {        $status = null;    if($num>0)    {       $status  = true;    }    else    {       $status  = false;    }    return $status; }

Reply content:

   0)    {        return true;    }    else    {        return false;    } } function dmeo2($num) {        $status = null;    if($num>0)    {       $status  = true;    }    else    {       $status  = false;    }    return $status; }


php// 一般情况function foo($num) {    if ($num>0) {        return true;    }    return false;}// 特殊情况:正好需要返回bool值function bar($num) {    return $num>0;}

Your code is fine, and I'll rewrite it for you again:

Original code:

javascriptfunction dmeo1($num){    if($num>0)    {        return true;    }    else    {        return false;    }}

Rewrite it again:

javascriptfunction dmeo1($num){    flag = $num > 0;    if(flag === true)    {        return true;    }    else    {        return false;    }}

The code idea was instantly clear.

How much return does not matter, to keep the code highly concise and understandable

function dmeo($num) {    return $num > 0;}

function dmeo3($num){    return $num>0;}

Flexible according to the structure

function dmeo($num) {    return $num>0;    }

The one that's not highlighted is "Returm"?

Removing the return of the guard condition type, multiple return is not a description of the function complex, too much responsibility and need to reconsider the design.

Return early ...

The return time is return, there is no forced to say a few return bad, after all, a return represents a logical fragment, so as long as the logic is clear, the code is clear enough!

You should export the return as early as possible, which would improve efficiency, meaning that you have already determined that the data has been obtained and that the execution of the code is not very

Agree @ south of the past, if a function in the first line can return, then there is no need to wait until the last line, efficiency first, the readability is absolutely different!

Which is the best way to use it to make the code clearer, but using multiple return typically is a better choice.
Refer to the "refactoring-Improve Code Design" section 9.5 for details.

The teacher said, the program should be an exit, avoid the use of multi-return,break

If there are many defensive designs in the program and the program exits after an error, then return directly at each error, otherwise:
If the program is an algorithm, there is a definite return value type, then I think it is a return good:

function getValue($x) {    $y = 0;    if ($x>0) {        $y = $x*$x;    } else if ($x == 0) {        $y = -1;    } else {        $y = 1/$x;    }    return $y;}


I personally think the first kind of good, if when a method is very long, with return can be seen where the direct exit, in the second method is not to see the last?

 function dmeo2($num) {        $status = false;    if($num>0)    {       $status  = true;    }    return $status; }

I'm more used to this.

Let's look at the specifics, and if some premises are judged, it's natural to return early (i.e. the first one). The second should be the kind of logical operation, and finally give a state of execution.

I prefer the second, use the variable to control the return of the results, rather than at each point of the program directly return, program controls, how can the redundant process?

It's really simple, the fewer branches, the better.

Why don't you give him a name for his method?

function isNonNegative($num) {    return $num > 0;}
  • Related Article

    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.