The code formatting style used for Tcl programs as reflected on the man pages and elsewhere is similar to C Code formatting. I propose an alternative, more compact style: CCFS. Not because i want it to be 'the' standard, but rather to share the idea. After reading (once again) '1% the code' [http://www.colorforth.com/1percent.html] and while writing (once again) a medium size Tcl application i started to think, that there are to many braces in the files. Python [http://www.python.org/] does without them, however Python lacks nice Syntax at other points, I won't discuss this here though. CCFS rules: * no lonly braces on a single line, except: * when a namespace, or a proc larger then 24 lines (buh) ends, or * for delimiting data * braces in if {expr} only when it is an expresion * braces around arguments only if there are more then one Some recomendations: * rather return, continue, break then else * don't while, foreach, for, if you can avoid it - recurse Illustrating with an example: proc ccfs param { if !$param return puts "we are in" if [llength $param] { puts "and we've got a list"} switch -- [lindex $param] { stop {return} continue { # test the second parameter set test [lindex $param 1]} default {puts "don't know what todo with: $param}}} if {$test eq "stop"} { puts "we stopped on the second param"}} proc ccfs1 {varA varB} {...} Observations: * If a line is empty, it clearly denotes that something new is going to happen * Of course I use a syntax aware editor to get the intending and the parent matching right * Conditional are: either flags - $var, results - [script], or expressions - {expr}, which is visually conveyed by the CCFS * Some say ([A question of style]) that run time is slightly longer, i don't care! (If you need it fast, postprocess the code to insert the braces). See also [while is bad] for a discussion about bracing expresions. Tricky decisions: If an if/else branch is to long to fit into the same line, you have to decide upon formatting. I give some examples with possible nice solutions. Remember always: avoid else, rather break, continue or return: if ..short cond.. {short then} else {short else} if ..long ........... cond {short then} if ..longer ........... cond } short then} if { ..very long cond.. ..over several lines.. } { ..then.. if ..cond.. { short cond} else {short else} if ..cond.. { ..then.. } \ else {short else} if ..cond.. { ..then.. } \ else { ..else..} Mhm.. if/then/else is bad, however, sometimes we need it, if you prefer it more traditional use: if ..cond.. { ..then.. ... } else { ..else.. ...} I really think it looks ok and compact. Or be somewhat more funky: if ..cond.. { ..then.. ... } { ..else.. ...} The idiom '} {' meaning "else" from now on. '''Loops''' 'for' and 'while' use expresion for iteration, however for reasons explained elsewhere you '''must''' enclose the expresion within braces, which is ugly. Use the intrinsic list processing of the Tcl 'proc' instead. The commandline parser of [TTP] is an example for it. Iff: * the iteration is not very deep * does not get called all the time the notational and expressive elegance of recursion is almost everytime better then looping (says i, but .. who am i to say this?). 'foreach' is an exception, especially if you iterate over more then one variable. Ugly example: proc printlist args { while {[llength $args]} { puts [lindex $args 0] set args [lreplace $args 0 0]}} Good looking: proc printlist {item args} { puts $item if [llength $args] {eval printlist $args}} Of course this is a very constructed example since the following is '''the''' way to do it: proc printlist args {foreach item $args {puts $item}} However i hope to illustrate the point of using 'proc' and 'args' for list iteration. For counting stuff consider: proc forloop i { if $i {puts $i; forloop [incr i -1]}} Oh.. this counts down and stops with '1'! Ahem, does that really matter? Yes! Then use: proc forloop {i n} { if $n {puts $i; forloop [incr i 1] [incr n -1]}} See [Tail call optimization] for more on recursion and: program language specialists please jump in. [LEG] ---- [RLH] That code is hard to read. Much more so than regular Tcl syntax style.