What is an elegant way to do "if none of the above"?

I'm sure you were there. You want to say: "If the flib does this, if it does the flob, if flab does the diet, etc.", Where any number of them may be true, then at the end you want "if you have not done ANY of them."

For example (the examples below are given in Swift, since I played with it, but I think that the situation in most languages ​​is the same):

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.")
}

I find myself tracking a boolean to tell me if I did something or something ridiculous.

The only other way I came across was the following:

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.")
}

but if something is worse (if you add a new sentence, you need to remember it in three places.

So my question is: is there a neat, concise way to do this?

:

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.")
}
+4
3

, . , , : , , , .

, :

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

, Swift, , ++.

, && , , . , , .

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

+1

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
        }
    }()
)
0

Source: https://habr.com/ru/post/1545349/


All Articles