Refactoring Recipes
The intention of this page is to collect small refactoring snippets that one can/should apply to an existing OpenACS installation in order to improve and modernize its code base. Most of the recipes have already been applied to the official core packages.
Modernize Tcl
-
Refactor
foreach {var1.. varN} $list {break}
tolassign
Deprecated code example
foreach {dog cat} $animalslist break
Recommended code example
lassign $animalslist dog cat
Command line for finding/replacing code
todo
-
Refactor multiple
lindex
operations tolassign
Deprecated code example
set dog [lindex $animalslist 0]
set cat [lindex $animalslist 1]Recommended code example
lassign $animalslist dog cat
Command line for finding/replacing code
todo
-
Brace
expr
expressionsDeprecated code example
[expr $money - 1]
Recommended code example
[expr {$money - 1}]
Command line for finding/replacing code
todo
-
Replace
string equal
witheq
in expressionsDeprecated code example
if {[string equal "" $dog]} {error "I want a dog!"}
Recommended code example
if {$dog eq ""} {error "I want a dog!"}
Command line for finding/replacing code
todo
-
Replace
lsearch
within
orni
in expressionsDeprecated code example
if {[lsearch -exact $animalslist $dog] != -1 } {error "I dont want a dog!"}
Recommended code example
if {$dog in $animalslist} {error "I dont want a dog!"}
Command line for finding/replacing code
todo
-
Replace
eval
with{*}
if possibleDeprecated code example
eval mycommand $args
Recommended code example
mycommand {*}$args
Command line for finding/replacing code
todo
Best Practices
-
Use bind variables in SQL statements
Deprecated code example
db_string check "SELECT * FROM animals WHERE color = $color;"
Recommended code example
db_string check "SELECT * FROM animals WHERE color = :color;"
Command line for finding/replacing code
todo
-
Use
util::http
instead ofutil_http*
,ns_httpget
,::http
,::xo::HttpRequest
Substitute Deprecated Procedures
-
Replace
empty_string_p
witheq ""
Deprecated code example
if {[empty_string_p $dog]} {error "I want a dog!"}
if {![empty_string_p $cat]} {error "I dont want a cat!"}Recommended code example
if {$dog eq ""} {error "I want a dog!"}
if {$cat ne ""} {error "I dont want a cat!"}Command line for finding/replacing code
todo
Rationale
Byte-compiled comparisons are faster.
-
Replace
exists_and_not_null
withinfo exists
andne
Deprecated code example
if {[exists_and_not_null cat]} {error "I dont want a cat!"}
Recommended code example
if {[info exists cat] && $cat ne "" } {error "I dont want a cat!"}
Command line for finding/replacing code
todo
Rationale
Byte-compiled comparisons are faster.
-
Replace
ad_parameter
withparameter::get
Deprecated code example
ad_parameter -package_id 123 SystemURL ""
Recommended code example
parameter::get -package_id 123 SystemURL -default ""
Command line for finding/replacing code
todo
-
Replace
ad_require_permission
withpermission::require_permission
Deprecated code example
ad_require_permission $oid "read"
Recommended code example
permission::require_permission -object_id $oid -privilege "read"
Command line for finding/replacing code
fgrep -rl "ad_require_permission" packages/ | xargs sed -i.sedbak -r 's/ad_require_permission\s+([^\s]*)\s+([^\s]*)/permission::require_permission -object_id \1 -privilege \2/g'
-
Replace
util_unlist
withlassign
Deprecated code example
util_unlist $animalslist dog cat
Recommended code example
lassign $animalslist dog cat
Command line for finding/replacing code
todo