Refactoring Recipes
Created by Michael Aram, last modified by Michael Aram 04 Jul 2017, at 05:31 PM
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}tolassignDeprecated code example
foreach {dog cat} $animalslist breakRecommended code example
lassign $animalslist dog catCommand line for finding/replacing code
todo -
Refactor multiple
lindexoperations tolassignDeprecated code example
set dog [lindex $animalslist 0]
set cat [lindex $animalslist 1]Recommended code example
lassign $animalslist dog catCommand line for finding/replacing code
todo -
Brace
exprexpressionsDeprecated code example
[expr $money - 1]Recommended code example
[expr {$money - 1}]Command line for finding/replacing code
todo -
Replace
string equalwitheqin 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
lsearchwithinorniin 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
evalwith{*}if possibleDeprecated code example
eval mycommand $argsRecommended code example
mycommand {*}$argsCommand 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::httpinstead ofutil_http*,ns_httpget,::http,::xo::HttpRequest
Substitute Deprecated Procedures
-
Replace
empty_string_pwitheq ""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
todoRationale
Byte-compiled comparisons are faster.
-
Replace
exists_and_not_nullwithinfo existsandneDeprecated 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
todoRationale
Byte-compiled comparisons are faster.
-
Replace
ad_parameterwithparameter::getDeprecated 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_permissionwithpermission::require_permissionDeprecated 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_unlistwithlassignDeprecated code example
util_unlist $animalslist dog catRecommended code example
lassign $animalslist dog catCommand line for finding/replacing code
todo