Page 1 of 1
Depreciated or not depreciated?
Posted: Fri Jan 25, 2008 9:56 am
by Alchera
Just a question (after a meaningful discussion) based on a friend's faith in Komodo (Windows syntax thingie).
Anyway.... according to Komodo the following is "depreciated":
Code: Select all
if {$vbl == 1} {
puts "vbl is one"
} {
puts "vbl is not one"
}
I personally prefer the above to the following (according to Komodo) current (non-depreciated code):
Code: Select all
if {$vbl == 1} {
puts "vbl is one"
} else {
puts "vbl is not one"
}
The purpose of this post is to determine if the first example is indeed depreciated (as the Komodo user believes) as I cannot find one piece of documentation actually stating this.
Any input is (as always) gratefully accepted.
Posted: Fri Jan 25, 2008 10:51 am
by DragnLord
I have seen nothing that indicates the code is depreciated. As long as it works (with it does), I wouldn't worry about it.
Posted: Fri Jan 25, 2008 11:09 am
by nml375
If dropping "else" is deprecated, wouldn't the dropping of "then" also be deprecated?
Sofar, I have not read anything suggesting either being deprecated, with perhaps the exception of multiple-cases coding:
Code: Select all
if {$vbl == 1} then {
puts "vbl is one"
} elseif {$vbl == 2} then {
puts "vbl is two"
} else {
puts "vbl is other than one or two"
}
Here you can drop both "then" and "else" (but not "elseif"), but not the "elseif". Still not quite as saying dropping either is deprecated.
Also, this qoute from the 8.5.0 if manpage:
Remember, expressions can be multi-line, but in that case it can be a good idea to use the optional then keyword for clarity:
Code: Select all
if {
$vbl == 1 || $vbl == 2 || $vbl == 3
} then {
puts "vbl is one, two or three"
}
Would suggest "then" and "else" are mainly intended to make the code more readable, and has no "syntax function"
Posted: Sat Jan 26, 2008 10:12 am
by Alchera
Thank you both for that information; very much appreciated.
I was up until the wee small hours trying to find any reference to this and all to no avail.
I am wary of any (authoritative) statement emanating from a Windows software company especially when it refers to an Open Source project (that I suspect they'd like to make closed source if they could).
Posted: Sun Jan 27, 2008 4:53 am
by De Kus
The tcl 8.5 docu doesn't prefer either of the 2 possibilities. To quote the documentation:
The then and else arguments are optional “noise words” to make the command easier to read.
And I'd take that literally. They are meant to make code easier to read, that means either always use them or never. ^-^
Code: Select all
if {
$vbl == 1 || $vbl == 2 || $vbl == 3
} then {
puts "vbl is one, two or three"
}
Is imho a really really bad idea, because the eye catchs
Code: Select all
if {} {
$vbl == 1 || $vbl == 2 || $vbl == 3
} else {
puts "vbl is one, two or three"
}
which isn't meant at all. I'd suggest to write something like the following:
Code: Select all
if {$vbl == 1 || $vbl == 2
|| $vbl == 3} {
puts "vbl is one, two or three"
}
I split the line, because I wanted to start the 2nd line of the condition with a || or && to don't make the impression of just wrong indention. This follows a often used rule to use the double indention for multiple line for a single command.
PS: I personally would always use else, unless you want to write some quick code which just works but doesn't have to be maintained.