Version 4 of Tcl Minimal Escaping Style

Updated 2015-04-22 17:35:58 by dbohdan

A minimal Tcl escaping style eschews unnecessary Tcl escaping syntax.

Description

A lot of Tcl code that one encounters employs double quotes even when not strictly necessary. Sometimes this is done to make a text editor highlight those values; other times to make Tcl seem more like C or a Unix shell. Such usage can be confusing to the beginner who is trying to understand the intended use of double quotes, braces, and backslash substitution. Furthermore, the use of these characters can at times have a performance impact that the beginner may find subtle and surprising. The minimal Tcl escaping style presented on this page results in Tcl code that only employs Tcl escape characters where semantically necessary, resulting in code that naturally avoids the pitfalls of extraneous escaping.

In some languages such as C, Javascript, Python, double quotes indicate a certain type of value: a string. In Tcl, all values are already strings, so double quotes do not have that function. Instead, they act as syntactic sugar for backslash escaping, providing a much more convenient syntax for words that include whitespace and other special characters. Braces do the same, but are more restricted in what special characters they escape. In summary, double quotes and braces don't do anything backslash can't do, but they're often more convenient.

Here are the rules of the minimal escaping style:

braces
Don't use braces where no backslash substitutions are otherwise needed.
quotes
Don't use quotes where braces could be used.

That's it. If your code follows this style, you can avoid potential performance issues like "unintended shimmering", as well as whatever miniscule performance improvement that comes from minimizing double quote processing by the Tcl interpreter. This minimal style also "scales" better as one moves into more complex forms of Tcl scripting such as code generation. Best of all , beginners will be able to learn by reading your code when braces and quotes are actually needed. It also helps to disabuse them of that all-to-common misunderstanding that braces mean "list".

Examples

#instead of
puts "Hello!"

#use
puts Hello!
#instead of 
puts "Hello World."

#use
puts {Hello, World.}
#instead of 
proc {x} { ... }

#use
proc x { ... }