【发布时间】:2013-08-08 21:10:25
【问题描述】:
到目前为止,我将所有对事件做出反应的代码直接放入事件处理方法中。
昨天我在某个地方看到有人提到那里只应该放最少的代码。
真的吗 ?或者最佳做法是什么?
例如从程序流畅工作的角度来看,哪个示例更好,以及为什么,如果可以的话: 图一:
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
var DropPosX = e.X;
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
for (int i = 0; i < s.Length; i++)
{
CheckFile(s[i])
LoadFile(s[i]);
// ..big chunk of code..
}
// ..big chunk of code..
}
图2:
DoDragDrop(int[] s, int DropPosX)
{
for (int i = 0; i < s.Length; i++)
{
CheckFile(s[i])
LoadFile(s[i]);
// ..big chunk of code..
}
// ..big chunk of code..
}
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
var DropPosX = e.X;
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
DoDragDrop(s, DropPos);
}
..甚至
图3:
int DropPosX;
string[] s;
DoDragDrop()
{
for (int i = 0; i < s.Length; i++)
{
CheckFile(s[i])
LoadFile(s[i]);
// ...
}
// ...
}
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
DropPosX = e.X;
s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
DoDragDrop();
}
【问题讨论】:
-
我认为这个问题在code review stack exchange website 上会更好。
-
对不起,也许我应该说我不是在寻找特定的代码审查。该代码仅作为示例。 -- 如果你觉得更好,我可以把它改成更简单的伪代码?