Pitch: Multi-statement if/switch/do expressions

I suppose. But that just makes it worse, IMO, because fundamentally the goal is to evaluate to a concrete value, irrespective of any side-effects. So do is still the wrong verb; it's focusing on the side-effects rather than the primary purpose.

Also, to actually show the more elaborate (and horrible) example case:

let description =
    if x % 2 == 0 {
        print("x is even")
        do { "even" }
    } else {
        print("Huh, that's odd")
        do {
            if x == 0 {
                print("x is zero")
                do { "zero" }
            } else {
                print("x is non-zero")
                do { "non-zero" }
            }
        }
    }

To me that's quite a mess. It's bad enough with all the prints splattered around, but putting the actual expression elements inside do statements is extra confusing.

In contrast, flipping it to put the imperative side-effects inside do:

let description =
    if x % 2 == 0 {
        do { print("x is even") }
        "even"
    } else {
        do { print("Huh, that's odd") }
        if x == 0 {
            do { print("x is zero") }
             "zero"
        } else {
            do { print("x is non-zero") }
            "non-zero"
        }
    }

…doesn't solve the overall issue of noise, but does:

  1. Require fewer levels of indentation.
  2. Make diffs much cleaner because the expression lines aren't touched when adding and removing the debug prints.
  3. Mean that if you can mentally filter out the do blocks while you're reading this, you can read the unadulterated expression.
2 Likes