我非常喜欢简化代码以实现可扩展性。我试图简化这段代码,但我一直陷入困境。有谁知道我如何简化这段代码,因为它包含了很多丑陋的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";
}
答案 0 :(得分: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 :(得分: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和代码。 HasAuction
比AuctionId > 0
更清晰。
答案 2 :(得分: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";
}
如果你有一个昂贵的操作,你总是可以将它缓存在一个局部变量中,但这就是我要做的。
答案 3 :(得分: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
}
答案 4 :(得分: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;
}