Forum OpenACS Development: But, what is truth?

Collapse
13: But, what is truth? (response to 1)
Posted by Alfred Werner on
% if 0 {puts true}
% if {0} {puts true}
% if true {puts true}
true
% if 1 {puts true}
true
% if {1} {puts true}
true
% if {true} {puts true}
true
% if {"true"} {puts true}
true
% set blah true
true
% if {$blah} { puts true}
true
% if {t} {puts true}
true
% if {yes} {puts true}
true

% if {randomstring} {puts true}
syntax error in expression "randomstring": variable references require preceding $
% set blah randomstring
randomstring
% if $blah {puts true}
syntax error in expression "randomstring": variable references require preceding $
% if {$blah} {puts true}
expected boolean value but got "randomstring"

% set blah -1.2
-1.2
% set blah -4323.937
-4323.937
% if $blah {puts true}
true
%  if {$blah} { puts true}
true

TCL is surprisingly generous in its ability to handle TRUTH. 1, t, true, and yes are all true as well as any number value that is non-zero. To be truly false you have to be equal to 0, f, false, no.

% set blah no
no
% if {$blah} { puts true}
%
% set blah false
false
% if {$blah} { puts true}
%
% if {!$blah} {puts true}
true

So, I'm not sure when all these versions of the truth showed up, but constructs like:

if {[string compare $html_p "t"] == 0} can be changed to
if {$html_p}
in /packages/ecommerce/www/admin/tools/spell.tcl
AND
if { [string compare $unique_users_p "t"] == 0 }
if {$unique_users_p}
in /packages/simple-survey/www/admin/responses.tcl

Ecommerce seems the most heavily 'protected' as far as using [string] and invoking == 0 against the results.

In most cases, people use 1 and 0 to store true and false, and then compare the result with == 0 anyway. In some cases, they actually store the result backwards (0 as true) but they are saved by the == 0.

find . -name *.tcl | xargs grep "&&"
find . -name *.tcl | xargs grep "||"
find . -name *.tcl | xargs grep "==0"
find . -name *.tcl | xargs grep "== 0"

Will find the majority of candidates.

A conversion to using boolean names - including in package configuration parameters, would lead to much more readable code - I'm willing to bet a lot of people often forget which is true and false - 1 and 0, which is why so many == 0 instances in the code. Strongly recommending 'true' and 'false' (or pick one of the three pairs) will help clean things up.

Ultimately [anything] == 0 should only be used in if statements when people are actually comparing numbers.

To that end, we should discourage the practice of _p variable names if people are actually storing counts.

Ultimately {[string compare $a $b] == 0} should be globally replaced with {$a eq $b}.

That's enough fun for a rainy Sunday afternoon :)