具有启用/禁用按钮的 C# 性能和可读性
我经常发现自己面对这样的代码:
if(Something > 0)
{
btnOne.Enabled = true;
btnTwo.Enabled = true;
btnThree.Enabled = false:
}
else
{
btnOne.Enabled = false;
btnTwo.Enabled = false;
btnThree.Enabled = true:
}
而且我一直想知道是不是这样更好,或者这样说:
bool ButtonEnabled = (Something > 0);
btnOne.Enabled = ButtonEnabled;
btnTwo.Enabled = ButtonEnabled;
btnThree.Enabled = !ButtonEnabled;
意识到这个问题有点争论,让我们把“可读性”放在一边“因素并专注于性能因素......什么是最好的?又一个任务还是一个条件?
预先感谢您的建议(或者更好的编写方式)!
编辑:更正了我的第二个片段中的错误。 编辑:两个最初的例子并不等同......
I've often found myself in front of that kind of code :
if(Something > 0)
{
btnOne.Enabled = true;
btnTwo.Enabled = true;
btnThree.Enabled = false:
}
else
{
btnOne.Enabled = false;
btnTwo.Enabled = false;
btnThree.Enabled = true:
}
And I've always wondered if it was better to let it like that, or put it like this :
bool ButtonEnabled = (Something > 0);
btnOne.Enabled = ButtonEnabled;
btnTwo.Enabled = ButtonEnabled;
btnThree.Enabled = !ButtonEnabled;
Realizing the question is a bit argumentative, let's put aside the "readability" factor and concentrate on the performance factor... What would be best ? One more assignation or a condition ?
Thanks in advance for your advices (or a even better way to write it) !
Edit : Corrected an error in my second snippet.
Edit : The two initial examples weren't equivalent...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法比较这两段代码,无论是在可读性还是性能上,因为它们给出了不同的结果。
第一个代码与第二个代码等效的版本为:
第二个代码与第一个代码等效的版本为:
因此,第一段代码显然比其等效替代方案更高效、更易读,并且第二段代码比它的等效替代方案更短并且更容易维护。
You can't compare the two pieces of code, neither on readability nor on performance, as they give different results.
The version of the first code that is equivalent to the second would be:
The version of the second code that is equivalent to the first would be:
So, the first piece of code is clearly more efficient and readable than it's equivalent alternative, and the second piece of code is shorter and somewhat easier to maintain than it's equivalent alternative.
这取决于被调用的属性。如您所知,财产可以做任何事情。在 Windows 窗体或 WPF 中,我不会担心这个问题。为了正确性和可读性,我主张后一种风格。如果您每次都设置所有必要的变量,那么遗漏某些内容并使某个按钮处于无效状态的可能性就会较小。
我会做类似的事情
That depends on the properties being called. As you know, a property can do any amount if things. In Windows Forms or WPF, I wouldn't worry about it. I'd argue for the latter style for correctness and readability. If you set all necessary variables every time, there is less chance of missing something and leaving one button in an invalid state.
I'd do something like
在这种情况下,无论您在两者之间看到什么性能差异,很可能都是微不足道的,因此我会选择最具可读性的一个。
Whatever performance difference you may see between the two will most likely be insignificant in this case, so I would pick the one that is most readable.
是的,与您的应用程序同时显示十万个按钮不同,
高度关注
可读性,而不是微观优化!无论如何,UI 层更新控件视觉效果所需的时间将比“启用”分配长 10.000 倍!解决方案 2 实际上几乎就是您在使用数据绑定时想要做的事情(您已经非常接近了:p)。实际上,您可以编写类似于以下内容的代码:
您的 UI 将类似于(WPF 风格):
这种模式允许您根据需要添加任意数量的按钮,而无需每次都更改方法。当方法往往非常复杂时,这尤其有用,并且提高了可读性。
它适用于 WPF、Qt、Java,我认为 Winforms 应该提供一些数据绑定功能。
Yep, unlike your application has one hundred thousand buttons displayed at the same time, concentrate
HEAVILY
on readability, not on micro-optimization ! The time it will take the UI layer to update the visual of your control will be 10.000 times longer than your "Enabled" assignments anyway !Solution 2 is actually nearly what you will want to do when using data-binding (you were very near :p). Actually, you would code something more like:
And your UI would be something like (WPF flavored):
This pattern allows you to add as many buttons as you want without changing your methods each time. This is especially helpful when methods tends to be quite complicated, and it improves the readability.
It works for WPF, Qt, Java, and I think Winforms should provide some data-binding capability.