How can I refactor out the required else clause?

后端 未结 6 466
春和景丽
春和景丽 2021-01-12 09:13

I have a C# method that looks a bit like this:

bool Eval() {
  // do some work
  if (conditionA) {
     // do some work
     if (conditionB) {
       // do s         


        
6条回答
  •  情深已故
    2021-01-12 09:47

    Well, since 'do some work' is already imperative (presumably), then I think that

    let eval() =
        let mutable result = false
        ... // ifs
            result <- true
        ... // no more elses
        result
    

    is shorter and reasonable. (In other words, else is only mandatory for if expressions that return values; since you're doing imperative work, use if statements that don't need an else.)

提交回复
热议问题