dbohdan 2014-07-24: I haven't found discussions on the matter on the wiki, so I made this page.
What do you guys think of the following style of indentation for functional-leaning Tcl code?
# style #1 return [ join [ ltrim [ struct::list mapfor x $msgLines {string range $x $indent end} ] ] "\n" ]
Its main benefit over "Lispish" indentation like
# style #2 return \ [join \ [ltrim \ [struct::list mapfor x $msgLines {string range $x $indent end}]] "\n"]
or
# style #3 return [join \ [ltrim \ [struct::list mapfor x $msgLines {string range $x $indent end}]] "\n"]
is that it doesn't break when a backslash at the end of a line goes missing. The respective downside is that when you can't tell whether a command at the beginning of the line is being quoted or substituted from just looking at it; you have to see whether the line above it ends with a [ or a {.
On a related note, have you programmed your text editor to manage the backslashes within square brackets for the second/third style of indentation? I'm considering doing that but it still seems brittle: insert one newline in another editor (e.g., vi on the command line) and it breaks.
An rather more ugly example:
# style #1 set conn [ ::ftp::Open [ dict get $websiteConfig deployFtpServer ] [ dict get $websiteConfig deployFtpUser ] [ dict get $websiteConfig deployFtpPassword ] -port [ dict-default-get 21 $websiteConfig deployFtpPort ] -mode passive ]
vs.
# style #3 set conn [::ftp::Open \ [dict get $websiteConfig deployFtpServer] \ [dict get $websiteConfig deployFtpUser] \ [dict get $websiteConfig deployFtpPassword] \ -port [dict-default-get 21 $websiteConfig deployFtpPort] \ -mode passive]
aspect - vote #1 for me. I hate line-continuation backslashes and will try to avoid them wherever possible. The difficulty distinguishing bracket-continuations from brace-continuations hasn't proven a problem for me - I think mostly because deeply nested code isn't particularly fun in Tcl under normal circumstances.
Your last example (style #3) invokes another situation where I will use continuation backslashes though: calling a procedure with lots of arguments. In this case I'll double-indent the arguments, which seems to be advocated in the Tcl Style Guide:
$w configure \ -label [dict get $state what_it_is_called] \ -command [lambda {} {puts "Hello, world!"}] \ -otherattribute some-really-long-continued-value \ ;# end $w configure
Note the comment at the end - that's a flourish of my own (?) which I add so the argument lines can be freely rearranged without the risk of forgetting to remove the last \.
And I think struct::list mapfor is obsoleted by lmap -- if not, I'd interp alias it to a less ugly name for common use :-).
dzach 2014-7-27 : One other useful aspect of the proposed style, when used in a proc, is that it preserves the white space, including line feeds, when doing [info body], thus allowing the regeneration of the initial appearance of a proc during an introspection.