View · Index

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

  1. Refactor foreach {var1.. varN} $list {break} to lassign

    See http://wiki.tcl.tk/1530

    Deprecated code example

    foreach {dog cat} $animalslist break

    Recommended code example

    lassign $animalslist dog cat

    Command line for finding/replacing code

    todo

  2. Refactor multiple lindex operations to lassign

    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

  3. Brace expr expressions

    Deprecated code example

    [expr $money - 1]

    Recommended code example

    [expr {$money - 1}]

    Command line for finding/replacing code

    todo

  4. Replace string equal with eq in expressions

    Deprecated 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

  5. Replace lsearch with in or ni in expressions

    Deprecated 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

  6. Replace eval with {*} if possible

    Deprecated code example

    eval mycommand $args

    Recommended code example

    mycommand {*}$args

    Command line for finding/replacing code

    todo

Best Practices

  1. 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

  2. Use util::http instead of util_http*, ns_httpget, ::http, ::xo::HttpRequest

Substitute Deprecated Procedures

  1. Replace empty_string_p with eq ""

    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.

  2. Replace exists_and_not_null with info exists and ne

    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.

  3. Replace ad_parameter with parameter::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

  4. Replace ad_require_permission with permission::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'

  5. Replace util_unlist with lassign

    Deprecated code example

    util_unlist $animalslist dog cat

    Recommended code example

    lassign $animalslist dog cat

    Command line for finding/replacing code

    todo

previous November 2024
Sun Mon Tue Wed Thu Fri Sat
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Popular tags

17 , 5.10 , 5.10.0 , 5.10.1 , 5.9.0 , 5.9.1 , ad_form , ADP , ajax , aolserver , asynchronous , bgdelivery , bootstrap , bugtracker , CentOS , COMET , compatibility , CSP , CSRF , cvs , debian , docker , docker-compose , emacs , engineering-standards , exec , fedora , FreeBSD , guidelines , host-node-map
No registered users in community xowiki
in last 30 minutes
Contributors

OpenACS.org