如何停止编写链码?
例如......
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的;您可以使用 AND 运算符将
if
语句组合成单个复合语句,并在单个块中处理错误条件。不过,根据您的错误处理需求,您可能需要再次使用多个if
来确保正确处理错误(这实际上取决于您的错误处理解决方案)。也就是说:
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 multipleif
s to make sure you're properly handling the error (it depends on your error handling resolution, really).To wit:
有几种方法,最简单的就是简单地去掉一些函数并抽象出不同的层(如果你发现自己深入的话,无论如何都应该这样做。)
基本前提是函数应该存在于一个抽象层上,如果可能,并做一件事。
下一个可能性是将其全部展平,这优于您最初的嵌套方法,但基本上具有相似的复杂性。如果您只有几个选项并且不打算添加更多选项,那么它可能比函数式方法干净得多。
最后,您可以存储具有所需答案的哈希或映射,并完全删除 if ,实现此功能的能力取决于您对某些结果进行哈希处理的能力:
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.)
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.
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:
如果这是一个库函数,
throw
可能是适当的操作。其他可接受的操作可能是:
0
、null
或undefined
。您必须确定哪种故障操作适合您的用例。
If this is a library function,
throw
may be the appropriate action.Other acceptable actions might be:
0
,null
, orundefined
.You will have to determine which failure action is right for your use case.
看起来您有 3 个条件要检查和 4 个操作(3 个不同的错误 + 1 个成功)。不幸的是,在一般情况下,它需要 3 个条件检查和 4 个操作。我认为可以通过使用以下结构来清理代码
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
那么您可以使用异常、块内的中断或多个函数。通常,这需要反转您的条件以使代码以正确的方式排序。
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.
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.
你会更喜欢这个吗?
Would you prefer this?
根据情况,您的代码可能是首选。例如,您可能希望在某个地方捕获这些异常。
Depending on the situation, your code may be prefered. You'll probably want to catch those exceptions somewhere for example.