This section defines the Wind River Systems standard for all C and Tcl code and for the accompanying documentation included in source code. The conventions are intended, in part, to encourage higher quality code; every source module is required to have certain essential documentation, and the code and documentation is required to be in a format that has been found to be readable and accessible.
The conventions are also intended to provide a level of uniformity in the code produced by different programmers. Uniformity allows programmers to work on code written by others with less overhead in adjusting to stylistic differences. Also it allows automated processing of the source; tools can be written to generate reference entries, module summaries, change reports, and so on.
These conventions are divided into the following categories:
A module is any unit of code that resides in a single Tcl file. The conventions in this section define the standard module heading that must come at the beginning of every Tcl module following the standard file heading. The module heading consists of the blocks described below; the blocks are separated by one or more blank lines.
After the modification history and before the first function or executable code of the module, the following sections are included in the following order, if appropriate:
# Browser.tcl - Browser Tcl implementation file # # Copyright 1994-1995 Wind River Systems, Inc. # # modification history # -------------------- # 02b,30oct95,jco added About menu and source browser.tcl in .wind. # 02a,02sep95,pad fixed communications loss with license daemon (SPR #1234). # 01c,05mar95,jcf upgraded spy dialog # 01b,08feb95,p_m take care of loadFlags in wtxObjModuleInfoGet. # 01a,06dec94,c_s written. # # DESCRIPTION # This module is the Tcl code for the browser. It creates the main window and # initializes the objects in it, such as the task list and memory charts. # # RESOURCE FILES # wpwr/host/resource/tcl/shelbrws.tcl # wpwr/host/resource/tcl/app-config/Browser/*.tcl # ... #*/ # globals set browserUpdate 0 ;# no auto update by default
The following conventions define the standard layout for every procedure in a module.
Each procedure is preceded by the procedure documentation, a series of Tcl comments that includes the following blocks. The documentation contains no blank lines, but each block is delimited with a line containing a single pound symbol (#) in the first column.
RETURNS: A list of 11 items: vxTicks taskId status priority pc sp errno timeout entry priNormal name
RETURNS: N/A
ERRORS: "Cannot find symbol in symbol table"
ERRORS: N/A
The procedure documentation ends with an empty Tcl comment starting in column one.
The procedure declaration follows the procedure heading and is separated from the documentation block by a single blank line. The format of the procedure and parameter declarations is shown in VxWorks Programmer's Guide: Coding Conventions.
##################################################################### # # browse - browse an object, given its ID # # This routine is bound to the "Show" button, and is invoked when # that button is clicked. If the argument (the contents of... # # SYNOPSIS # .tS # browse [objAddr | symbol | &symbol] # .tE # # PARAMETERS # .IP <objAddr> # the address of an object to browse # .IP <symbol> # a symbolic address whose contents is the address of # an object to browse # .IP <&symbol> # a symbolic address that is the address of an object to browse # # RETURNS: N/A # # ERRORS: N/A #
Tcl allows code that is not in a procedure. This code is interpreted immediately when the file is read by the Tcl interpreter. Aside from the global-variable initialization done in the globals block near the top of the file, collect all such material at the bottom of the file.
However, it improves clarity--when possible--to collect any initialization code in an initialization procedure, leaving only a single call to that procedure at the bottom of the file. This is especially true for dialog creation and initialization, and more generally for all commands related to graphic objects.
Tcl code outside procedures must also have a documentation heading, including the following blocks:
##################################################################### # 01Spy.tcl - Initialization code # # This code is executed when the file is sourced. It executes the module # entry routine which does all the necessary initialization to get a # runnable spy utility. # # Call the entry point for the module spyInit
Include only one declaration per line. Declarations are indented in accordance with Indentation, and begin at the current indentation level. The remainder of this section describes the declaration formats for variables and procedures.
For global variables, the Tcl set command appears first on the line, separated from the identifier by a tab character. Complete the declaration with a meaningful comment at the end of the same line. Variables, values, and comments should be aligned, as in the following example:
set rootMemNBytes 0 ;# memory for TCB and root stack set rootTaskId 0 ;# root task ID set symSortByName 1 ;# boolean for alphabetical sort
The procedure name and list of parameters appear on the first line, followed by the opening curly brace. The declarations of global variables used inside the procedure begin on the next line, one on each separate line. The rest of the procedure code begins after a blank line. For example:
proc lstFind {list node} { global firstNode global lastNode ... }
for {set i 0} {$i < 10} {incr i 3} {
if {$i > $count} { set i $count }
set status [fooGet $foo [expr $i + 3] $value] if {&value & &mask} {
set a [expr ($b + $c) * \ ($d + $e)] set status [fooList $foo $a $b $c \ $d $e] if {($a == $b) && \ ($c == $d)} { ... }
while { condition }{ statements } foreach i $elem { statements }
if { condition } { statements } else { statements }
if { condition } { statements } elseif { condition } { statements } else { statements }
switch [flags] value { a { statements } b { statements } default { statements } }
switch [flags] value { a {set x $aVar} b {set x $bVar} c {set x $cVar} }
# This is the correct format for a single-line comment set foo 0
# This is the CORRECT format for a multiline comment # in a section of code. set foo 0 # This is the INCORRECT format for a multiline comment \ in a section of code. set foo 0
set day night ;# This is a global variable
The following conventions define the standards for naming modules, routines and variables. The purpose of these conventions is uniformity and readability of code.
if {$id != 0} {
if {[expr $id] != 0} {
if {[expr $id != 0]} {
incr index
incr index -4
set index [expr $index + 1]
source [wtxPath host resource tcl]wtxcore.tcl set backenddir [wtxPath host [wtxHostType] lib backend]*
if [catch "dataFetch $list" result] { if {$result == "known problem"} { specialCaseHandle } else { error $result }
catch "dataFetch $list" result
if {$id != 0} { ... } else { ... }
if {$id !=0} then { ... } { ... }
# 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]]
# 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." }
if [catch "wtxObjModuleLoad $periodModule" status] { error "Cannot add period support module to Target ($status)" }