A.9   Tcl Style

The following conventions define additional standards of programming style:

CORRECT:
                if {$id != 0} {
CORRECT:
                if {[expr $id] != 0} {
INCORRECT:
                if {[expr $id != 0]} {

CORRECT:
                incr index
CORRECT:
                incr index -4
INCORRECT:
                set index [expr $index + 1]

source [wtxPath host resource tcl]wtxcore.tcl 
set backenddir [wtxPath host [wtxHostType] lib backend]*

CORRECT:
                if [catch "dataFetch $list" result] { 
                        if {$result == "known problem"} { 
                            specialCaseHandle 
                        } else { 
                            error $result 
                        }
INCORRECT:
                catch "dataFetch $list" result

CORRECT:
                if {$id != 0} { 
                        ... 
                    } else { 
                        ... 
                    }
INCORRECT:
                if {$id !=0} then { 
                        ... 
                    } { 
                        ... 
                    }

The following illustrates a complex return value consisting of a description:

# Return a list of 11 items: vxTicks taskId status priority pc 
# sp errno timeout entry priNormal name 
 
return [concat [lrange $tiList 0 1] [lrange $tiList 3 end]]
The following illustrates and simple return value:

# This code checks whether the VxMP component is installed: 
 
if [catch "wtxSymFind -name smObjPoolMinusOne" result] { 
    if {[wtxErrorName $result] == "SYMTBL_SYMBOL_NOT_FOUND"} { 
        return -1       # VxMP is not installed 
    } else { 
        error $result 
    } 
} else { 
    return 0             # VxMP is installed 
}

if {$defaultTaskId == 0} { 
    error "No default task has been established." 
}
Because every error message and error code must be described in the procedure header in the ERRORS: block, it is sometimes useful to call error in order to replace an underlying error message with an error expressed in terms directly relevant to the current procedure. For example:

if [catch "wtxObjModuleLoad $periodModule" status] { 
    error "Cannot add period support module to Target ($status)" 
}