2012-01-11 92 views
1

我非常喜欢简化代码以实现可扩展性。我试图简化这段代码,但我一直在卡住。有谁知道我可以简化这个代码,因为它包含了很多丑陋的,如果如果。过度并发症的原因是传递给方法的变量数量。这些都是必需的。C#:简化代码

感谢

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"; 
} 
+4

听起来像你会对http://codereview.stackexchange.com/感兴趣http://codereview.stackexchange.com/ – 2012-01-11 10:00:16

回答

1

您可以尝试直接设置布尔值,而不是创建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"; 
+1

但是,这稍微改变了代码行为,因为原始代码具有不会为某些参数分配任何值的路径。 – Jan 2012-01-11 09:57:08

+1

@Jan在函数开始时,所有变量都设置为'false'。所以这应该没问题。 – s3rius 2012-01-11 10:02:05

+0

@Stefan:好的,忽略了这一点。比你的回答完全正确:) – Jan 2012-01-11 10:08:45

0

我个人喜欢移动控制t的代码他看起来更接近控制。 Avoding混乱的代码有很多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"; 
} 

如果你有一个昂贵的操作你总是可以缓存它在一个局部变量,但这是我会怎么做。

1

您可以使用布尔表达式,就像这样:

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和密码。 HasAuctionAuctionId > 0更清楚。

0

如何反其道而行?

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 
} 
0
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; 
}