Version 15 of Tcl Minimal Escaping Style

Updated 2015-04-30 18:35:58 by pooryorick

A minimal Tcl escaping style is a code style that 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".

EMJ: Except that that's not it because

  • Command substitution, variable substitution, and backslash substitution are performed on the characters between the quotes.
  • No substitutions are performed on the characters between the braces except for backslash-newline substitutions ..., nor do semi-colons, newlines, close brackets, or white space receive any special interpretation. The word will consist of exactly the characters between the outer braces, not including the braces themselves.

Both the above are quotes from the dodekalogue.

PYK 2014-04-30: Except that it is. This page isn't about substitutions. That's laid out on other pages, and the reader is expected to know, or at least to be able to find that information. Substitutions are one class of things you might escape, and you might choose braces to escape them. The minimal quoting style implies that you wouldn't quote variable or command substitutions unless some whitespace in the value needed to be escaped. In that case, the rules for the minimal style clearly indicate that you should use quotes because braces would over-escape the value. A counterexample that you think shows what these rules don't cover would be helpful.

Discussion

EMJ (2015-04-22) I added the bit about substitution differences, but it's not discussion, it's essential that something about that is on this page to avoid misleading beginners. Rewrite it if you want but I think it is necessary that it is mentioned here. Moving it under a Discussion heading is contrary to the spirit of co-operative editing.

EMJ (2015-04-22) Re "Sometimes this is done..." near the beginning: it might be done to clarify the purpose of a string, or with an eye on a future change (and so is about program maintainability). And I just don't believe the "seem more like C" bit.

dbohdan 2015-04-22: Sorry about that. I moved your contribution under a new heading because it was clearly a counterargument to the argument put forth in the Description. Just merging it with the description made the text sound strangely self-refuting (a common problem, e.g., on WikiWikiWeb ). My initial choice of heading was probably a poor one but I do think it needs a subsection of its own, e.g., Criticism or Counterargument (with Description renamed Argument in the latter case).

PYK 2015-04-30: Anyone who thinks they can improve this page is free to dive in and rewrite it wholesale as far as I'm concerned. If an edit war starts to shape up, we can back off and discuss. The end goal is to get quality content here. I'm sure EMJ's contentions will eventually result in a better exposition of the idea.

Examples

#instead of
puts "Hello!"

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

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

#use
proc x { ... }

See also