函数调用与局部变量 [英] Function calls vs. local variables

查看:187
本文介绍了函数调用与局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到其他函数被多次调用的函数,而不是一次存储函数的结果。



(1)

  void ExampleFunction()
{
if(TestFunction()> x || TestFunction )< y || TestFunction()== z)
{
a = TestFunction();
return;
}
b = TestFunction(); strong>:

  void ExampleFunction()
{
int test = TestFunction
if(test> x || test< y || test == z)
{
a = test;
return;
}
b = test;
}

我认为版本2更好的阅读和更好的调试。
但是我想知道为什么人们像1号一样做?
有没有什么我看不到?性能问题?
当我看到它,我看到在最坏的情况下4个函数调用在号码(1)而不是1号函数调用在号码(2),因此性能应该在数量(1),不应该?

解决方案

如果我想强调,在整个代码中使用相同的值,我想强调的是,该值的类型是 int 。强调真实但不明显的事情可以帮助读者快速理解代码。



我会使用(1)如果我不想强调那些特别是如果它们不是真的,或者如果调用 TestFunction()的次数由于副作用而很重要。



显然,如果你强调目前 的东西,但是在未来 TestFunction()那么你有一个bug。所以我也想要控制 TestFunction()自己,或者对作者未来兼容性的计划有一定的信心。通常这种信心很容易:如果 TestFunction()返回CPU的数量,那么你很乐意拍摄该值的快照,你也很乐意存储它在 int ,无论实际返回什么类型。你必须对未来的兼容性有最小的信心使用一个函数。确信它不会在将来返回键盘的数量。但不同的人有时有不同的想法什么是突变,特别是当界面没有准确记录。因此,重复调用 TestFunction()有时可能是一种防御性编程。


I often see functions where other functions are called multiple times instead of storing the result of the function once.

i.e (1):

void ExampleFunction()
{ 
    if (TestFunction() > x || TestFunction() < y || TestFunction() == z)
    {
        a = TestFunction();
        return; 
    }
    b = TestFunction();
}

Instead I would write it that way, (2):

void ExampleFunction()
{
    int test = TestFunction();
    if (test > x || test < y || test == z)
    {
        a = test;
        return;
    }
    b = test;
}

I think version 2 is much better to read and better to debug. But I'm wondering why people do it like in number 1? Is there anything I don't see? Performance Issue? When I look at it, I see in the worst case 4 function calls in number (1) instead of 1 function call in number (2), so performance should be worse in number (1), shouldn't it?

解决方案

I'd use (2) if I wanted to emphasize that the same value is used throughout the code, or if I wanted to emphasize that the type of that value is int. Emphasizing things that are true but not obvious can assist readers to understand the code quickly.

I'd use (1) if I didn't want to emphasize either of those things, especially if they weren't true, or if the number of times that TestFunction() is called is important due to side-effects.

Obviously if you emphasize something that's currently true, but then in future TestFunction() changes and it becomes false, then you have a bug. So I'd also want either to have control of TestFunction() myself, or to have some confidence in the author's plans for future compatibility. Often that confidence is easy: if TestFunction() returns the number of CPUs then you're happy to take a snapshot of the value, and you're also reasonably happy to store it in an int regardless of what type it actually returns. You have to have minimal confidence in future compatibility to use a function at all, e.g. be confident that it won't in future return the number of keyboards. But different people sometimes have different ideas what's a "breaking change", especially when the interface isn't documented precisely. So the repeated calls to TestFunction() might sometimes be a kind of defensive programming.

这篇关于函数调用与局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆