相关变量和操作的分组?
在Code Complete,第10章中,建议对相关语句进行分组,并给出了以下示例:
void SummarizeData(...) {
...
GetOldData( oldData, &numOldData );
GetNewData( newData, &numNewData );
totalOldData = Sum( oldData, numOldData );
totalNewData = Sum( newData, numNewData );
PrintOldDataSummary( oldData, totalOldData, numOldData );
PrintNewDataSummary( newData, totalNewData, numNewData );
SaveOldDataSummary( totalOldData, numOldData );
SaveNewDataSummary( totalNewData, numNewData );
...
}
据说这种分组和并发处理是糟糕的设计,而是给出了更分离的东西:
void SummarizeData(...) {
GetOldData( oldData, &numOldData );
totalOldData = Sum( oldData, numOldData );
PrintOldDataSummary( oldData, totalOldData, numOldData );
SaveOldDataSummary( totalOldData, numOldData );
...
GetNewData( newData, &numNewData );
totalNewData = Sum( newData, numNewData );
PrintNewDataSummary( newData, totalNewData, numNewData );
SaveNewDataSummary( totalNewData, numNewData );
...
}
我确实同意第二种方法是更容易阅读和理解,并提供看起来更清晰的代码,至少从我自己的角度来看是这样。那么,我的问题是,第二种方法有什么缺点吗?例如,我能想到的一个可能的问题是与数据库的临时连接,例如:
void SummarizeData(...) {
...
externalDataStore.open();
externalDataStore.save(oldData, numOldData);
externalDataStore.save(newData, numNewData);
externalDataStore.close();
...
}
第一种方法将在一个打开/关闭周期中完成两个保存操作。但是,使用第二种方法...
void SummarizeData(...) {
...
externalDataStore.open();
externalDataStore.save(oldData, numOldData);
externalDataStore.close();
...
externalDataStore.open();
externalDataStore.save(newData, numNewData);
externalDataStore.close();
...
}
您必须为每个操作打开和关闭连接。这看起来很浪费,但我不知道它如何影响实践中的性能。
抱歉问了一个不必要的长问题......
In Code Complete, chapter 10, it is advised to group related statements, and the following example is given:
void SummarizeData(...) {
...
GetOldData( oldData, &numOldData );
GetNewData( newData, &numNewData );
totalOldData = Sum( oldData, numOldData );
totalNewData = Sum( newData, numNewData );
PrintOldDataSummary( oldData, totalOldData, numOldData );
PrintNewDataSummary( newData, totalNewData, numNewData );
SaveOldDataSummary( totalOldData, numOldData );
SaveNewDataSummary( totalNewData, numNewData );
...
}
It is stated that such grouping and concurrent processing is bad design, and instead gives something more separated:
void SummarizeData(...) {
GetOldData( oldData, &numOldData );
totalOldData = Sum( oldData, numOldData );
PrintOldDataSummary( oldData, totalOldData, numOldData );
SaveOldDataSummary( totalOldData, numOldData );
...
GetNewData( newData, &numNewData );
totalNewData = Sum( newData, numNewData );
PrintNewDataSummary( newData, totalNewData, numNewData );
SaveNewDataSummary( totalNewData, numNewData );
...
}
I do agree that the second approach is easier to read and to understand, and offers cleaner-looking code, at least from my own perspective. So, my question is, are there any disadvantages with the second approach? For example, one possible issue that I could think of is with temporary connections to databases and such:
void SummarizeData(...) {
...
externalDataStore.open();
externalDataStore.save(oldData, numOldData);
externalDataStore.save(newData, numNewData);
externalDataStore.close();
...
}
This first approach would complete both save operations in one open/close cycle. However, with the second approach...
void SummarizeData(...) {
...
externalDataStore.open();
externalDataStore.save(oldData, numOldData);
externalDataStore.close();
...
externalDataStore.open();
externalDataStore.save(newData, numNewData);
externalDataStore.close();
...
}
You have to open and close the connection for each operation. This seems wasteful, but I have no idea how it affects performance in practice.
Sorry for the unnecessarily long question...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有读到 Code Complete 中的第 10 章(应该再花几个晚上才能读到!),但我认为这里的要点是以逻辑且易于阅读的方式对代码行进行分组,没有影响程序功能。换句话说,尽可能地清理并重新排列它,但一旦它开始真正影响行为就停止。
在您的示例中,我们应该记住“过早优化是万恶之源”,但我认为我们仍然可以安全地假设,如果您要立即再次打开连接,则不应关闭连接,因为这两个动作实际上是相互抵消的。作为一般规则,为了简单起见,任何连接都应该仅在第一次需要它们之前打开,并在最后一次使用之后立即关闭。
I haven't gotten to Chapter 10 in Code Complete just yet (a few more evenings ought to do it!) but I think the main point here is to group your lines of code in a logical and easily readable way, without affecting program functionality. In other words, clean it up and rearrange it as much as possible, but stop as soon as it starts to actually affect behaviour.
In your example, we should keep in mind that "Premature optimization is the root of all evil," but I think we can still safely assume that you shouldn't be closing the connection if you're about to open it again right away again, since those two actions literally cancel each other out. As a general rule, any connections should be opened only right before the first time you need them, and closed immediately after the last time they're used, for simplicity's sake.
我很无聊,所以我尝试使用 Sqlite 在 Python 中进行概念验证速度测试(我意识到这不是最好的方法)。
首先,进行 50,000 次迭代的基础测试,每次迭代后打开和关闭连接。
现在,迭代了 50,000 次,但没有关闭连接。
结果?
因此,它会产生影响,尽管影响可以忽略不计。
现在你就得到了它。
不过,我绝对同意斯蒂芬所说的。
I was bored, so I tried doing a proof-of-concept speed test in Python using Sqlite (which I realize is not the best way to do it).
First, the base test of 50,000 iterations, opening and closing the connection after every iteration.
And now, 50,000 iterations, but without closing the connection.
The results?
So, it makes a difference, albeit a negligible one.
And there you have it.
Definitely agree with what Stephane said, though.