在循环中的函数调用或函数调用本身附近是否更清晰地睡眠? [英] Is it clearer to sleep near a function call in a loop or in the function call itself?

查看:134
本文介绍了在循环中的函数调用或函数调用本身附近是否更清晰地睡眠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在循环中的函数调用或函数调用本身中,函数调用是否更清晰?我个人倾向于在呼叫附近睡觉,而不是在呼叫,因为没有什么关于getApple(),暗示它应该睡了一段时间,然后返回苹果。我认为它会更清楚:

Is it clearer to sleep near a function call in a loop or in the function call itself? Personally I lean toward sleeping near the call rather than in the call, because there is nothing about "getApple()" that implies that it should sleep for some amount of time before returning the apple. I think it would be clearer to have:


for ( int i = 0; i < 10; ++i ) {
    getApple();
    sleep()
}

比...


for ( int i = 0; i < 10; ++i ) {
    getApple();
}

Apple getApple() {
    sleep(1);
    return new Apple();
}

当然,如果方法 getAppleSlowly

请让我知道你的想法。

一些额外的信息):

等待不需要一个苹果。等待是为了避免每分钟对API的查询的速率限制,但如果你只得到一个苹果,则没有必要睡觉。睡眠中的方式使得不可能得到一个苹果没有睡觉,即使它是不必要的。然而,它有益于确保无论什么方法可以称之为它,而不必担心超过速率限制。但是,这似乎是一个参数重命名为getAppleSlowly()。

The wait is not required to get an apple. The wait is to avoid a rate limit on queries per minute to an API, but it is not necessary to sleep if you're only getting one apple. The sleep-in-get way makes it impossible to get an apple without sleeping, even if it is unnecessary. However, it has the benefit of making sure that no matter what, methods can call it without worrying about going over the rate limit. But that seems like an argument for renaming to getAppleSlowly().

推荐答案

我不会把 sleep 进入函数本身只要函数名不建议会有延迟。

I would not put the sleep into the function itself as long as the function name does not suggest that there will be a delay.

今后可能会有其他来电者不希望睡眠。

There might be other callers to the function in the future who do not expect it to sleep.

这篇关于在循环中的函数调用或函数调用本身附近是否更清晰地睡眠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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