简体   繁体   English

具有启用/禁用按钮的C#性能和可读性

[英]C# performance and readability with buttons enabling/disabling

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... 编辑:两个初始示例不等效...

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. 在Windows Forms或WPF中,我不必担心。 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 我会做类似的事情

bool ButtonEnabled = (Something > 0);
btnOne.Enabled = ButtonEnabled;
btnTwo.Enabled = ButtonEnabled;
btnThree.Enabled = !ButtonEnabled;
btnFour.Enabled = !ButtonEnabled;

在这种情况下,您可能会发现两者之间的任何性能差异都将是微不足道的,因此,我将选择可读性最强的一个。

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: 与第二个代码等效的第一个代码的版本为:

if(Something > 0)
{
    btnOne.Enabled = true;
    btnTwo.Enabled = true;
    btnThree.Enabled = false;
    btnFour.Enabled = false;
}
else
{
    btnOne.Enabled = false;
    btnTwo.Enabled = false;
    btnThree.Enabled = true;
    btnFour.Enabled = true;
}

The version of the second code that is equivalent to the first would be: 与第一个代码等效的第二个代码的版本为:

bool ButtonEnabled = (Something > 0);

btnOne.Enabled = ButtonEnabled ? true : btnOne.Enabled;
btnTwo.Enabled = ButtonEnabled ? true : btnTwo.Enabled;
btnThree.Enabled = !ButtonEnabled ? false : btnThree.Enabled;
btnFour.Enabled = !ButtonEnabled ? false : btnFour.Enabled;

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. 因此,第一段代码显然比等效的替代方案更有效,更易读,而第二段代码比等效的替代方案更短且更易于维护。

Yep, unlike your application has one hundred thousand buttons displayed at the same time, concentrate HEAVILY on readability, not on micro-optimization ! 是的,不像你的应用程序在同一时间显示十万按钮,集中HEAVILY可读性,而不是微观优化! 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 ! UI层更新控件外观所需的时间将比“已启用”分配的时间长10.000倍!

Solution 2 is actually nearly what you will want to do when using data-binding (you were very near :p). 解决方案2实际上几乎是您在使用数据绑定时想要做的(您非常接近:p)。 Actually, you would code something more like: 实际上,您将编写类似于以下内容的代码:

public class MyClass {
    public bool IsSomethingTrue { get; set; } // with notification on property changed
    public bool IsSomethingFalse { get { return !IsSomethingTrue; } }

    private AMethod() {
        ...
        IsSomethingTrue = Something > 0;
        ...
    }

And your UI would be something like (WPF flavored): 您的UI类似于(WPF风格):

<Button IsEnabled={Binding IsSomethingTrue} /> <!-- btn 1 -->
<Button IsEnabled={Binding IsSomethingTrue} /> <!-- btn 2 -->
<Button IsEnabled={Binding IsSomethingFalse} /> <!-- btn 3 -->
<Button IsEnabled={Binding IsSomethingFalse} /> <!-- btn 4 -->
<!-- Want a 5th button ? just add it without changing your code-behind ! -->

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. 它适用于WPF,Qt,Java,我认为Winforms应该提供一些数据绑定功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM