C#:简化代码

本文关键字:代码 | 更新日期: 2023-09-27 18:19:55

我非常喜欢简化代码以实现可扩展性。我曾试图简化这段代码,但一直被卡住。有人知道我如何简化这段代码吗?因为它包含了很多丑陋的if-if-if。过度复杂的原因是传递给该方法的变量数量。这些都是必需的。

感谢

private void SetVisibility(int AuctionID, int BidStatus, int LoginErrorCode, int IsHome, bool IsGetMoreTokens)
{
        this._dealBidPlacedControl.Visible = false;
        this._dealBidControl.Visible = false;
        this._loginReg.Visible = false;
        this._deals.Visible = false;            
        if (AuctionID > 0)
        {
            if (LoginErrorCode == 0)
            {
                if (BidStatus > 0)
                {
                    this._dealBidPlacedControl.Visible = true;
                }
                else
                {
                    this._dealBidControl.Visible = true;
                }
            }
            else
            {
                this._loginReg.LoginErrorType = LoginErrorCode;
                this._loginReg.Visible = true;
                this._deals.Visible = true;
            }
        }
        else
        {                
            this._loginReg.Visible = true;
            this._deals.Visible = true;                
        }
        if (IsGetMoreTokens)
        {
            this._getMoreTokens.Visible = true;
            this._loginReg.Visible = false;
            this._deals.Visible = false;     
        }
        // set the hidden field which can be used to set visibility if included in a tab control or anything
        // by the parent site
       if (this.AuctionID > 0 || BidStatus > 0 || IsHome > 0 || LoginErrorCode > 0 || IsGetMoreTokens)
            this._visibilityStatus.Value = "1";   
}

C#:简化代码

您可以使用布尔表达式,如下所示:

this._dealBidPlacedControl.Visible = AuctionId > 0 && LoginErrorCode == 0 && BidStatus > 0;
this._dealBidControl.Visible = AuctionId > 0 && LoginErrorCode == 0 && BidStatus <= 0;
this._loginReg.Visible = AuctionID > 0 && LoginErrorCode != 0;
this._deals.Visible = LoginErrorCode != 0 && !IsGetMoreTokens;

此外,您可以使用枚举或布尔值来代替id和代码。CCD_ 1比CCD_。

您可以尝试通过直接设置布尔值来简化代码,而不是创建if语句并分配true:

_dealBidPlacedControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0

示例:

bool hasAuction = AuctionID > 0;
bool hasLoginError = LoginErrorCode != null;
bool hasBidStatus = BidStatus > 0;
this._dealBidPlacedControl.Visible = hasAction && !hasLoginError && hasBidStatus;
this._dealBidControl.Visible = hasAction && !hasLoginError && !hasBidStatus;
this._loginReg.Visible = this._deals.Visible = !hasAction || hasLoginError;
if (hasAuction && hasLoginError)
{
  this._loginReg.LoginErrorType = LoginErrorCode;
}
if (IsGetMoreTokens)
{
  this._getMoreTokens.Visible = true;
  this._loginReg.Visible = false;
  this._deals.Visible = false;     
}
// set the hidden field which can be used to set visibility if included in a tab control or anything
// by the parent site
if (this.AuctionID > 0 || BidStatus > 0 || IsHome > 0 || LoginErrorCode > 0 || IsGetMoreTokens)
        this._visibilityStatus.Value = "1"; 

换一种方式怎么样?

private void SetVisibility(int AuctionID, int BidStatus, int LoginErrorCode, int     IsHome, bool IsGetMoreTokens)
{
    this._dealBidPlacedControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0;
    this._dealBidControl.Visible       = AuctionID > 0 && LoginErrorCode == 0 && BidStatus <= 0;
    this._loginReg.Visible             = AuctionID > 0 && LoginErrorCode != 0;
    this._deals.Visible                = AuctionID > 0 && LoginErrorCode != 0;
    etc.
}

你甚至可以把所有的参数框成一个辅助类:

class VisibilityParameters
{
  public int AuctionID;
  public int BidStatus;
  ...
}

以便您的方法在未来更容易扩展:

  private void SetVisibility( VisibilityParamteres parameters ) 
  {
  }

这使您有机会从方法体中提取布尔表达式:

private void SetVisibility( VisibilityParameters parameters )
{
    this._dealBidPlacedControl.Visible = DealBidPlaceVisible( parameters );
    etc.
}
private bool DealBidPlaceVisible( VisibilityParameters parameters )
{
     return parameters.AuctionID > 0 && parameters.LoginErrorCode == 0 && parameters.BidStatus > 0
}

就我个人而言,我喜欢将控制外观的代码移到更靠近控件的位置。避免使用大量gui逻辑扰乱代码,如以下所示:

private void SetVisibility(int AuctionID, int BidStatus, int LoginErrorCode, int     IsHome, bool IsGetMoreTokens)
{
    this._dealBidPlacedControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0;
    this._dealBidControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus <= 0;
    this._loginReg.LoginErrorType = LoginErrorCode;
    this._loginReg.Visible = !IsGetMoreTokens && LoginErrorCode != 0;
    this._deals.Visible = !IsGetMoreTokens && LoginErrorCode != 0;
    this._getMoreTokens.Visible = IsGetMoreTokens;
    // set the hidden field which can be used to set visibility if included in a tab control or anything
    // by the parent site
    if (this.AuctionID > 0 || BidStatus > 0 || IsHome > 0 || LoginErrorCode > 0 || IsGetMoreTokens)
        this._visibilityStatus.Value = "1";   
}

如果你有一个昂贵的操作,你可以总是把它缓存在一个本地变量中,但我就是这样做的

if (AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0)
{
    this._dealBidPlacedControl.Visible = true;
}
else if (AuctionID > 0 && LoginErrorCode == 0 && AuctionID <= 0)
{
    this._dealBidControl.Visible = true;
}
else if (AuctionID > 0 && LoginErrorCode != 0)
{
    this._loginReg.LoginErrorType = LoginErrorCode;
    this._loginReg.Visible = true;
    this._deals.Visible = true;
}
else if(AuctionID <= 0)
{
    this._loginReg.Visible = true;
    this._deals.Visible = true;
}