这个 if-else-else 逻辑可以重写为更简洁的东西吗?

发布于 2024-09-15 10:50:43 字数 424 浏览 9 评论 0原文

我能把下面的逻辑写得更简单、更容易阅读吗?下面满足我的需要,但非常混乱:

if (IsChanged == true)
{
    return;
}

// Executed when the close (x) button is pressed,
// as the Status string is not yet set to a real value...
else if (Status == "" && IsChanged == false) 
{
    CancelClose();
}

// saving logic falls to here...
else if (IsChanged == false && Status == "saving") 
{
    IsChanged = false;
}

谢谢

Could I write the following logic in a simpler, more easy-to-read way? The below does what I need, but it is very messy:

if (IsChanged == true)
{
    return;
}

// Executed when the close (x) button is pressed,
// as the Status string is not yet set to a real value...
else if (Status == "" && IsChanged == false) 
{
    CancelClose();
}

// saving logic falls to here...
else if (IsChanged == false && Status == "saving") 
{
    IsChanged = false;
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

孤独难免 2024-09-22 10:50:43
if (isChanged) return;

switch (Status) {
   case "": 
       CancelClose(); 
       break;
   case "saving": 
       // IsChanged = false;
       break;
}

这已经是最简洁的了。请注意,因为如果 isChanged 为 true,则返回,所以您可以进一步始终假设 isChanged 为 false。

if (isChanged) return;

switch (Status) {
   case "": 
       CancelClose(); 
       break;
   case "saving": 
       // IsChanged = false;
       break;
}

This is about as neat as it gets. Note that because you return if isChanged is true you can further on always assume that isChanged is false.

岁吢 2024-09-22 10:50:43

这更清晰一些:

if (IsChanged)
{

}
else if (Status == "saving")
{

}
else if (Status == "")
{

}
else
{

}

我建议您使用 enum 来表示状态。这将使您的代码成为强类型的。

public enum Status
{
    Closing,
    Saving,
    Changed,
}

然后您可以使用一个很好的 switch 语句来决定要采取什么操作。

switch (_status)
{
    case Status.Saving:
        break;
    case Status.Closing:
        break;
    case Status.Changed:
        break;
    default:
        break;
}

This is a bit cleaner:

if (IsChanged)
{

}
else if (Status == "saving")
{

}
else if (Status == "")
{

}
else
{

}

I would recommend you use an enum to represent the status. This will allow your code to be strongly typed.

public enum Status
{
    Closing,
    Saving,
    Changed,
}

Then you can use a nice switch statement to decide what action to take.

switch (_status)
{
    case Status.Saving:
        break;
    case Status.Closing:
        break;
    case Status.Changed:
        break;
    default:
        break;
}
江湖正好 2024-09-22 10:50:43
if(IsChanged)
  return;

if(Status == "saving")
{
    // save      
}
else if(string.IsNullOrEmpty(Status))
{
    CancelClose();    
}
if(IsChanged)
  return;

if(Status == "saving")
{
    // save      
}
else if(string.IsNullOrEmpty(Status))
{
    CancelClose();    
}
∞琼窗梦回ˉ 2024-09-22 10:50:43

由于您返回 if IsChanged==true,因此在其他 if 中不需要它。

    if (IsChanged == true)
        return;

    switch (Status)
    { 
       case "":
        CancelClose();
        break;
       case "saving":
        break;
    }

Since you return if IsChanged==true, you don't need it in the other ifs.

    if (IsChanged == true)
        return;

    switch (Status)
    { 
       case "":
        CancelClose();
        break;
       case "saving":
        break;
    }
深居我梦 2024-09-22 10:50:43

是的:

   if (IsChanged) return;
   if (String.IsNullOrEmpty(Status)) CancelClose();

yes:

   if (IsChanged) return;
   if (String.IsNullOrEmpty(Status)) CancelClose();
倒带 2024-09-22 10:50:43
  • 将第一个 else if 剪切为 just if。如果 IsChanged 为真,则永远不会到达“else”。
  • 从其他 if 中删除 IsChanged==false 因为它们始终为 true。
  • 考虑使用枚举而不是字符串来表示您的状态。

我建议:

if (IsChanged)
{
      return;
}

if (CurrentStatus == Status.None) 
{
     CancelClose();
     return;
}

if (CurrentStatus == Status.Saving) 
{
  //     IsChanged = false;
}
  • cut the first else if to just if. If IsChanged is true the "else" will never be reached.
  • remove the IsChanged==false from your other ifs because they are always true.
  • Think about enums instead of strings for your status.

I'd recommend:

if (IsChanged)
{
      return;
}

if (CurrentStatus == Status.None) 
{
     CancelClose();
     return;
}

if (CurrentStatus == Status.Saving) 
{
  //     IsChanged = false;
}
长梦不多时 2024-09-22 10:50:43
    if(!IsChanged) {
        if (Status == "saving") // saving logic falls to here...
        {
            //     IsChanged = false;
        } 
        else if (Status == "") // Executed when the close (x) button is pressed, as the Status string is not yet set to a real value...
        {
            CancelClose();
        }
    } else {
        return;
    }
    if(!IsChanged) {
        if (Status == "saving") // saving logic falls to here...
        {
            //     IsChanged = false;
        } 
        else if (Status == "") // Executed when the close (x) button is pressed, as the Status string is not yet set to a real value...
        {
            CancelClose();
        }
    } else {
        return;
    }
抽个烟儿 2024-09-22 10:50:43

它可以简化为

    if (IsChanged)
    {
        return;
    }

    else if (Status == "") // Executed when the close (x) button is pressed, as the Status string is not yet set to a real value...
    {
        CancelClose();
    }

    else if (Status == "saving") // saving logic falls to here...
    {
        //     IsChanged = false;
    }

您不需要在第一次检查中使用 == True,因为它已经是 true 或 false。您不需要检查其他选择是否为假,因为如果它不正确,那么它一定是错误的。

It can be simplifed in to

    if (IsChanged)
    {
        return;
    }

    else if (Status == "") // Executed when the close (x) button is pressed, as the Status string is not yet set to a real value...
    {
        CancelClose();
    }

    else if (Status == "saving") // saving logic falls to here...
    {
        //     IsChanged = false;
    }

You do not need the == True in the first check, as it already is true or false. you do not need to check for false in the other choices because if it is not true it must be false.

娇妻 2024-09-22 10:50:43
if (IsChanged) return;

if (Status == "saving")
{
  //IsChanged = false;
}
else if (Status = "")
{
  CancelClose();
}

我会避免以大写字母开头变量名称。

if (IsChanged) return;

if (Status == "saving")
{
  //IsChanged = false;
}
else if (Status = "")
{
  CancelClose();
}

I'd avoid beginning your variable names with uppercase.

忆梦 2024-09-22 10:50:43
if (IsChanged) 
   return;

if (String.IsNullOrEmpty(Status)) // better use this unless you would like a
   CancelClose();                 // nullPointerException

else if (Status.equals("Saving"))
   // whatever you want for save
if (IsChanged) 
   return;

if (String.IsNullOrEmpty(Status)) // better use this unless you would like a
   CancelClose();                 // nullPointerException

else if (Status.equals("Saving"))
   // whatever you want for save
回首观望 2024-09-22 10:50:43

我不熟悉 c#,但 它支持条件运算符

condition ? first_expression : second_expression;

由于我不熟悉c#,我不会尝试重写你的代码,但无论如何,三元运算符可以在某些地方带来令人愉悦的简洁。

I'm not familiar with c#, but it supports the conditional operator

condition ? first_expression : second_expression;

Since I'm not familiar with c#, I won't try to re-write your code, but in any case, the ternary operator can lead to pleasing concision in some places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文