So ... we'll invest some time.
> On May 14, 4:41 pm, "MartinLemburg@UGS" <martin.lemburg.
@gmx.net>
> wrote:
> > now I have to maintain older tcl source and I found a lot of "if"
> > constructs with non-braced expressions, like this:
> > if [info exists $varName] {
> > I would write the "if" this way:
> > if {[info exists $varName]} {
> > Is there a need to go through the old sources and to change the non-
> > braced into braced if-expressions?
> The first argument of if, while, and expr, undergoes an extra
> substitution step.
> There's a good reason: noncommutative ops like && and ||, allowing to
> write
> if {[foo]||[bar]} {...}
> and making sure that only [foo] is executed if it returns true. If
> instead you write
> if [foo]||[bar] {...}
> then clearly both are executed before any boolean operation can take
> place: too bad if [bar] raises an exception in that case.
> A corollary is that if your legacy code contains
> if [f] {...}
> and the result of [f] contains [] or $, then the second substitution
> will be non-negligible.
> If this was the intended behavior, then of course you mustn't brace
> the argument.
> But in the example you gave, [info exists ...], the possible results 0
> and 1 are constants, so there's no risk.
> On the other hand, there's no strong need either. A filesystem hit
> like [info exists] is most likely much more expensive than the extra
> substitution and loss of bytecode compilation caused by the lack of
> braces.
> > If it is only about speed, runtime behaviour, than I don't think, that
> > I will invest time in this.
> If you only have [info exist]s, don't.
> If things are trickier, you might consider doing it automatically.
> It will be an interesting exercise demonstrating the introspective
> power of Tcl ! (hint: use [regexp] and [info complete])
> -Alex