Version 7 of then

Updated 2007-06-01 12:44:41 by wdb

Optional argument of if. It's usually good style to omit the then itself, but can aid clarity when using multi-line expressions. Compare with else.

 if {$some_long_and_complex_condition
     && $some_other_condition_making_it_even_longer
 } then {
      # do the right thing here
 } else {
      # do the other right thing here
 }

wdb In my humble opinion, clarity matters always, such that I personally count the usage of then to the list of good style. I've read the opinion above more than once, and I never could understand it's background (possibly the absence of keyword then in some other languages?)

But, of course, that's my personal taste.

Additionally, I suppose that usage of then does not impact the perfomance.

LV I agree with wdb that I find the presence of then a better style than leaving it implicit. However, I think that the point of the original poster was that in the case of writing an if on one line, that poster preferred not seeing the then:

 if {$a eq 17} {do stuff}

instead of

 if {$a eq 17} then {do stuff}

Perhaps the noise of the syntactical sugar is the problem for them.

DKF: You think correctly. I prefer:

 if {$a == 17} {
    do stuff
 }

to:

 if {$a == 17} then {
    do stuff
 }

but when the condition is long, this is best:

 if {
    ([info exists longNamed(variable)] &&
    [get some value from a command with many arguments] == $longNamed(variable)) ||
    [set code [catch {
       something which might fail
    } msg]] == 1
 } then {
    do stuff
 }

Code for clarity and you're on the right track!

wdb Thank you for pointing out the aspect of noise which I did not care. And which deserves a wiki page which I have spent a [ page ] intentionally left blank by me.


Category Command | Category Control Structure