2012-01-11 67 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; 
}