【发布时间】:2017-05-07 16:02:21
【问题描述】:
C#代码语法如下
public void Cancel()
{
// If reservation already started throw exception
if (DateTime.Now > From)
{
throw new InvalidOperationException("It's too late to cancel.");
}
//for gold customer IsCanceled= false
if (IsGoldCustomer() && LessThan(24))
{
IsCanceled = false;
}
//for not gold customer IsCanceled= true
if (!IsGoldCustomer() &&LessThan(48))
{
IsCanceled = true;
}
}
private bool IsGoldCustomer()
{
return Customer.LoyaltyPoints > 100;
}
private bool LessThan(int maxHours)
{
return (From - DateTime.Now).TotalHours < maxHours;
}
注释描述的业务逻辑,想结合 if (IsGoldCustomer() && LessThan(24)) 和 if (!IsGoldCustomer() &&LessThan(48)) 条件。有什么建议吗?
如果条件如下,修改了两者,但修改不符合我的要求。
//for gold customer IsCanceled= false
IsCanceled = !(IsGoldCustomer() && LessThan(24));
//for not gold customer IsCanceled= true
IsCanceled = !IsGoldCustomer() &&LessThan(48);
【问题讨论】:
-
看起来应该重构为返回布尔值的
CanCancel函数。Cancel函数可能不应该在非异常情况下失败。
标签: c# coding-style code-cleanup