如何停止编写链码?

发布于 2024-10-04 14:39:08 字数 295 浏览 3 评论 0原文

例如......

if ( /* Condition */ ) {

    if ( /* Condition */ ) {

        if ( /* Condition */ ) {

          // Superb!

        } else {

          // Error 3

        }

    } else {

      // Error 2

    }

} else {

  // Error 1

}

你知道如何避免这种情况吗?谢谢你!

for example...

if ( /* Condition */ ) {

    if ( /* Condition */ ) {

        if ( /* Condition */ ) {

          // Superb!

        } else {

          // Error 3

        }

    } else {

      // Error 2

    }

} else {

  // Error 1

}

Do you know how to avoid this? Thank you!

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

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

发布评论

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

评论(8

无语# 2024-10-11 14:39:09
if (!condition1)
    // Error 1 (and exit scope if necessary)

if (!condition2)
    // Error 2 (and exit scope if necessary)

if (!condition3)
    // Error 3 (and exit scope if necessary)

// Superb!
if (!condition1)
    // Error 1 (and exit scope if necessary)

if (!condition2)
    // Error 2 (and exit scope if necessary)

if (!condition3)
    // Error 3 (and exit scope if necessary)

// Superb!
生生漫 2024-10-11 14:39:09

是的;您可以使用 AND 运算符将 if 语句组合成单个复合语句,并在单个块中处理错误条件。不过,根据您的错误处理需求,您可能需要再次使用多个 if 来确保正确处理错误(这实际上取决于您的错误处理解决方案)。

也就是说:

if (CondA && CondB && CondC)
   {
   // Execute success
   }
else
   {
   if (!CondA)
      // Do error A
   else if (!CondB)
      // Do error B
   else if (!CondC)
     // Do error C
   }
}

Yes; you can compose together your if statements into a single compound statement using the AND operator, and handle the error conditions in a single block. Depending on your error handling needs, though, you may need to again have multiple ifs to make sure you're properly handling the error (it depends on your error handling resolution, really).

To wit:

if (CondA && CondB && CondC)
   {
   // Execute success
   }
else
   {
   if (!CondA)
      // Do error A
   else if (!CondB)
      // Do error B
   else if (!CondC)
     // Do error C
   }
}
留蓝 2024-10-11 14:39:09

有几种方法,最简单的就是简单地去掉一些函数并抽象出不同的层(如果你发现自己深入的话,无论如何都应该这样做。)

if ( /* Condition */ ) {
    value = aFunctionSaysWhat();
} else {
  // value = Error 1
}

....
value aFunctionSaysWhat(){
    if ( /* Condition */ ) {
         return aSecondFunctionHere();
    } else {
      // return Error 2
    }
}

基本前提是函数应该存在于一个抽象层上,如果可能,并做一件事。

下一个可能性是将其全部展平,这优于您最初的嵌套方法,但基本上具有相似的复杂性。如果您只有几个选项并且不打算添加更多选项,那么它可能比函数式方法干净得多。

if(ErrorCondition1){
   //Error 1
}else if(ErrorCondition2){
   //Error 2
}else if(ErrorCondition3){
   //Error 3
}else{
   //Superb
}

最后,您可以存储具有所需答案的哈希或映射,并完全删除 if ,实现此功能的能力取决于您对某些结果进行哈希处理的能力:

Results = {'Result1':'Error1', 'Result2':'Error2', 'Result3':'Error3', 'Success':'Superb'}

return Results[ConditionHash(Condition)];

There are a few ways, the simplest is to simply bust out some functions and abstract out the different layers (this should be done anyway if you find yourself going deep.)

if ( /* Condition */ ) {
    value = aFunctionSaysWhat();
} else {
  // value = Error 1
}

....
value aFunctionSaysWhat(){
    if ( /* Condition */ ) {
         return aSecondFunctionHere();
    } else {
      // return Error 2
    }
}

The basic premise is that a function should live on one layer of abstraction if possible, and do one thing.

The next possibility is to flatten it all out, this is superior to your initial nested method, but basically has similar complexity. It could be much cleaner than the functional approach if you only have a few options and you don't plan on adding more.

if(ErrorCondition1){
   //Error 1
}else if(ErrorCondition2){
   //Error 2
}else if(ErrorCondition3){
   //Error 3
}else{
   //Superb
}

Finally, you can store a hash or map with the desired answers and remove the if completely, the ability to implement this relies on your ability to hash some result:

Results = {'Result1':'Error1', 'Result2':'Error2', 'Result3':'Error3', 'Success':'Superb'}

return Results[ConditionHash(Condition)];
多彩岁月 2024-10-11 14:39:08

如果这是一个库函数,throw 可能是适当的操作。

if (!condition1) {
    throw "Condition 1 failed.";
}

if (!condition2) {
    throw "Condition 2 failed.";
}

if (!condition3) {
    throw "Condition 3 failed.";
}

// Superb!

其他可接受的操作可能是:

  • 返回 0nullundefined
  • 向用户显示错误并返回。

您必须确定哪种故障操作适合您的用例。

If this is a library function, throw may be the appropriate action.

if (!condition1) {
    throw "Condition 1 failed.";
}

if (!condition2) {
    throw "Condition 2 failed.";
}

if (!condition3) {
    throw "Condition 3 failed.";
}

// Superb!

Other acceptable actions might be:

  • Returning 0, null, or undefined.
  • Displaying an error to the user and returning.

You will have to determine which failure action is right for your use case.

稳稳的幸福 2024-10-11 14:39:08

看起来您有 3 个条件要检查和 4 个操作(3 个不同的错误 + 1 个成功)。不幸的是,在一般情况下,它需要 3 个条件检查和 4 个操作。我认为可以通过使用以下结构来清理代码

if (! /* condition 1 */ ) {
  // Error 1
} else if (! /* condition 2 */ ) { 
  // Error 2
} else if (! /* condition 3 */ ) { 
  // Error 3
} else {
  // superb
}

It looks like you have 3 conditions to check and 4 actions (3 different errors + 1 success). Unfortunately in the general case it's going to require 3 conditional checks and 4 actions. I think the code can be cleaned up a bit by using the following structure though

if (! /* condition 1 */ ) {
  // Error 1
} else if (! /* condition 2 */ ) { 
  // Error 2
} else if (! /* condition 3 */ ) { 
  // Error 3
} else {
  // superb
}
枕梦 2024-10-11 14:39:08

那么您可以使用异常、块内的中断或多个函数。通常,这需要反转您的条件以使代码以正确的方式排序。

do {
    if (! /* Condition 1 */ ) {
        // Error 1
        break;
    }

    if (! /* Condition 2 */ ) {
        // Error 2
        break;
    }

    if (! /* Condition 3 */ ) {
        // Error 3
        break;
    }

    // Superb!
} while (false);

do-while(false) 循环是一种创建块的方法,您可以在不允许匿名块的语言中打破该块。它可以很容易地成为一个函数并使用返回值,或者一个带有异常的 try-catch。

Well you can use exceptions, or breaks within a block, or multiple functions. Often this requires inverting your conditions to get the code ordered the right way.

do {
    if (! /* Condition 1 */ ) {
        // Error 1
        break;
    }

    if (! /* Condition 2 */ ) {
        // Error 2
        break;
    }

    if (! /* Condition 3 */ ) {
        // Error 3
        break;
    }

    // Superb!
} while (false);

The do-while(false) loop is a way of making a block that you can break out of in languages that will not tolerate an anonymous block. It could just as easily be a function and use returns, or a try-catch with exceptions.

写给空气的情书 2024-10-11 14:39:08

你会更喜欢这个吗?

if ( /* Condition 1*/ && /* Condition 2*/ && /* Condition 3 */) {
  // Superb!
}
else if (! /* Condition 1*/){
  // Error 1
}
else if (! /* Condition 2*/){
  // Error 2
}
else if (! /* Condition 3*/){
  // Error 3
}

Would you prefer this?

if ( /* Condition 1*/ && /* Condition 2*/ && /* Condition 3 */) {
  // Superb!
}
else if (! /* Condition 1*/){
  // Error 1
}
else if (! /* Condition 2*/){
  // Error 2
}
else if (! /* Condition 3*/){
  // Error 3
}
你列表最软的妹 2024-10-11 14:39:08
if ( ! /* Condition */ ) {
Error 1
throw someSortOfException
}

if (! condition 2){
Error 2
throw someSortOfOtherException
}

if (! condition 3){
Error 3
throw another Exception
}

// Success!

根据情况,您的代码可能是首选。例如,您可能希望在某个地方捕获这些异常。

if ( ! /* Condition */ ) {
Error 1
throw someSortOfException
}

if (! condition 2){
Error 2
throw someSortOfOtherException
}

if (! condition 3){
Error 3
throw another Exception
}

// Success!

Depending on the situation, your code may be prefered. You'll probably want to catch those exceptions somewhere for example.

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