在Swift中,使用链式的if-else语法更好。

huangapple go评论72阅读模式
英文:

What is better syntax with chain if else in swift

问题

我可以帮你翻译这段代码。以下是翻译后的代码:

func funcA() -> Bool {
    // funcA的实现在这里
    // 用实际的逻辑替换这里的代码
    return true // 用funcA的实际结果替换'true'
}

func funcB() -> Bool {
    // funcB的实现在这里
    // 用实际的逻辑替换这里的代码
    return false // 用funcB的实际结果替换'false'
}

func funcC() {
    // funcC的实现在这里
    // 用实际的逻辑替换这里的代码
    print("执行函数C")
}

func performLogic() {
    let resultA = funcA()
    
    if resultA {
        return // 如果resultA为true,则提前返回
    }
    
    let resultB = funcB()
    
    if !resultB {
        funcC()
    }
}

performLogic()

希望这可以帮到你!如果你有任何其他问题,请随时问我。

英文:

I would like to write a function that execute couple of subset function based on return value of subset function. The logic is
if funcA is successfully executed return
Otherwise executes funcB
if funcB is successfully executed return
Otherwise executes funcC
I have followed snippet but wondering if any better syntax in swift?

func funcA() -> Bool {
    // Implementation of funcA goes here
    // Replace this with your actual logic for funcA
    return true // Replace 'true' with the actual result of funcA
}

func funcB() -> Bool {
    // Implementation of funcB goes here
    // Replace this with your actual logic for funcB
    return false // Replace 'false' with the actual result of funcB
}

func funcC() {
    // Implementation of funcC goes here
    // Replace this with your actual logic for funcC
    print("Function C executed")
}

func performLogic() {
    let resultA = funcA()
    
    if resultA {
        return // Return early if resultA is true
    }
    
    let resultB = funcB()
    
    if !resultB {
        funcC()
    }
}

performLogic()

答案1

得分: 1

老实说,你当前的代码非常易读,我认为它不需要“更好”的语法。你可以使语法更简短,但这会降低可读性。

例如,你可以这样写(假设你不需要中间结果):

if !funcA() && !funcB() {
    funcC()
}

这利用了&&的短路特性。

如果你可以改变函数的返回类型,那么将funcC也改为返回Bool类型,你可以这样写:

funcA() || funcB() || funcC()

另外,你也可以将它们全部改为返回Void?类型。当函数执行失败时返回nil,否则返回()。这样你就可以使用??运算符,这是一种常见的习惯用法,用于在某些操作失败时提供一个“默认值”。

funcA() ?? funcB() ?? funcC()

不过,返回Void?看起来相当奇怪。如果这些函数实际上返回一些有意义的值,那么我建议按照这种方式进行修改。

英文:

To be honest, your current code is very readable and I don't think it needs a "better" syntax. You can make the syntax shorter, but that would make it less readable.

For example, you can do (assuming you don't need the intermediate results):

if !funcA() && !funcB() {
    funcC()
}

This takes advantage of the fact that && is lazy.

If you can change the return types of the functions, then changing funcC to also return a Bool allows you to do:

funcA() || funcB() || funcC()

Alternatively, change them all to return a Void?. Return nil when it is not successful, and return () otherwise. This allows you to use the ?? operator, which is a common idiom to provide a "default" when something fails

funcA() ?? funcB() ?? funcC()

That said, returning Void? looks quite weird. If the functions actually return some meaningful value, then I would recommend doing it this way.

答案2

得分: 0

我会尽量只在必要时使用局部作用域变量。这种方法可以减少复杂性并使代码更清晰。如果函数的命名有意义,可能根本不需要使用局部作用域变量。

在Swift中实现早期返回通常使用guard语句。

func performLogic() {
    guard !funcA() else { return }
    guard funcB() else { return }

    funcC()
}
英文:

I would use locally scoped variables only when really needed. This approach reduces complexity and can make the code cleaner. If the functions are named meaningfully, using locally scoped variables might not be necessary at all.

Implementing early returns in Swift, typically a guard is used.

func performLogic() {
    guard !funcA() else { return }
    guard funcB() else { return }

    funcC()
}

答案3

得分: 0

最好使用guard_1guard_2

func funcA() -> Bool {
    // funcA的实现在这里
    // 用funcA的实际逻辑替换这里
    return true // 用funcA的实际结果替换'true'
}

func funcB() -> Bool {
    // funcB的实现在这里
    // 用funcB的实际逻辑替换这里
    return false // 用funcB的实际结果替换'false'
}

func funcC() {
    // funcC的实现在这里
    // 用funcC的实际逻辑替换这里
    print("执行了函数C")
}

func performLogic() {
    guard funcA() else {
        return // 如果resultA为true,则提前返回
    }
    
    guard !funcB() else {
        funcC()
        return
    }
}

performLogic()
英文:

Better to use guard_1 or guard_2

func funcA() -> Bool {
    // Implementation of funcA goes here
    // Replace this with your actual logic for funcA
    return true // Replace 'true' with the actual result of funcA
}

func funcB() -> Bool {
    // Implementation of funcB goes here
    // Replace this with your actual logic for funcB
    return false // Replace 'false' with the actual result of funcB
}

func funcC() {
    // Implementation of funcC goes here
    // Replace this with your actual logic for funcC
    print("Function C executed")
}

func performLogic() {
    guard funcA() else {
        return // Return early if resultA is true
    }
    
    guard !funcB() else {
        funcC()
        return
    }
}

performLogic()

huangapple
  • 本文由 发表于 2023年7月27日 15:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定