是在循环中的函数调用附近休眠还是在函数调用本身中休眠更清晰?
是在循环中的函数调用附近休眠还是在函数调用本身中休眠更清晰?就我个人而言,我倾向于在调用附近而不是在调用中休眠,因为“getApple()”中没有任何内容表明它应该在返回苹果之前休眠一段时间。我认为拥有:
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() 的一个论据。
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() }
than...
for ( int i = 0; i < 10; ++i ) { getApple(); } Apple getApple() { sleep(1); return new Apple(); }
Of course, this would be different if the method were getAppleSlowly() or something.
Please let me know what you think.
Some additional information (also in comments below, see comments):
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().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
理想的情况是用一种方法做一件事。由于获取苹果和睡觉是两件不同的事情,我同意你的观点,最好让它们成为两种不同的方法。
The ideal is for one method to do one thing. Since getting apples and sleeping are two different things, I agree with you that it'd be better to have them be two different methods.
只要函数名称不暗示会有延迟,我就不会将
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.
我认为这是一个公平的问题,在一个你可能不知道它存在的方法中放置睡眠是非常糟糕的(想象一下,当你在几个月内尝试调试应用程序的缓慢程度时)忘记了你做了什么。只有当你明白为什么要睡觉时才应该睡觉(而且大概你有一个很好的理由)。
I think this is a fair question, and it's very bad to put a sleep inside of a method where you might not know it's there (imagine trying to debug the slowness of your application in a few months when you have forgotten what you have done. The sleep should be only where you understand why it's sleeping (and presumably you have a good reason for that).
这是一个非常有趣的问题,我认为您的两个解决方案都有些缺陷。 “getApple(); sleep();”即使我们再也不会执行 getApple() ,解决方案也会强制每个 getApple() 在处理之前暂停。 “sleep(); return new Apple();”解决方案在我们得到的第一个苹果上有类似的开销。最佳解决方案是这样的。
我倾向于将“我要睡多久才能避免 API 节流”的整个事情放在某个接口后面,并让其他一些类完全负责执行它。
This is a very interesting question and I think that both of your solutions are somewhat flawed. The "getApple(); sleep();" solution suffers from forcing each getApple() to pause before processing even if we will never again do a getApple(). The "sleep(); return new Apple();" solution has a similar overhead on the first Apple we get. The optimal solution is something like.
I would be inclined to get the whole "how long do I sleep to avoid the API throttle" thing behind some interface and make some other class wholly responsible for enforcing it.