什么是优雅的做法"如果没有上述"?

时间:2014-06-20 10:04:10

标签: if-statement switch-statement boolean-logic

我相信你一直在那里。你想说"如果flib这样做,如果flob这样做,如果flab做饮食,等等#34;其中任何一个都可以是真的,那么最后你想要一个"如果你没有做任何一个"。

例如(以下示例在Swift中,因为我一直在使用它,但我认为大多数语言的情况都是一样的):

let thing = 101
var isInteresting = false
if (thing % 3 == 0) {
    println("\"\(thing)\" is a multiple of three.")
    isInteresting = true
}
if (thing > 100) {
    println("\"\(thing)\" is greater than one hundred.")
    isInteresting = true
}
if (thing > 1000) {
    println("\"\(thing)\" is greater than one thousand.")
    isInteresting = true
}
if !isInteresting {
    println("\"\(thing)\" is boring.")
}

我发现跟踪一个布尔值告诉我是否做了什么或者没有笨拙。

我想出的唯一另一种方式是:

let thing = 101
let isAMultipleOfThree = (thing % 3 == 0)
let isGreaterThan100 = (thing > 100)
let isGreaterThan1000 = (thing > 1000)

if isAMultipleOfThree {
    println("\"\(thing)\" is a multiple of three.")
}
if isGreaterThan100 {
    println("\"\(thing)\" is greater than one hundred.")
}
if isGreaterThan1000 {
    println("\"\(thing)\" is greater than one thousand.")
}
if !(isAMultipleOfThree  || isGreaterThan100 || isGreaterThan1000 ) {
    println("\"\(thing)\" is boring.")
}

但如果更糟糕的话(如果你添加一个新条款,你需要记住在三个地方添加它。

所以我的问题是,是否有一种简洁明了的做法?

我梦想着一个虚构的开关式声明:

switchif {   //Would have fallthrough where every case condition is checked
case thing % 3 == 0:
    println("\"\(thing)\" is a multiple of three.")
case thing >100 :
    println("\"\(thing)\" is greater than one hundred.")
case thing > 1000:
    println("\"\(thing)\" is greater than one thousand.")
none:   //Unlike 'default' this would only occur if none of the above did
    println("\"\(thing)\" is boring.")
}

3 个答案:

答案 0 :(得分:1)

这是一个没有完美答案的好问题。但是,除了你建议的那些之外,还有另外一个想法:在一个过程中封装测试机器,以使调用代码至少更加简化。

具体来说,对于您的示例,调用代码可以是:

if (! doInterestingStuff(101)) {
  println("\"\(thing)\" is boring.");
}

如果将测试封装到程序中:

  public boolean doInterestingStuff(int thing) {
    var isInteresting = false

    if (thing % 3 == 0) {
      println("\"\(thing)\" is a multiple of three.")
      isInteresting = true
    }
    if (thing > 100) {
      println("\"\(thing)\" is greater than one hundred.")
      isInteresting = true
    }
    if (thing > 1000) {
      println("\"\(thing)\" is greater than one thousand.")
      isInteresting = true
    }

    return isInteresting
  }

答案 1 :(得分:1)

我不确定你是如何在Swift中这样做的,但由于你没有给出语言标签,我将用C ++回答。

关键是&&短路,第一部分为假时不会评估第二部分。它与您的布尔标志具有相同的想法,但它更加自动化。

struct Tracker
{
    Tracker() : any(false) { }
    bool operator()() { any = true; return true; }
    bool any;
};

int thing = 101;
Tracker tracker;
if (thing % 3 == 0 && tracker()) {
    printf("\"%d\" is a multiple of three.\n", thing);
}
if (thing > 100 && tracker()) {
    printf("\"%d\" is greater than one hundred.\n", thing);
}
if (thing > 1000 && tracker()) {
    printf("\"%d\" is greater than one thousand.\n", thing);
}
if (!tracker.any) {
    printf("\"%d\" is boring.\n", thing);
}

查看实际操作:http://ideone.com/6MQYY2

答案 2 :(得分:0)

kjhughes'答案给了我一点启发:

也许有人可以编写一个全局函数来接受一个不确定数量的键值对(或者甚至只是两个元素数组),其中键是一个比较,如果它是真的,则值是要运行的语句。如果没有运行则返回false,否则为true。

更新: 试了一下,太可怕了!

//Function:
func ifNone(ifNoneFunc:()->Void, tests: Bool...)
{
    var oneTestPassed = false
    for test in tests
    {
        oneTestPassed |= test
    }
    if(!oneTestPassed)
    {
        ifNoneFunc()
    }
}


//Example:
let thisThing = 7
ifNone(
    {
        println("\(thisThing) is boring")
    },
    {
        if(thisThing % 10 == 0)
        {
            println("\"\(thisThing)\" is a multiple of 10")
            return true
        }
        else
        {
            return false
        }
    }(),
    {
        if(thisThing % 3 == 0)
        {
            println("\"\(thisThing)\" is a multiple of 3")
            return true
        }
        else
        {
            return false
        }
    }(),
    {
        if(thisThing > 1_000_000)
        {
            println("\"\(thisThing)\" is over a million!!")
            return true
        }
        else
        {
            return false
        }
    }()
)